-
Notifications
You must be signed in to change notification settings - Fork 742
Expand file tree
/
Copy pathactive_links_test.tsx
More file actions
177 lines (159 loc) · 5.38 KB
/
active_links_test.tsx
File metadata and controls
177 lines (159 loc) · 5.38 KB
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
import { App, staticFiles } from "fresh";
import {
allIslandApp,
assertNotSelector,
assertSelector,
buildProd,
Doc,
getIsland,
parseHtml,
withBrowserApp,
} from "./test_utils.tsx";
import { SelfCounter } from "./fixtures_islands/SelfCounter.tsx";
import { PartialInIsland } from "./fixtures_islands/PartialInIsland.tsx";
import { JsonIsland } from "./fixtures_islands/JsonIsland.tsx";
import { FakeServer } from "../src/test_utils.ts";
import { Partial } from "fresh/runtime";
import { getBuildCache, setBuildCache } from "../src/app.ts";
await buildProd(allIslandApp);
function testApp<T>(): App<T> {
const selfCounter = getIsland("SelfCounter.tsx");
const partialInIsland = getIsland("PartialInIsland.tsx");
const jsonIsland = getIsland("JsonIsland.tsx");
const app = new App<T>()
.island(selfCounter, "SelfCounter", SelfCounter)
.island(partialInIsland, "PartialInIsland", PartialInIsland)
.island(jsonIsland, "JsonIsland", JsonIsland)
.use(staticFiles());
setBuildCache(app, getBuildCache(allIslandApp));
return app;
}
Deno.test({
name: "active links - without client nav",
fn: async () => {
function View() {
return (
<Doc>
<div>
<h1>nav</h1>
<p>
<a href="/active_nav/foo/bar">/active_nav/foo/bar</a>
</p>
<p>
<a href="/active_nav/foo">/active_nav/foo</a>
</p>
<p>
<a href="/active_nav">/active_nav</a>
</p>
<p>
<a href="/">/</a>
</p>
</div>
</Doc>
);
}
const app = testApp()
.get("/active_nav/foo", (ctx) => {
return ctx.render(<View />);
})
.get("/active_nav", (ctx) => {
return ctx.render(<View />);
});
const server = new FakeServer(app.handler());
let res = await server.get("/active_nav");
let doc = parseHtml(await res.text());
assertSelector(doc, "a[href='/'][data-ancestor]");
// Current
assertNotSelector(doc, "a[href='/active_nav'][data-ancestor]");
assertSelector(doc, "a[href='/active_nav'][data-current]");
assertSelector(doc, `a[href='/active_nav'][aria-current="page"]`);
// Unrelated links
assertNotSelector(doc, "a[href='/active_nav/foo'][data-ancestor]");
assertNotSelector(doc, "a[href='/active_nav/foo'][aria-current]");
assertNotSelector(doc, "a[href='/active_nav/foo/bar'][data-ancestor]");
assertNotSelector(doc, "a[href='/active_nav/foo/bar'][aria-current]");
res = await server.get(`/active_nav/foo`);
doc = parseHtml(await res.text());
assertSelector(doc, "a[href='/active_nav/foo'][data-current]");
assertSelector(doc, `a[href='/active_nav/foo'][aria-current="page"]`);
assertSelector(doc, "a[href='/active_nav'][data-ancestor]");
assertSelector(doc, `a[href='/active_nav'][aria-current="true"]`);
assertSelector(doc, "a[href='/'][data-ancestor]");
assertSelector(doc, `a[href='/'][aria-current="true"]`);
},
});
Deno.test({
name: "active links - updates outside of vdom",
fn: async () => {
function PartialPage() {
return (
<div>
<Partial name="content">
<h1>/active_nav_partial</h1>
</Partial>
<p>
<a href="/active_nav_partial/foo/bar">
/active_nav_partial/foo/bar
</a>
</p>
<p>
<a href="/active_nav_partial/foo">/active_nav_partial/foo</a>
</p>
<p>
<a href="/active_nav_partial">/active_nav_partial</a>
</p>
<p>
<a href="/">/</a>
</p>
</div>
);
}
const app = testApp()
.get("/active_nav_partial/foo", (ctx) => {
return ctx.render(<PartialPage />);
})
.get("/active_nav_partial", (ctx) => {
return ctx.render(<PartialPage />);
});
await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/active_nav_partial`);
let doc = parseHtml(await page.content());
assertSelector(doc, "a[href='/'][data-ancestor]");
// Current
assertNotSelector(doc, "a[href='/active_nav_partial'][data-ancestor]");
assertSelector(doc, "a[href='/active_nav_partial'][data-current]");
assertSelector(doc, `a[href='/active_nav_partial'][aria-current="page"]`);
// Unrelated links
assertNotSelector(
doc,
"a[href='/active_nav_partial/foo'][data-ancestor]",
);
assertNotSelector(
doc,
"a[href='/active_nav_partial/foo'][aria-current]",
);
assertNotSelector(
doc,
"a[href='/active_nav_partial/foo/bar'][data-ancestor]",
);
assertNotSelector(
doc,
"a[href='/active_nav_partial/foo/bar'][aria-current]",
);
await page.goto(`${address}/active_nav_partial/foo`);
doc = parseHtml(await page.content());
assertSelector(doc, "a[href='/active_nav_partial/foo'][data-current]");
assertSelector(
doc,
`a[href='/active_nav_partial/foo'][aria-current="page"]`,
);
assertSelector(doc, "a[href='/active_nav_partial'][data-ancestor]");
assertSelector(
doc,
`a[href='/active_nav_partial'][data-ancestor][aria-current="true"]`,
);
assertSelector(doc, "a[href='/'][data-ancestor]");
assertSelector(doc, `a[href='/'][aria-current="true"]`);
});
},
});