-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintegration.test.tsx
187 lines (171 loc) · 6.23 KB
/
integration.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { createFreshContext } from "./server.ts";
import {
cleanup,
getByText,
render,
setup,
userEvent,
} from "$fresh-testing-library/components.ts";
import { expect } from "$fresh-testing-library/expect.ts";
import { assert } from "$std/assert/assert.ts";
import { assertEquals } from "$std/assert/assert_equals.ts";
import { assertExists } from "$std/assert/assert_exists.ts";
import { assertStrictEquals } from "$std/assert/assert_strict_equals.ts";
import { assertStringIncludes } from "$std/assert/assert_string_includes.ts";
import { afterEach, beforeAll, describe, it } from "$std/testing/bdd.ts";
import { cheerio } from "./deps/cheerio.ts";
import { default as UserDetail } from "./demo/routes/users/[id].tsx";
import { default as manifest } from "./demo/fresh.gen.ts";
import type { Data } from "./demo/routes/(admin)/dashboard.tsx";
import { handler } from "./demo/routes/(admin)/dashboard.tsx";
import { createInMemoryUsers } from "./demo/services/users.ts";
import DocPage from "./demo/routes/docs/[...path].tsx";
import DocLayout from "./demo/routes/docs/_layout.tsx";
import { assertDefaultResponseAsync } from "$fresh-testing-library/internal/test_utils/mod.ts";
import type { State } from "🗺/users/_middleware.ts";
describe("integration tests", () => {
beforeAll(() => setup({ manifest }));
afterEach(cleanup);
describe("routes", () => {
it("supports testing an async route component", async () => {
const req = new Request("http://localhost:8000/users/2");
const state = { users: createInMemoryUsers() };
const ctx = createFreshContext<void, typeof state>(req, {
manifest,
state,
});
const screen = render(await UserDetail(req, ctx));
const list = screen.getByRole("group");
assertExists(getByText(list, "2"));
assertExists(getByText(list, "bar"));
});
it("supports testing a route component with `handler.GET`", async () => {
// https://github.com/uki00a/fresh-testing-library/issues/38
const request = new Request("http://localhost:8000/dashboard");
const ctx = createFreshContext<Data, Data>(
request,
{
state: { activeUsers: 45, totalUsers: 123 },
manifest,
},
);
assert(handler.GET);
const res = await handler.GET(request, ctx);
assertEquals(res.status, 200);
assertEquals(res.headers.get("content-type"), "text/html; charset=UTF-8");
const html = await res.text();
const $ = cheerio.load(html);
const $dd = $("dd");
assertEquals($dd.length, 2);
assertEquals($dd.eq(0).text(), "123");
assertEquals($dd.eq(1).text(), "45");
});
it("should use `SetupOptions.manifest` as the default manifest for `createFreshContext`", async () => {
// TODO: use `demo/routes/index.tsx` for testing.
const req = new Request("http://localhost:8003/users/1");
const user = Object.freeze({
id: 1,
name: "foo",
email: "[email protected]",
});
const users = [user];
const state: State = {
users: {
all() {
return Promise.resolve(users);
},
getByID(id) {
const user = users.find((x) => x.id === id);
if (user == null) {
throw new Error("NotFound");
}
return Promise.resolve(user);
},
},
};
{
const ctx = createFreshContext(req, { manifest: undefined, state });
const res = await ctx.render();
await assertDefaultResponseAsync(res);
}
{
const ctx = createFreshContext(req, { state });
const res = await ctx.render();
assertStrictEquals(res.status, 200);
const html = await res.text();
assertStringIncludes(
html,
user.name,
"`routes/users/[id].tsx` should be rendered",
);
assertStringIncludes(
html,
user.email,
"`routes/users/[id].tsx` should be rendered",
);
}
});
});
describe("partials", () => {
it("supports client navigation by an `<a>` tag", async () => {
const data = Object.freeze({
content: `First content`,
});
const ctx = createFreshContext({
manifest,
data,
});
const screen = render(
// TODO: support rendering `_app.tsx` and `_layout.tsx`.
<DocLayout {...ctx} Component={() => <DocPage {...ctx} />} />,
);
const user = userEvent.setup();
expect(screen.getByText(data.content)).toBeVisible();
const expectedText = "This module does not require any permissions.";
expect(screen.queryByText(expectedText)).not.toBeInTheDocument();
await user.click(screen.getByRole("link", { name: "Permissions" }));
expect(await screen.findByText(expectedText)).toBeInTheDocument();
expect(screen.queryByText(data.content)).not
.toBeInTheDocument();
});
it("supports `f-partial`", async () => {
const data = Object.freeze({
content: `First content`,
});
const partialLinkText = "This is a link with `f-partial`";
const ctx = createFreshContext({
manifest,
data,
});
const screen = render(
// TODO: support rendering `_app.tsx` and `_layout.tsx`.
<DocLayout
{...ctx}
Component={() => (
<>
<aside>
<nav f-client-nav>
<a href="/docs" f-partial="/docs/permissions">
{partialLinkText}
</a>
<a href="/docs" f-partial="/docs/no-such-page">
NotFound
</a>
</nav>
</aside>
<DocPage {...ctx} />
</>
)}
/>,
);
const user = userEvent.setup();
expect(screen.getByText(data.content)).toBeVisible();
const expectedText = "This module does not require any permissions.";
expect(screen.queryByText(expectedText)).not.toBeInTheDocument();
await user.click(screen.getByRole("link", { name: partialLinkText }));
expect(await screen.findByText(expectedText)).toBeInTheDocument();
expect(screen.queryByText(data.content)).not
.toBeInTheDocument();
});
});
});