@alepha/react 0.14.2 → 0.14.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/index.js +2 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +7 -4
- package/dist/core/index.js.map +1 -1
- package/dist/head/index.d.ts +17 -17
- package/dist/head/index.d.ts.map +1 -1
- package/dist/head/index.js +2 -1
- package/dist/head/index.js.map +1 -1
- package/dist/router/index.d.ts.map +1 -1
- package/dist/router/index.js +2 -2
- package/dist/router/index.js.map +1 -1
- package/package.json +3 -3
- package/src/auth/__tests__/$auth.spec.ts +188 -0
- package/src/core/__tests__/Router.spec.tsx +169 -0
- package/src/core/hooks/useAction.browser.spec.tsx +569 -0
- package/src/core/hooks/useAction.ts +11 -0
- package/src/form/hooks/useForm.browser.spec.tsx +366 -0
- package/src/head/__tests__/expandSeo.spec.ts +203 -0
- package/src/head/__tests__/page-head.spec.ts +39 -0
- package/src/head/__tests__/seo-head.spec.ts +121 -0
- package/src/head/hooks/useHead.spec.tsx +288 -0
- package/src/head/index.ts +2 -1
- package/src/head/providers/BrowserHeadProvider.browser.spec.ts +271 -0
- package/src/head/providers/ServerHeadProvider.spec.ts +163 -0
- package/src/i18n/__tests__/integration.spec.tsx +239 -0
- package/src/i18n/components/Localize.spec.tsx +357 -0
- package/src/i18n/hooks/useI18n.browser.spec.tsx +438 -0
- package/src/i18n/providers/I18nProvider.spec.ts +389 -0
- package/src/router/primitives/$page.browser.spec.tsx +702 -0
- package/src/router/primitives/$page.spec.tsx +702 -0
- package/src/router/providers/ReactServerProvider.spec.tsx +316 -0
- package/src/router/providers/ReactServerProvider.ts +4 -3
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { $page } from "@alepha/react/router";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { describe, it } from "vitest";
|
|
4
|
+
import { $head, AlephaReactHead } from "../index.ts";
|
|
5
|
+
|
|
6
|
+
class App {
|
|
7
|
+
head = $head({
|
|
8
|
+
title: "Alepha Framework",
|
|
9
|
+
description: "TypeScript framework made easy",
|
|
10
|
+
image: "https://alepha.dev/og-image.png",
|
|
11
|
+
url: "https://alepha.dev/",
|
|
12
|
+
siteName: "Alepha",
|
|
13
|
+
locale: "en_US",
|
|
14
|
+
type: "website",
|
|
15
|
+
imageWidth: 1200,
|
|
16
|
+
imageHeight: 630,
|
|
17
|
+
twitter: {
|
|
18
|
+
card: "summary_large_image",
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
home = $page({
|
|
23
|
+
component: () => "Home",
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const alepha = Alepha.create().with(AlephaReactHead);
|
|
28
|
+
const a = alepha.inject(App);
|
|
29
|
+
|
|
30
|
+
describe("SEO Head", () => {
|
|
31
|
+
it("should render page with SEO meta tags", async ({ expect }) => {
|
|
32
|
+
const result = await a.home.render({ html: true, hydration: false });
|
|
33
|
+
|
|
34
|
+
// Should include description
|
|
35
|
+
expect(result.html).toContain(
|
|
36
|
+
'<meta name="description" content="TypeScript framework made easy">',
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
// Should include OpenGraph tags with property attribute
|
|
40
|
+
expect(result.html).toContain(
|
|
41
|
+
'<meta property="og:title" content="Alepha Framework">',
|
|
42
|
+
);
|
|
43
|
+
expect(result.html).toContain(
|
|
44
|
+
'<meta property="og:description" content="TypeScript framework made easy">',
|
|
45
|
+
);
|
|
46
|
+
expect(result.html).toContain(
|
|
47
|
+
'<meta property="og:image" content="https://alepha.dev/og-image.png">',
|
|
48
|
+
);
|
|
49
|
+
expect(result.html).toContain(
|
|
50
|
+
'<meta property="og:url" content="https://alepha.dev/">',
|
|
51
|
+
);
|
|
52
|
+
expect(result.html).toContain('<meta property="og:type" content="website">');
|
|
53
|
+
expect(result.html).toContain(
|
|
54
|
+
'<meta property="og:site_name" content="Alepha">',
|
|
55
|
+
);
|
|
56
|
+
expect(result.html).toContain(
|
|
57
|
+
'<meta property="og:locale" content="en_US">',
|
|
58
|
+
);
|
|
59
|
+
expect(result.html).toContain(
|
|
60
|
+
'<meta property="og:image:width" content="1200">',
|
|
61
|
+
);
|
|
62
|
+
expect(result.html).toContain(
|
|
63
|
+
'<meta property="og:image:height" content="630">',
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
// Should include Twitter Card tags with name attribute
|
|
67
|
+
expect(result.html).toContain(
|
|
68
|
+
'<meta name="twitter:card" content="summary_large_image">',
|
|
69
|
+
);
|
|
70
|
+
expect(result.html).toContain(
|
|
71
|
+
'<meta name="twitter:title" content="Alepha Framework">',
|
|
72
|
+
);
|
|
73
|
+
expect(result.html).toContain(
|
|
74
|
+
'<meta name="twitter:description" content="TypeScript framework made easy">',
|
|
75
|
+
);
|
|
76
|
+
expect(result.html).toContain(
|
|
77
|
+
'<meta name="twitter:image" content="https://alepha.dev/og-image.png">',
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
// Should include canonical link
|
|
81
|
+
expect(result.html).toContain(
|
|
82
|
+
'<link rel="canonical" href="https://alepha.dev/">',
|
|
83
|
+
);
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
class AppWithPageSeo {
|
|
88
|
+
head = $head({
|
|
89
|
+
title: "My Site",
|
|
90
|
+
titleSeparator: " | ",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
blog = $page({
|
|
94
|
+
path: "/blog",
|
|
95
|
+
head: {
|
|
96
|
+
title: "Blog",
|
|
97
|
+
description: "Read our latest articles",
|
|
98
|
+
image: "https://example.com/blog-og.png",
|
|
99
|
+
type: "article",
|
|
100
|
+
},
|
|
101
|
+
component: () => "Blog",
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
const alepha2 = Alepha.create().with(AlephaReactHead);
|
|
106
|
+
const app2 = alepha2.inject(AppWithPageSeo);
|
|
107
|
+
|
|
108
|
+
describe("SEO Head on Page", () => {
|
|
109
|
+
it("should render page-level SEO", async ({ expect }) => {
|
|
110
|
+
const result = await app2.blog.render({ html: true, hydration: false });
|
|
111
|
+
|
|
112
|
+
expect(result.html).toContain("<title>Blog | My Site</title>");
|
|
113
|
+
expect(result.html).toContain(
|
|
114
|
+
'<meta name="description" content="Read our latest articles">',
|
|
115
|
+
);
|
|
116
|
+
expect(result.html).toContain('<meta property="og:type" content="article">');
|
|
117
|
+
expect(result.html).toContain(
|
|
118
|
+
'<meta property="og:image" content="https://example.com/blog-og.png">',
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
});
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
import { AlephaContext } from "@alepha/react";
|
|
2
|
+
import type { Head } from "../index.ts";
|
|
3
|
+
import { useHead } from "../index.ts";
|
|
4
|
+
import { render } from "@testing-library/react";
|
|
5
|
+
import { Alepha } from "alepha";
|
|
6
|
+
import { act, type ReactNode } from "react";
|
|
7
|
+
import { beforeEach, describe, it, vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @vitest-environment jsdom
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
describe("useHead", () => {
|
|
14
|
+
const renderWithAlepha = (alepha: Alepha, element: ReactNode) => {
|
|
15
|
+
return render(
|
|
16
|
+
<AlephaContext.Provider value={alepha}>{element}</AlephaContext.Provider>,
|
|
17
|
+
);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
// Reset document state before each test
|
|
22
|
+
document.title = "";
|
|
23
|
+
document.head.innerHTML = "";
|
|
24
|
+
document.body.removeAttribute("class");
|
|
25
|
+
document.body.removeAttribute("style");
|
|
26
|
+
document.documentElement.removeAttribute("lang");
|
|
27
|
+
document.documentElement.removeAttribute("class");
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("should set initial head options on mount", ({ expect }) => {
|
|
31
|
+
const alepha = Alepha.create();
|
|
32
|
+
const TestComponent = () => {
|
|
33
|
+
useHead({
|
|
34
|
+
title: "Test Title",
|
|
35
|
+
});
|
|
36
|
+
return <div>Test</div>;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
40
|
+
|
|
41
|
+
expect(document.title).toBe("Test Title");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("should return current head state and setter function", ({ expect }) => {
|
|
45
|
+
const alepha = Alepha.create();
|
|
46
|
+
let headState: Head;
|
|
47
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
48
|
+
|
|
49
|
+
const TestComponent = () => {
|
|
50
|
+
const [head, setHead] = useHead();
|
|
51
|
+
headState = head;
|
|
52
|
+
setHeadFn = setHead;
|
|
53
|
+
return <div>Test</div>;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
57
|
+
|
|
58
|
+
expect(headState!).toBeDefined();
|
|
59
|
+
expect(setHeadFn!).toBeDefined();
|
|
60
|
+
expect(typeof setHeadFn!).toBe("function");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("should update document title when setHead is called", ({ expect }) => {
|
|
64
|
+
const alepha = Alepha.create();
|
|
65
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
66
|
+
|
|
67
|
+
const TestComponent = () => {
|
|
68
|
+
const [, setHead] = useHead();
|
|
69
|
+
setHeadFn = setHead;
|
|
70
|
+
return <div>Test</div>;
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
74
|
+
|
|
75
|
+
act(() => {
|
|
76
|
+
setHeadFn({ title: "Updated Title" });
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
expect(document.title).toBe("Updated Title");
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("should update meta tags when setHead is called", ({ expect }) => {
|
|
83
|
+
const alepha = Alepha.create();
|
|
84
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
85
|
+
|
|
86
|
+
const TestComponent = () => {
|
|
87
|
+
const [, setHead] = useHead();
|
|
88
|
+
setHeadFn = setHead;
|
|
89
|
+
return <div>Test</div>;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
93
|
+
|
|
94
|
+
act(() => {
|
|
95
|
+
setHeadFn({
|
|
96
|
+
meta: [
|
|
97
|
+
{ name: "description", content: "Test Description" },
|
|
98
|
+
{ name: "keywords", content: "test, keywords" },
|
|
99
|
+
],
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const descriptionMeta = document.querySelector('meta[name="description"]');
|
|
104
|
+
const keywordsMeta = document.querySelector('meta[name="keywords"]');
|
|
105
|
+
|
|
106
|
+
expect(descriptionMeta?.getAttribute("content")).toBe("Test Description");
|
|
107
|
+
expect(keywordsMeta?.getAttribute("content")).toBe("test, keywords");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("should update body attributes when setHead is called", ({ expect }) => {
|
|
111
|
+
const alepha = Alepha.create();
|
|
112
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
113
|
+
|
|
114
|
+
const TestComponent = () => {
|
|
115
|
+
const [, setHead] = useHead();
|
|
116
|
+
setHeadFn = setHead;
|
|
117
|
+
return <div>Test</div>;
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
121
|
+
|
|
122
|
+
act(() => {
|
|
123
|
+
setHeadFn({
|
|
124
|
+
bodyAttributes: {
|
|
125
|
+
class: "dark-theme",
|
|
126
|
+
style: "background: black;",
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
expect(document.body.getAttribute("class")).toBe("dark-theme");
|
|
132
|
+
expect(document.body.getAttribute("style")).toBe("background: black;");
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should update html attributes when setHead is called", ({ expect }) => {
|
|
136
|
+
const alepha = Alepha.create();
|
|
137
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
138
|
+
|
|
139
|
+
const TestComponent = () => {
|
|
140
|
+
const [, setHead] = useHead();
|
|
141
|
+
setHeadFn = setHead;
|
|
142
|
+
return <div>Test</div>;
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
146
|
+
|
|
147
|
+
act(() => {
|
|
148
|
+
setHeadFn({
|
|
149
|
+
htmlAttributes: {
|
|
150
|
+
lang: "en",
|
|
151
|
+
class: "no-js",
|
|
152
|
+
},
|
|
153
|
+
});
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(document.documentElement.getAttribute("lang")).toBe("en");
|
|
157
|
+
expect(document.documentElement.getAttribute("class")).toBe("no-js");
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it("should support functional updates", ({ expect }) => {
|
|
161
|
+
const alepha = Alepha.create();
|
|
162
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
163
|
+
|
|
164
|
+
const TestComponent = () => {
|
|
165
|
+
const [, setHead] = useHead({ title: "Initial Title" });
|
|
166
|
+
setHeadFn = setHead;
|
|
167
|
+
return <div>Test</div>;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
171
|
+
|
|
172
|
+
expect(document.title).toBe("Initial Title");
|
|
173
|
+
|
|
174
|
+
act(() => {
|
|
175
|
+
setHeadFn((prev) => ({
|
|
176
|
+
...prev,
|
|
177
|
+
title: `${prev?.title} - Updated`,
|
|
178
|
+
}));
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(document.title).toBe("Initial Title - Updated");
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("should handle multiple head updates", ({ expect }) => {
|
|
185
|
+
const alepha = Alepha.create();
|
|
186
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
187
|
+
|
|
188
|
+
const TestComponent = () => {
|
|
189
|
+
const [, setHead] = useHead();
|
|
190
|
+
setHeadFn = setHead;
|
|
191
|
+
return <div>Test</div>;
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
195
|
+
|
|
196
|
+
act(() => {
|
|
197
|
+
setHeadFn({
|
|
198
|
+
title: "First Title",
|
|
199
|
+
meta: [{ name: "description", content: "First Description" }],
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
expect(document.title).toBe("First Title");
|
|
204
|
+
expect(
|
|
205
|
+
document
|
|
206
|
+
.querySelector('meta[name="description"]')
|
|
207
|
+
?.getAttribute("content"),
|
|
208
|
+
).toBe("First Description");
|
|
209
|
+
|
|
210
|
+
act(() => {
|
|
211
|
+
setHeadFn({
|
|
212
|
+
title: "Second Title",
|
|
213
|
+
meta: [{ name: "description", content: "Second Description" }],
|
|
214
|
+
});
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
expect(document.title).toBe("Second Title");
|
|
218
|
+
expect(
|
|
219
|
+
document
|
|
220
|
+
.querySelector('meta[name="description"]')
|
|
221
|
+
?.getAttribute("content"),
|
|
222
|
+
).toBe("Second Description");
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it("should not crash on server-side (non-browser environment)", ({
|
|
226
|
+
expect,
|
|
227
|
+
}) => {
|
|
228
|
+
const alepha = Alepha.create();
|
|
229
|
+
// Mock isBrowser to return false
|
|
230
|
+
vi.spyOn(alepha, "isBrowser").mockReturnValue(false);
|
|
231
|
+
|
|
232
|
+
let headState: Head;
|
|
233
|
+
let setHeadFn: (head?: Head | ((previous?: Head) => Head)) => void;
|
|
234
|
+
|
|
235
|
+
const TestComponent = () => {
|
|
236
|
+
const [head, setHead] = useHead({ title: "Server Title" });
|
|
237
|
+
headState = head;
|
|
238
|
+
setHeadFn = setHead;
|
|
239
|
+
return <div>Test</div>;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
expect(() => {
|
|
243
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
244
|
+
}).not.toThrow();
|
|
245
|
+
|
|
246
|
+
expect(headState!).toEqual({});
|
|
247
|
+
|
|
248
|
+
// setHead should not crash on server
|
|
249
|
+
expect(() => {
|
|
250
|
+
setHeadFn({ title: "New Title" });
|
|
251
|
+
}).not.toThrow();
|
|
252
|
+
|
|
253
|
+
// Document title should remain unchanged on server
|
|
254
|
+
expect(document.title).toBe("");
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it("should get current head state from document", ({ expect }) => {
|
|
258
|
+
const alepha = Alepha.create();
|
|
259
|
+
|
|
260
|
+
// Pre-populate document with some head data
|
|
261
|
+
document.title = "Existing Title";
|
|
262
|
+
document.body.setAttribute("class", "existing-class");
|
|
263
|
+
document.documentElement.setAttribute("lang", "fr");
|
|
264
|
+
|
|
265
|
+
const meta = document.createElement("meta");
|
|
266
|
+
meta.setAttribute("name", "author");
|
|
267
|
+
meta.setAttribute("content", "John Doe");
|
|
268
|
+
document.head.appendChild(meta);
|
|
269
|
+
|
|
270
|
+
let headState: Head;
|
|
271
|
+
|
|
272
|
+
const TestComponent = () => {
|
|
273
|
+
const [head] = useHead();
|
|
274
|
+
headState = head;
|
|
275
|
+
return <div>Test</div>;
|
|
276
|
+
};
|
|
277
|
+
|
|
278
|
+
renderWithAlepha(alepha, <TestComponent />);
|
|
279
|
+
|
|
280
|
+
expect(headState!.title).toBe("Existing Title");
|
|
281
|
+
expect(headState!.bodyAttributes?.class).toBe("existing-class");
|
|
282
|
+
expect(headState!.htmlAttributes?.lang).toBe("fr");
|
|
283
|
+
expect(headState!.meta).toContainEqual({
|
|
284
|
+
name: "author",
|
|
285
|
+
content: "John Doe",
|
|
286
|
+
});
|
|
287
|
+
});
|
|
288
|
+
});
|
package/src/head/index.ts
CHANGED
|
@@ -9,6 +9,7 @@ import { $head } from "./primitives/$head.ts";
|
|
|
9
9
|
import type { Head } from "./interfaces/Head.ts";
|
|
10
10
|
import { ServerHeadProvider } from "./providers/ServerHeadProvider.ts";
|
|
11
11
|
import { HeadProvider } from "./providers/HeadProvider.ts";
|
|
12
|
+
import { SeoExpander } from "./helpers/SeoExpander.ts";
|
|
12
13
|
|
|
13
14
|
// ---------------------------------------------------------------------------------------------------------------------
|
|
14
15
|
|
|
@@ -54,5 +55,5 @@ declare module "@alepha/react/router" {
|
|
|
54
55
|
export const AlephaReactHead = $module({
|
|
55
56
|
name: "alepha.react.head",
|
|
56
57
|
primitives: [$head],
|
|
57
|
-
services: [AlephaReact, ServerHeadProvider, HeadProvider],
|
|
58
|
+
services: [AlephaReact, ServerHeadProvider, HeadProvider, SeoExpander],
|
|
58
59
|
});
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { $page } from "@alepha/react/router";
|
|
2
|
+
import { Alepha } from "alepha";
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
|
4
|
+
import { AlephaReactHead } from "../index.browser.ts";
|
|
5
|
+
import type { Head } from "../interfaces/Head.ts";
|
|
6
|
+
import { BrowserHeadProvider } from "./BrowserHeadProvider.ts";
|
|
7
|
+
|
|
8
|
+
describe("BrowserHeadProvider", () => {
|
|
9
|
+
let alepha: Alepha;
|
|
10
|
+
let provider: BrowserHeadProvider;
|
|
11
|
+
|
|
12
|
+
beforeEach(async () => {
|
|
13
|
+
alepha = Alepha.create();
|
|
14
|
+
provider = alepha.inject(BrowserHeadProvider);
|
|
15
|
+
|
|
16
|
+
// Reset document state
|
|
17
|
+
document.title = "";
|
|
18
|
+
document.head.innerHTML = "";
|
|
19
|
+
document.body.removeAttribute("class");
|
|
20
|
+
document.body.removeAttribute("style");
|
|
21
|
+
document.documentElement.removeAttribute("lang");
|
|
22
|
+
document.documentElement.removeAttribute("class");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
describe("getHead", () => {
|
|
26
|
+
it("should return current document head state", () => {
|
|
27
|
+
document.title = "Test Title";
|
|
28
|
+
document.body.setAttribute("class", "test-class");
|
|
29
|
+
document.documentElement.setAttribute("lang", "en");
|
|
30
|
+
|
|
31
|
+
const meta = document.createElement("meta");
|
|
32
|
+
meta.setAttribute("name", "description");
|
|
33
|
+
meta.setAttribute("content", "Test description");
|
|
34
|
+
document.head.appendChild(meta);
|
|
35
|
+
|
|
36
|
+
const head = provider.getHead(document);
|
|
37
|
+
|
|
38
|
+
expect(head.title).toBe("Test Title");
|
|
39
|
+
expect(head.bodyAttributes?.class).toBe("test-class");
|
|
40
|
+
expect(head.htmlAttributes?.lang).toBe("en");
|
|
41
|
+
expect(head.meta).toContainEqual({
|
|
42
|
+
name: "description",
|
|
43
|
+
content: "Test description",
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("should handle empty document state", () => {
|
|
48
|
+
const head = provider.getHead(document);
|
|
49
|
+
|
|
50
|
+
expect(head.title).toBe("");
|
|
51
|
+
expect(head.bodyAttributes).toEqual({});
|
|
52
|
+
expect(head.htmlAttributes).toEqual({});
|
|
53
|
+
expect(head.meta).toEqual([]);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
describe("renderHead", () => {
|
|
58
|
+
it("should set document title", () => {
|
|
59
|
+
const head: Head = { title: "New Title" };
|
|
60
|
+
|
|
61
|
+
provider.renderHead(document, head);
|
|
62
|
+
|
|
63
|
+
expect(document.title).toBe("New Title");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should set body attributes", () => {
|
|
67
|
+
const head: Head = {
|
|
68
|
+
bodyAttributes: {
|
|
69
|
+
class: "new-class",
|
|
70
|
+
style: "background: blue;",
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
provider.renderHead(document, head);
|
|
75
|
+
|
|
76
|
+
expect(document.body.getAttribute("class")).toBe("new-class");
|
|
77
|
+
expect(document.body.getAttribute("style")).toBe("background: blue;");
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should remove body attributes when value is falsy", () => {
|
|
81
|
+
document.body.setAttribute("class", "old-class");
|
|
82
|
+
|
|
83
|
+
const head: Head = {
|
|
84
|
+
bodyAttributes: {
|
|
85
|
+
class: "",
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
provider.renderHead(document, head);
|
|
90
|
+
|
|
91
|
+
expect(document.body.hasAttribute("class")).toBe(false);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("should set html attributes", () => {
|
|
95
|
+
const head: Head = {
|
|
96
|
+
htmlAttributes: {
|
|
97
|
+
lang: "fr",
|
|
98
|
+
dir: "ltr",
|
|
99
|
+
},
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
provider.renderHead(document, head);
|
|
103
|
+
|
|
104
|
+
expect(document.documentElement.getAttribute("lang")).toBe("fr");
|
|
105
|
+
expect(document.documentElement.getAttribute("dir")).toBe("ltr");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it("should remove html attributes when value is falsy", () => {
|
|
109
|
+
document.documentElement.setAttribute("lang", "en");
|
|
110
|
+
|
|
111
|
+
const head: Head = {
|
|
112
|
+
htmlAttributes: {
|
|
113
|
+
lang: "",
|
|
114
|
+
},
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
provider.renderHead(document, head);
|
|
118
|
+
|
|
119
|
+
expect(document.documentElement.hasAttribute("lang")).toBe(false);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("should create new meta tags", () => {
|
|
123
|
+
const head: Head = {
|
|
124
|
+
meta: [
|
|
125
|
+
{ name: "description", content: "Test description" },
|
|
126
|
+
{ name: "keywords", content: "test, browser" },
|
|
127
|
+
],
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
provider.renderHead(document, head);
|
|
131
|
+
|
|
132
|
+
const descriptionMeta = document.querySelector(
|
|
133
|
+
'meta[name="description"]',
|
|
134
|
+
);
|
|
135
|
+
const keywordsMeta = document.querySelector('meta[name="keywords"]');
|
|
136
|
+
|
|
137
|
+
expect(descriptionMeta?.getAttribute("content")).toBe("Test description");
|
|
138
|
+
expect(keywordsMeta?.getAttribute("content")).toBe("test, browser");
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it("should update existing meta tags", () => {
|
|
142
|
+
// Pre-populate with existing meta tag
|
|
143
|
+
const existingMeta = document.createElement("meta");
|
|
144
|
+
existingMeta.setAttribute("name", "description");
|
|
145
|
+
existingMeta.setAttribute("content", "Old description");
|
|
146
|
+
document.head.appendChild(existingMeta);
|
|
147
|
+
|
|
148
|
+
const head: Head = {
|
|
149
|
+
meta: [{ name: "description", content: "New description" }],
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
provider.renderHead(document, head);
|
|
153
|
+
|
|
154
|
+
const descriptionMeta = document.querySelector(
|
|
155
|
+
'meta[name="description"]',
|
|
156
|
+
);
|
|
157
|
+
expect(descriptionMeta?.getAttribute("content")).toBe("New description");
|
|
158
|
+
expect(
|
|
159
|
+
document.querySelectorAll('meta[name="description"]'),
|
|
160
|
+
).toHaveLength(1);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
it("should handle complete head object", () => {
|
|
164
|
+
const head: Head = {
|
|
165
|
+
title: "Complete Test",
|
|
166
|
+
htmlAttributes: {
|
|
167
|
+
lang: "es",
|
|
168
|
+
class: "theme-dark",
|
|
169
|
+
},
|
|
170
|
+
bodyAttributes: {
|
|
171
|
+
class: "page-test",
|
|
172
|
+
"data-theme": "dark",
|
|
173
|
+
},
|
|
174
|
+
meta: [
|
|
175
|
+
{ name: "description", content: "Complete test page" },
|
|
176
|
+
{ name: "author", content: "Test Author" },
|
|
177
|
+
],
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
provider.renderHead(document, head);
|
|
181
|
+
|
|
182
|
+
expect(document.title).toBe("Complete Test");
|
|
183
|
+
expect(document.documentElement.getAttribute("lang")).toBe("es");
|
|
184
|
+
expect(document.documentElement.getAttribute("class")).toBe("theme-dark");
|
|
185
|
+
expect(document.body.getAttribute("class")).toBe("page-test");
|
|
186
|
+
expect(document.body.getAttribute("data-theme")).toBe("dark");
|
|
187
|
+
|
|
188
|
+
const descriptionMeta = document.querySelector(
|
|
189
|
+
'meta[name="description"]',
|
|
190
|
+
);
|
|
191
|
+
const authorMeta = document.querySelector('meta[name="author"]');
|
|
192
|
+
expect(descriptionMeta?.getAttribute("content")).toBe(
|
|
193
|
+
"Complete test page",
|
|
194
|
+
);
|
|
195
|
+
expect(authorMeta?.getAttribute("content")).toBe("Test Author");
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
describe("$page integration", () => {
|
|
200
|
+
class TestApp {
|
|
201
|
+
simplePage = $page({
|
|
202
|
+
path: "/",
|
|
203
|
+
head: {
|
|
204
|
+
title: "Simple Page",
|
|
205
|
+
bodyAttributes: { class: "simple-page" },
|
|
206
|
+
},
|
|
207
|
+
component: () => "Simple content",
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
complexPage = $page({
|
|
211
|
+
path: "/complex",
|
|
212
|
+
head: {
|
|
213
|
+
title: "Complex Page",
|
|
214
|
+
htmlAttributes: {
|
|
215
|
+
lang: "en",
|
|
216
|
+
"data-theme": "dark",
|
|
217
|
+
},
|
|
218
|
+
bodyAttributes: {
|
|
219
|
+
class: "complex-page",
|
|
220
|
+
style: "background: black;",
|
|
221
|
+
},
|
|
222
|
+
meta: [
|
|
223
|
+
{ name: "description", content: "Complex test page" },
|
|
224
|
+
{
|
|
225
|
+
name: "viewport",
|
|
226
|
+
content: "width=device-width, initial-scale=1",
|
|
227
|
+
},
|
|
228
|
+
],
|
|
229
|
+
},
|
|
230
|
+
component: () => "Complex content",
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
afterEach(() => {
|
|
235
|
+
document.body.querySelector("#root")?.remove();
|
|
236
|
+
});
|
|
237
|
+
|
|
238
|
+
it("should render simple page head configuration", async () => {
|
|
239
|
+
const alepha = Alepha.create().with(AlephaReactHead).with(TestApp);
|
|
240
|
+
await alepha.start();
|
|
241
|
+
|
|
242
|
+
expect(document.title).toBe("Simple Page");
|
|
243
|
+
expect(document.body.getAttribute("class")).toBe("simple-page");
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
it("should get current head state and match page configuration", async () => {
|
|
247
|
+
const alepha = Alepha.create().with(AlephaReactHead);
|
|
248
|
+
const app = alepha.inject(TestApp);
|
|
249
|
+
await alepha.start();
|
|
250
|
+
|
|
251
|
+
// Apply complex page head
|
|
252
|
+
const headConfig = app.complexPage.options.head as Head;
|
|
253
|
+
provider.renderHead(document, headConfig);
|
|
254
|
+
|
|
255
|
+
// Get current head state
|
|
256
|
+
const currentHead = provider.getHead(document);
|
|
257
|
+
|
|
258
|
+
expect(currentHead.title).toBe(headConfig.title);
|
|
259
|
+
expect(currentHead.htmlAttributes?.lang).toBe(
|
|
260
|
+
headConfig.htmlAttributes?.lang,
|
|
261
|
+
);
|
|
262
|
+
expect(currentHead.bodyAttributes?.class).toBe(
|
|
263
|
+
headConfig.bodyAttributes?.class,
|
|
264
|
+
);
|
|
265
|
+
expect(currentHead.meta).toContainEqual({
|
|
266
|
+
name: "description",
|
|
267
|
+
content: "Complex test page",
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|