Skip to content

Commit 03f6b05

Browse files
Update dependency react-cookie to v8 (#271)
* Update dependency react-cookie to v8 * Fix missing cookie provider. Fix joi import. Add tests. --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Stan Lemon <stanlemon@users.noreply.github.com>
1 parent d2f4720 commit 03f6b05

File tree

8 files changed

+238
-39
lines changed

8 files changed

+238
-39
lines changed

apps/template/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stanlemon/app-template",
3-
"version": "0.3.80",
3+
"version": "0.3.82",
44
"description": "A template for creating apps using the webdev package.",
55
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
66
"license": "MIT",
@@ -27,7 +27,7 @@
2727
"@stanlemon/server-with-auth": "*",
2828
"@stanlemon/webdev": "*",
2929
"react": "^19.0.0",
30-
"react-cookie": "^7.2.2",
30+
"react-cookie": "^8.0.1",
3131
"react-dom": "^19.0.0",
3232
"wouter": "^3.6.0"
3333
},

apps/template/src/App.test.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import App from "./App";
44
import { ItemData } from "./views";
55
import { SessionAware } from "./Session";
66
import { fetchApi } from "./helpers/fetchApi";
7+
import { CookiesProvider } from "react-cookie";
78

89
jest.mock("./helpers/fetchApi");
910

@@ -14,9 +15,11 @@ describe("<App/>", () => {
1415

1516
it("logged out", async () => {
1617
render(
17-
<SessionAware initialized={true} token={null} user={null}>
18-
<App />
19-
</SessionAware>
18+
<CookiesProvider>
19+
<SessionAware initialized={true} token={null} user={null}>
20+
<App />
21+
</SessionAware>
22+
</CookiesProvider>
2023
);
2124

2225
expect(
@@ -37,17 +40,19 @@ describe("<App/>", () => {
3740
mockedFetchApi.mockResolvedValue([]);
3841

3942
render(
40-
<SessionAware
41-
initialized={true}
42-
token="abcd"
43-
user={{
44-
username: "user",
45-
name: "user",
46-
email: "user@example.com",
47-
}}
48-
>
49-
<App />
50-
</SessionAware>
43+
<CookiesProvider>
44+
<SessionAware
45+
initialized={true}
46+
token="abcd"
47+
user={{
48+
username: "user",
49+
name: "user",
50+
email: "user@example.com",
51+
}}
52+
>
53+
<App />
54+
</SessionAware>
55+
</CookiesProvider>
5156
);
5257

5358
// The header is present

apps/template/src/index.tsx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
import { createRoot } from "react-dom/client";
22
import App from "./App";
33
import Session from "./Session";
4+
import { CookiesProvider } from "react-cookie";
45

56
document.title = "App";
67

78
const root = createRoot(
89
document.body.appendChild(document.createElement("div"))
910
);
1011
root.render(
11-
<Session>
12-
<App />
13-
</Session>
12+
<CookiesProvider>
13+
<Session>
14+
<App />
15+
</Session>
16+
</CookiesProvider>
1417
);
1518

1619
const link = document.createElement("link");

package-lock.json

Lines changed: 51 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@stanlemon/server",
3-
"version": "0.3.45",
3+
"version": "0.3.46",
44
"description": "A basic express web server setup.",
55
"author": "Stan Lemon <stanlemon@users.noreply.github.com>",
66
"license": "MIT",
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import asyncJsonHandler from "./asyncJsonHandler";
2+
3+
describe("asyncHandler()", () => {
4+
it("handles fn response", async () => {
5+
const body = {
6+
hello: "World",
7+
};
8+
const req = jest.fn();
9+
const res = jest.fn();
10+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
11+
const next = jest.fn();
12+
13+
const controller = async (req, res, next) => {
14+
return Promise.resolve(body);
15+
};
16+
17+
await asyncJsonHandler(controller)(req, res, next);
18+
19+
expect(res.status.mock.calls[0][0]).toBe(200);
20+
expect(res.status().json.mock.calls[0][0]).toEqual(body);
21+
});
22+
23+
it("handles fn 400", async () => {
24+
const req = jest.fn();
25+
const res = jest.fn();
26+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
27+
const next = jest.fn();
28+
29+
const controller = async (req, res, next) => {
30+
throw new Error("Bad Request");
31+
};
32+
33+
await asyncJsonHandler(controller)(req, res, next);
34+
35+
expect(res.status.mock.calls[0][0]).toBe(400);
36+
});
37+
38+
it("handles fn 403", async () => {
39+
const req = jest.fn();
40+
const res = jest.fn();
41+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
42+
const next = jest.fn();
43+
44+
const controller = async (req, res, next) => {
45+
throw new Error("Not Authorized");
46+
};
47+
48+
await asyncJsonHandler(controller)(req, res, next);
49+
50+
expect(res.status.mock.calls[0][0]).toBe(403);
51+
});
52+
53+
it("handles fn 404", async () => {
54+
const req = jest.fn();
55+
const res = jest.fn();
56+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
57+
const next = jest.fn();
58+
59+
const controller = async (req, res, next) => {
60+
throw new Error("Not Found");
61+
};
62+
63+
await asyncJsonHandler(controller)(req, res, next);
64+
65+
expect(res.status.mock.calls[0][0]).toBe(404);
66+
});
67+
68+
it("handles fn 409", async () => {
69+
const req = jest.fn();
70+
const res = jest.fn();
71+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
72+
const next = jest.fn();
73+
74+
const controller = async (req, res, next) => {
75+
throw new Error("Already Exists");
76+
};
77+
78+
await asyncJsonHandler(controller)(req, res, next);
79+
80+
expect(res.status.mock.calls[0][0]).toBe(409);
81+
});
82+
83+
it("handles fn 500", async () => {
84+
const req = jest.fn();
85+
const res = jest.fn();
86+
res.status = jest.fn().mockReturnValue({ json: jest.fn() });
87+
const next = jest.fn();
88+
89+
const controller = async (req, res, next) => {
90+
throw new Error("Who knows!");
91+
};
92+
93+
await asyncJsonHandler(controller)(req, res, next);
94+
95+
expect(res.status.mock.calls[0][0]).toBe(500);
96+
});
97+
});

packages/server/src/schemaHandler.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { ValidationError } from "joi";
1+
import Joi from "joi";
22
import { asyncJsonHandler } from "./asyncJsonHandler.js";
33

4+
const { ValidationError } = Joi;
5+
46
/**
57
*
68
* @param {Joi.Schema} schema

0 commit comments

Comments
 (0)