@eldrajs/theme-cli 0.1.0 → 0.2.1
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/CHANGELOG.md +8 -0
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/{validate-C6jB8_Oc.js → validate-DprFpjV7.js} +29 -4
- package/package.json +3 -3
- package/template/.eldra/block-types.d.ts +12 -3
- package/template/.eldra/manifest.json +317 -26
- package/template/.eldra/previews.json +35 -34
- package/template/.github/workflows/eldra-deploy.yml +2 -0
- package/template/.gitlab-ci.yml +2 -0
- package/template/.storybook/main.ts +3 -0
- package/template/.storybook/preview.ts +2 -1
- package/template/.storybook/virtual-eldra.d.ts +5 -0
- package/template/README.md +31 -14
- package/template/app/components/FormHoneypot.vue +21 -0
- package/template/app/composables/useFormSpam.ts +55 -0
- package/template/app/composables/usePageStructuredData.ts +112 -0
- package/template/app/composables/useTurnstile.ts +111 -0
- package/template/app/formSeeds.ts +93 -0
- package/template/app/pages/[...slug].vue +48 -12
- package/template/app/plugins/eldra-storefront.ts +19 -2
- package/template/app/plugins/eldra-ui-messages.ts +1 -1
- package/template/app/seededPages.ts +7 -0
- package/template/app/storefront/analytics.ts +202 -0
- package/template/app/storefront/cart.ts +7 -0
- package/template/app/storefront/demo.ts +48 -2
- package/template/app/storefront/forms.ts +133 -0
- package/template/app/storefront/gateway.ts +113 -7
- package/template/app/storefront/money.ts +18 -17
- package/template/app/storefront/structuredData.ts +161 -0
- package/template/app/storefront/types.ts +101 -2
- package/template/app/utils/seoInput.ts +172 -0
- package/template/bitbucket-pipelines.yml +3 -1
- package/template/blocks/breadcrumbs/Block.vue +1 -0
- package/template/blocks/cart/Block.vue +1 -0
- package/template/blocks/cart/parts/Summary.vue +1 -0
- package/template/blocks/contact/Block.vue +50 -21
- package/template/blocks/contact/__tests__/Block.spec.ts +89 -8
- package/template/blocks/contact/block.json +2 -9
- package/template/blocks/contact/mock.json +1 -2
- package/template/blocks/footer/Block.vue +58 -8
- package/template/blocks/footer/__tests__/Block.spec.ts +70 -16
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/Block.vue +614 -0
- package/template/blocks/form/__tests__/Block.spec.ts +536 -0
- package/template/blocks/form/block.json +69 -0
- package/template/blocks/form/mock.json +25 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/form/validate.ts +68 -0
- package/template/blocks/newsletter/Block.vue +57 -3
- package/template/blocks/newsletter/__tests__/Block.spec.ts +89 -31
- package/template/blocks/newsletter/block.json +4 -5
- package/template/blocks/newsletter/mock.json +1 -1
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/product-detail/__tests__/Block.spec.ts +7 -10
- package/template/i18n/en-US.json +33 -2
- package/template/i18n/is-IS.json +33 -2
- package/template/nuxt.config.ts +15 -1
- package/template/package.json +1 -0
- package/template/public/apple-touch-icon.png +0 -0
- package/template/public/favicon.ico +0 -0
- package/template/stories/generated/form.stories.ts +68 -0
- package/template/test/formValidate.spec.ts +46 -0
- package/template/test/inlineEditable.spec.ts +2 -1
- package/template/test/moneyFormatting.spec.ts +4 -8
- package/template/test/pages/ssr.spec.ts +5 -11
- package/template/test/prerenderFailure.spec.ts +1 -1
- package/template/test/prerenderRefresh.browser.spec.ts +5 -1
- package/template/test/richText.spec.ts +12 -9
- package/template/test/seededPages.spec.ts +13 -0
- package/template/test/seoInput.spec.ts +119 -0
- package/template/test/slugPage.spec.ts +62 -26
- package/template/test/slugPageTemplate.spec.ts +5 -1
- package/template/test/starter.spec.ts +42 -4
- package/template/test/storefront/analytics.spec.ts +283 -0
- package/template/test/storefront/forms.spec.ts +385 -0
- package/template/test/storefront/money.spec.ts +11 -12
- package/template/test/storefront/structuredData.spec.ts +138 -0
- package/template/test/structuredData.generate.spec.ts +159 -0
- package/template/test/support/mockGateway.ts +29 -1
- package/template/test/support/turnstileForms.ts +53 -0
- package/template/test/useBlockData.spec.ts +7 -4
- package/template/app/utils/pageTitle.ts +0 -37
- package/template/test/pageTitle.spec.ts +0 -49
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
import { ref } from 'vue';
|
|
2
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
createEldraClient,
|
|
5
|
+
EldraHttpError,
|
|
6
|
+
type EldraFormDefinition,
|
|
7
|
+
type EldraHttpRequest,
|
|
8
|
+
} from '@eldrajs/sdk';
|
|
9
|
+
import { createGatewayStorefront } from '../../app/storefront/gateway';
|
|
10
|
+
import { localizeFormDefinition, pickLocalized } from '../../app/storefront/forms';
|
|
11
|
+
import type { StorefrontRoute } from '../../app/storefront/types';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* The forms half of `createGatewayStorefront` over the **real** SDK client with a stub transport:
|
|
15
|
+
* what `forms.definition` serves (a baked definition with no request, any other fetched, every
|
|
16
|
+
* per-locale text resolved for the page) and what `forms.submit` sends and how it reads each answer
|
|
17
|
+
* (`422` field messages, `429`, `413`, an unreachable platform, the `formsEndpoint` override).
|
|
18
|
+
*/
|
|
19
|
+
const definition: EldraFormDefinition = {
|
|
20
|
+
key: 'contact',
|
|
21
|
+
name: 'Contact',
|
|
22
|
+
fields: [
|
|
23
|
+
{
|
|
24
|
+
id: 'email',
|
|
25
|
+
type: 'email',
|
|
26
|
+
required: true,
|
|
27
|
+
label: { 'en-US': 'Email address', 'is-IS': 'Netfang' },
|
|
28
|
+
placeholder: { 'en-US': 'name@example.com' },
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
id: 'topic',
|
|
32
|
+
type: 'select',
|
|
33
|
+
required: false,
|
|
34
|
+
label: { 'en-US': 'Topic' },
|
|
35
|
+
options: [{ value: 'order', label: { 'en-US': 'An order', 'is-IS': 'Pöntun' } }],
|
|
36
|
+
validation: { maxLength: 40 },
|
|
37
|
+
},
|
|
38
|
+
],
|
|
39
|
+
honeypotField: 'website',
|
|
40
|
+
turnstileSiteKey: '1x00000000000000000000AA',
|
|
41
|
+
success: {
|
|
42
|
+
message: { 'en-US': 'Thanks.', 'is-IS': 'Takk.' },
|
|
43
|
+
redirect: { 'en-US': '/thanks' },
|
|
44
|
+
},
|
|
45
|
+
kinds: ['store', 'notify'],
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
function route(): StorefrontRoute {
|
|
49
|
+
return {
|
|
50
|
+
productHandle: null,
|
|
51
|
+
collectionHandle: null,
|
|
52
|
+
categorySlug: null,
|
|
53
|
+
categoryPath: null,
|
|
54
|
+
orderToken: null,
|
|
55
|
+
query: null,
|
|
56
|
+
page: 1,
|
|
57
|
+
sort: null,
|
|
58
|
+
columns: null,
|
|
59
|
+
filters: {},
|
|
60
|
+
setQuery: () => {},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function problem(status: number, body: unknown = {}): EldraHttpError {
|
|
65
|
+
return new EldraHttpError(new Response('{}', { status, statusText: `S${status}` }), body);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function setup(
|
|
69
|
+
answer: (request: EldraHttpRequest) => unknown,
|
|
70
|
+
options: Partial<Parameters<typeof createGatewayStorefront>[1]> = {}
|
|
71
|
+
) {
|
|
72
|
+
const requests: EldraHttpRequest[] = [];
|
|
73
|
+
const client = createEldraClient({
|
|
74
|
+
apiBaseUrl: 'https://api.example.test/api',
|
|
75
|
+
orgId: 'org-1',
|
|
76
|
+
httpClient: (async (request: EldraHttpRequest) => {
|
|
77
|
+
requests.push(request);
|
|
78
|
+
return answer(request);
|
|
79
|
+
}) as never,
|
|
80
|
+
});
|
|
81
|
+
const storefront = createGatewayStorefront(client, { route: route(), ...options });
|
|
82
|
+
return { storefront, requests };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
afterEach(() => {
|
|
86
|
+
vi.unstubAllGlobals();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
describe('pickLocalized', () => {
|
|
90
|
+
it('prefers the exact tag, then the same language, then the first entry', () => {
|
|
91
|
+
const map = { 'en-US': 'Name', 'is-IS': 'Nafn' };
|
|
92
|
+
expect(pickLocalized(map, 'is-IS')).toBe('Nafn');
|
|
93
|
+
expect(pickLocalized(map, 'is')).toBe('Nafn');
|
|
94
|
+
expect(pickLocalized({ 'en-GB': 'Colour' }, 'en-US')).toBe('Colour');
|
|
95
|
+
expect(pickLocalized(map, 'fr-FR')).toBe('Name');
|
|
96
|
+
expect(pickLocalized(map, undefined)).toBe('Name');
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('answers the empty string for nothing', () => {
|
|
100
|
+
expect(pickLocalized({}, 'en-US')).toBe('');
|
|
101
|
+
expect(pickLocalized(undefined, 'en-US')).toBe('');
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
describe('localizeFormDefinition', () => {
|
|
106
|
+
it('resolves every text for the locale and drops what the page does not need', () => {
|
|
107
|
+
const result = localizeFormDefinition(definition, 'is-IS');
|
|
108
|
+
|
|
109
|
+
expect(result.fields[0]).toEqual({
|
|
110
|
+
id: 'email',
|
|
111
|
+
type: 'email',
|
|
112
|
+
required: true,
|
|
113
|
+
label: 'Netfang',
|
|
114
|
+
placeholder: 'name@example.com',
|
|
115
|
+
});
|
|
116
|
+
expect(result.fields[1]?.options).toEqual([{ value: 'order', label: 'Pöntun' }]);
|
|
117
|
+
expect(result.fields[1]?.validation).toEqual({ maxLength: 40 });
|
|
118
|
+
expect(result).toMatchObject({
|
|
119
|
+
key: 'contact',
|
|
120
|
+
honeypotField: 'website',
|
|
121
|
+
turnstileSiteKey: '1x00000000000000000000AA',
|
|
122
|
+
successMessage: 'Takk.',
|
|
123
|
+
successRedirect: '/thanks',
|
|
124
|
+
});
|
|
125
|
+
expect(result).not.toHaveProperty('kinds');
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('resolves the consent sentence for the locale, and is empty without one', () => {
|
|
129
|
+
const subscribing = {
|
|
130
|
+
...definition,
|
|
131
|
+
consentText: { 'en-US': 'I want mail from Shop.', 'is-IS': 'Ég vil póst frá Shop.' },
|
|
132
|
+
};
|
|
133
|
+
expect(localizeFormDefinition(subscribing, 'is-IS').consentText).toBe('Ég vil póst frá Shop.');
|
|
134
|
+
expect(localizeFormDefinition(definition, 'is-IS').consentText).toBe('');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('refuses a field type the theme cannot render rather than showing it as text', () => {
|
|
138
|
+
const odd = {
|
|
139
|
+
...definition,
|
|
140
|
+
fields: [{ ...definition.fields[0], type: 'signature' }],
|
|
141
|
+
} as unknown as EldraFormDefinition;
|
|
142
|
+
expect(() => localizeFormDefinition(odd, 'en-US')).toThrow(/unsupported field type/);
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('carries phone and consent fields through', () => {
|
|
146
|
+
const result = localizeFormDefinition(
|
|
147
|
+
{
|
|
148
|
+
...definition,
|
|
149
|
+
fields: [
|
|
150
|
+
{ id: 'phone', type: 'phone', required: true, label: { 'en-US': 'Phone' } },
|
|
151
|
+
{ id: 'ok', type: 'consent', required: true, label: { 'en-US': 'I agree' } },
|
|
152
|
+
],
|
|
153
|
+
},
|
|
154
|
+
'en-US'
|
|
155
|
+
);
|
|
156
|
+
expect(result.fields.map((field) => field.type)).toEqual(['phone', 'consent']);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('carries number and url fields through', () => {
|
|
160
|
+
const result = localizeFormDefinition(
|
|
161
|
+
{
|
|
162
|
+
...definition,
|
|
163
|
+
fields: [
|
|
164
|
+
{ id: 'qty', type: 'number', required: true, label: { 'en-US': 'Quantity' } },
|
|
165
|
+
{ id: 'site', type: 'url', required: false, label: { 'en-US': 'Website' } },
|
|
166
|
+
],
|
|
167
|
+
},
|
|
168
|
+
'en-US'
|
|
169
|
+
);
|
|
170
|
+
expect(result.fields.map((field) => field.type)).toEqual(['number', 'url']);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
it('has no redirect when the platform names none', () => {
|
|
174
|
+
const { redirect: _drop, ...success } = definition.success;
|
|
175
|
+
expect(localizeFormDefinition({ ...definition, success }, 'en-US').successRedirect).toBeNull();
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('forms.definition', () => {
|
|
180
|
+
it('serves a baked definition with no request, resolved for the page locale', async () => {
|
|
181
|
+
const { storefront, requests } = setup(() => ({}), {
|
|
182
|
+
formDefinitions: { contact: definition },
|
|
183
|
+
formLocale: () => 'is-IS',
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const result = storefront.forms.definition(ref('contact'));
|
|
187
|
+
await vi.waitFor(() => expect(result.pending.value).toBe(false));
|
|
188
|
+
|
|
189
|
+
expect(requests).toHaveLength(0);
|
|
190
|
+
expect(result.data.value?.fields[0]?.label).toBe('Netfang');
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it('replaces the baked definition with the live one in a browser, and keeps it when the read fails', async () => {
|
|
194
|
+
vi.stubGlobal('window', {});
|
|
195
|
+
try {
|
|
196
|
+
const live = {
|
|
197
|
+
...definition,
|
|
198
|
+
turnstileSiteKey: 'live-key',
|
|
199
|
+
fields: [{ ...definition.fields[0], label: { 'en-US': 'Live email' } }],
|
|
200
|
+
};
|
|
201
|
+
const { storefront, requests } = setup(() => live, {
|
|
202
|
+
formDefinitions: { contact: definition },
|
|
203
|
+
});
|
|
204
|
+
const result = storefront.forms.definition(ref('contact'));
|
|
205
|
+
await vi.waitFor(() => expect(result.data.value?.fields[0]?.label).toBe('Live email'));
|
|
206
|
+
expect(requests).toHaveLength(1);
|
|
207
|
+
expect(result.data.value?.turnstileSiteKey).toBe('live-key');
|
|
208
|
+
|
|
209
|
+
const failing = setup(
|
|
210
|
+
() => {
|
|
211
|
+
throw problem(503);
|
|
212
|
+
},
|
|
213
|
+
{ formDefinitions: { contact: definition } }
|
|
214
|
+
);
|
|
215
|
+
const kept = failing.storefront.forms.definition(ref('contact'));
|
|
216
|
+
await vi.waitFor(() => expect(failing.requests).toHaveLength(1));
|
|
217
|
+
expect(kept.data.value?.fields[0]?.label).toBe('Email address');
|
|
218
|
+
} finally {
|
|
219
|
+
vi.unstubAllGlobals();
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it('fetches a form that was not baked', async () => {
|
|
224
|
+
const { storefront, requests } = setup(() => definition);
|
|
225
|
+
|
|
226
|
+
const result = storefront.forms.definition(ref('contact'));
|
|
227
|
+
await vi.waitFor(() => expect(result.pending.value).toBe(false));
|
|
228
|
+
|
|
229
|
+
expect(requests.map((request) => request.url)).toEqual([
|
|
230
|
+
'https://api.example.test/api/site/v1/forms/contact',
|
|
231
|
+
]);
|
|
232
|
+
expect(result.data.value?.fields[0]?.label).toBe('Email address');
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('leaves data null, with an error, for a form the platform does not know', async () => {
|
|
236
|
+
const { storefront } = setup(() => {
|
|
237
|
+
throw problem(404);
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
const result = storefront.forms.definition(ref('nope'));
|
|
241
|
+
await vi.waitFor(() => expect(result.pending.value).toBe(false));
|
|
242
|
+
|
|
243
|
+
expect(result.data.value).toBeNull();
|
|
244
|
+
expect(result.error.value).not.toBeNull();
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
describe('forms.submit', () => {
|
|
249
|
+
const input = { values: { email: 'anna@example.is' } };
|
|
250
|
+
|
|
251
|
+
it('posts the values with the locale and page path, and omits an empty honeypot and token', async () => {
|
|
252
|
+
const { storefront, requests } = setup(() => ({ ok: true }), { formLocale: () => 'is-IS' });
|
|
253
|
+
|
|
254
|
+
const result = await storefront.forms.submit('contact', { ...input, hp: '' });
|
|
255
|
+
|
|
256
|
+
expect(result).toEqual({ ok: true, pending: false });
|
|
257
|
+
expect(requests[0]?.url).toBe('https://api.example.test/api/site/v1/forms/contact/submissions');
|
|
258
|
+
expect(JSON.parse(requests[0]?.body as string)).toEqual({
|
|
259
|
+
values: { email: 'anna@example.is' },
|
|
260
|
+
locale: 'is-IS',
|
|
261
|
+
pagePath: '/',
|
|
262
|
+
});
|
|
263
|
+
});
|
|
264
|
+
|
|
265
|
+
it('sends the Turnstile token and what a bot typed into the honeypot', async () => {
|
|
266
|
+
const { storefront, requests } = setup(() => ({ ok: true }));
|
|
267
|
+
|
|
268
|
+
await storefront.forms.submit('contact', { ...input, turnstileToken: 'tok', hp: 'spam' });
|
|
269
|
+
|
|
270
|
+
expect(JSON.parse(requests[0]?.body as string)).toMatchObject({
|
|
271
|
+
turnstileToken: 'tok',
|
|
272
|
+
hp: 'spam',
|
|
273
|
+
locale: 'en-US',
|
|
274
|
+
});
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('reports a pending double opt-in', async () => {
|
|
278
|
+
const { storefront } = setup(() => ({ ok: true, pending: true }));
|
|
279
|
+
|
|
280
|
+
expect(await storefront.forms.submit('newsletter', input)).toEqual({ ok: true, pending: true });
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
it('keeps a 422’s field-keyed messages', async () => {
|
|
284
|
+
const { storefront } = setup(() => {
|
|
285
|
+
throw problem(422, { errors: { email: ['Not an address.'] }, form: ['Check it.'] });
|
|
286
|
+
});
|
|
287
|
+
|
|
288
|
+
expect(await storefront.forms.submit('contact', input)).toEqual({
|
|
289
|
+
ok: false,
|
|
290
|
+
reason: 'invalid',
|
|
291
|
+
errors: { email: ['Not an address.'] },
|
|
292
|
+
form: ['Check it.'],
|
|
293
|
+
});
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
it.each([
|
|
297
|
+
[429, 'rate-limited'],
|
|
298
|
+
[413, 'too-large'],
|
|
299
|
+
[500, 'unavailable'],
|
|
300
|
+
[404, 'unavailable'],
|
|
301
|
+
] as const)('reads a %i as %s', async (status, reason) => {
|
|
302
|
+
const { storefront } = setup(() => {
|
|
303
|
+
throw problem(status);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
expect(await storefront.forms.submit('contact', input)).toEqual({ ok: false, reason });
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
it('reads an unreachable platform as unavailable, never a throw', async () => {
|
|
310
|
+
const { storefront } = setup(() => {
|
|
311
|
+
throw new TypeError('fetch failed');
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
expect(await storefront.forms.submit('contact', input)).toEqual({
|
|
315
|
+
ok: false,
|
|
316
|
+
reason: 'unavailable',
|
|
317
|
+
});
|
|
318
|
+
});
|
|
319
|
+
|
|
320
|
+
describe('with a formsEndpoint override', () => {
|
|
321
|
+
it('posts there instead of to the platform, and a 2xx is a success', async () => {
|
|
322
|
+
const fetchMock = vi.fn(async () => new Response('{}', { status: 200 }));
|
|
323
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
324
|
+
const { storefront, requests } = setup(() => ({ ok: true }), {
|
|
325
|
+
formsEndpoint: 'https://forms.example.test/submit',
|
|
326
|
+
formLocale: () => 'is-IS',
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
const result = await storefront.forms.submit('contact', input);
|
|
330
|
+
|
|
331
|
+
expect(result).toEqual({ ok: true, pending: false });
|
|
332
|
+
expect(requests).toHaveLength(0);
|
|
333
|
+
expect(fetchMock).toHaveBeenCalledOnce();
|
|
334
|
+
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
|
|
335
|
+
expect(url).toBe('https://forms.example.test/submit');
|
|
336
|
+
expect(JSON.parse(init.body as string)).toEqual({
|
|
337
|
+
kind: 'form',
|
|
338
|
+
key: 'contact',
|
|
339
|
+
values: { email: 'anna@example.is' },
|
|
340
|
+
locale: 'is-IS',
|
|
341
|
+
pagePath: '/',
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('forwards the honeypot and the Turnstile token to the endpoint', async () => {
|
|
346
|
+
const fetchMock = vi.fn(async () => new Response('{}', { status: 200 }));
|
|
347
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
348
|
+
const { storefront } = setup(() => ({ ok: true }), {
|
|
349
|
+
formsEndpoint: 'https://forms.example.test/submit',
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
await storefront.forms.submit('contact', { ...input, hp: 'bot', turnstileToken: 'tok' });
|
|
353
|
+
|
|
354
|
+
const [, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
|
|
355
|
+
expect(JSON.parse(init.body as string)).toMatchObject({ hp: 'bot', turnstileToken: 'tok' });
|
|
356
|
+
});
|
|
357
|
+
|
|
358
|
+
it.each([
|
|
359
|
+
[429, 'rate-limited'],
|
|
360
|
+
[413, 'too-large'],
|
|
361
|
+
[502, 'unavailable'],
|
|
362
|
+
] as const)('reads the endpoint’s %i as %s', async (status, reason) => {
|
|
363
|
+
vi.stubGlobal(
|
|
364
|
+
'fetch',
|
|
365
|
+
vi.fn(async () => new Response('{}', { status }))
|
|
366
|
+
);
|
|
367
|
+
const { storefront } = setup(() => ({}), { formsEndpoint: 'https://forms.example.test/s' });
|
|
368
|
+
|
|
369
|
+
expect(await storefront.forms.submit('contact', input)).toEqual({ ok: false, reason });
|
|
370
|
+
});
|
|
371
|
+
|
|
372
|
+
it('reads a network failure as unavailable', async () => {
|
|
373
|
+
vi.stubGlobal(
|
|
374
|
+
'fetch',
|
|
375
|
+
vi.fn(async () => Promise.reject(new TypeError('offline')))
|
|
376
|
+
);
|
|
377
|
+
const { storefront } = setup(() => ({}), { formsEndpoint: 'https://forms.example.test/s' });
|
|
378
|
+
|
|
379
|
+
expect(await storefront.forms.submit('contact', input)).toEqual({
|
|
380
|
+
ok: false,
|
|
381
|
+
reason: 'unavailable',
|
|
382
|
+
});
|
|
383
|
+
});
|
|
384
|
+
});
|
|
385
|
+
});
|
|
@@ -27,7 +27,7 @@ describe('formatMoney', () => {
|
|
|
27
27
|
expect(formatMoney(28, 'ISK', 'is-IS')).toContain('28');
|
|
28
28
|
// Not even for an amount that has a fraction: krónur have no minor unit to show one in, and
|
|
29
29
|
// so nothing to pad to either.
|
|
30
|
-
expect(formatMoney(28.4, 'ISK', 'en-US')).toBe('
|
|
30
|
+
expect(formatMoney(28.4, 'ISK', 'en-US')).toBe('ISK\u00a028');
|
|
31
31
|
expect(formatMoney(28, 'USD', 'en-US')).toBe('$28.00');
|
|
32
32
|
expect(formatMoney(28.5, 'USD', 'en-US')).toBe('$28.50');
|
|
33
33
|
expect(formatMoney(28.567, 'USD', 'en-US')).toBe('$28.57');
|
|
@@ -48,16 +48,16 @@ describe('formatMoney', () => {
|
|
|
48
48
|
* read "ISK 2,800" beside a `<Price>` reading "kr 2,800" for the same money. Both now go through
|
|
49
49
|
* `@eldrajs/ui`'s `formatCurrency`, so there is one sign on the page.
|
|
50
50
|
*/
|
|
51
|
-
it('writes the currency’s
|
|
52
|
-
expect(formatMoney(2800, 'ISK', 'en-US')).toBe('
|
|
51
|
+
it('writes the currency’s wide sign, the same one every <Price> writes', () => {
|
|
52
|
+
expect(formatMoney(2800, 'ISK', 'en-US')).toBe('ISK\u00a02,800');
|
|
53
53
|
expect(formatMoney(2800, 'ISK', 'is-IS')).toBe('2.800\u00a0kr.');
|
|
54
54
|
expect(formatMoney(28, 'USD', 'en-US')).toBe('$28.00');
|
|
55
|
-
// `is-IS`
|
|
56
|
-
expect(formatMoney(28, 'USD', 'is-IS')).toBe('28,00\
|
|
55
|
+
// `is-IS`'s wide sign for dollars is the code itself.
|
|
56
|
+
expect(formatMoney(28, 'USD', 'is-IS')).toBe('28,00\u00a0USD');
|
|
57
57
|
});
|
|
58
58
|
|
|
59
59
|
it('is `@eldrajs/ui`’s own `formatCurrency`, not a second copy of it', () => {
|
|
60
|
-
// Asserted as an identity across every usable pair the theme can see — with the same
|
|
60
|
+
// Asserted as an identity across every usable pair the theme can see — with the same wide
|
|
61
61
|
// sign and the same fraction arguments this module passes — so the day the package's
|
|
62
62
|
// formatter changes, this module follows it instead of drifting from it.
|
|
63
63
|
for (const [currency, digits] of [
|
|
@@ -67,7 +67,7 @@ describe('formatMoney', () => {
|
|
|
67
67
|
] as const) {
|
|
68
68
|
for (const locale of ['en-US', 'is-IS']) {
|
|
69
69
|
expect(formatMoney(2800.5, currency, locale)).toBe(
|
|
70
|
-
formatCurrency(2800.5, locale, currency,
|
|
70
|
+
formatCurrency(2800.5, locale, currency, false, digits, digits)
|
|
71
71
|
);
|
|
72
72
|
}
|
|
73
73
|
}
|
|
@@ -135,13 +135,12 @@ describe('toMinorUnits', () => {
|
|
|
135
135
|
});
|
|
136
136
|
|
|
137
137
|
describe('currencyLabel', () => {
|
|
138
|
-
it('names the currency as code plus its
|
|
139
|
-
//
|
|
140
|
-
// English one (the narrow sign for krónur — the wide one there *is* the code).
|
|
138
|
+
it('names the currency as code plus the sign its prices carry, in the given locale', () => {
|
|
139
|
+
// English krónur and Icelandic dollars are written with the code itself, so the label is the code.
|
|
141
140
|
expect(currencyLabel('ISK', 'is-IS')).toBe('ISK kr.');
|
|
142
|
-
expect(currencyLabel('ISK', 'en-US')).toBe('ISK
|
|
141
|
+
expect(currencyLabel('ISK', 'en-US')).toBe('ISK');
|
|
143
142
|
expect(currencyLabel('USD', 'en-US')).toBe('USD $');
|
|
144
|
-
expect(currencyLabel('USD', 'is-IS')).toBe('USD
|
|
143
|
+
expect(currencyLabel('USD', 'is-IS')).toBe('USD');
|
|
145
144
|
expect(currencyLabel('EUR', 'en-US')).toBe('EUR €');
|
|
146
145
|
});
|
|
147
146
|
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest';
|
|
2
|
+
import { formatMoney } from '../../app/storefront/money';
|
|
3
|
+
import { pageStructuredData, type PageLdParts } from '../../app/storefront/structuredData';
|
|
4
|
+
import type { StorefrontProduct } from '../../app/storefront/types';
|
|
5
|
+
|
|
6
|
+
const ORIGIN = 'https://shop.test';
|
|
7
|
+
|
|
8
|
+
const product = (over: Partial<StorefrontProduct> = {}): StorefrontProduct => ({
|
|
9
|
+
handle: 'mug',
|
|
10
|
+
title: 'Mug',
|
|
11
|
+
url: '/products/mug',
|
|
12
|
+
featuredImage: null,
|
|
13
|
+
images: [{ src: 'https://cdn.test/mug.jpg', alt: 'Mug' }],
|
|
14
|
+
price: { amount: 2800, compareAt: null },
|
|
15
|
+
stock: 'in',
|
|
16
|
+
available: true,
|
|
17
|
+
productId: 'p1',
|
|
18
|
+
variantId: 'v1',
|
|
19
|
+
options: [],
|
|
20
|
+
categoryTrail: [],
|
|
21
|
+
description: 'A <b>hand-thrown</b> mug.',
|
|
22
|
+
inventory: null,
|
|
23
|
+
...over,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const parts = (over: Partial<PageLdParts> = {}): PageLdParts => ({
|
|
27
|
+
kind: 'page',
|
|
28
|
+
title: 'About',
|
|
29
|
+
currentUrl: `${ORIGIN}/about`,
|
|
30
|
+
url: (path) => `${ORIGIN}${path}`,
|
|
31
|
+
absolute: true,
|
|
32
|
+
homeLabel: 'Home',
|
|
33
|
+
trail: [],
|
|
34
|
+
product: null,
|
|
35
|
+
currency: 'ISK',
|
|
36
|
+
items: [],
|
|
37
|
+
profile: {
|
|
38
|
+
name: 'Shop',
|
|
39
|
+
logoUrl: 'https://media.test/icon-512',
|
|
40
|
+
sameAs: [],
|
|
41
|
+
contact: { email: null, telephone: null },
|
|
42
|
+
},
|
|
43
|
+
searchPath: '/search',
|
|
44
|
+
...over,
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
const types = (entries: ReturnType<typeof pageStructuredData>) =>
|
|
48
|
+
entries.map((entry) => entry.type);
|
|
49
|
+
|
|
50
|
+
describe('pageStructuredData', () => {
|
|
51
|
+
it('writes Organization and WebSite on home, and only with an absolute origin', () => {
|
|
52
|
+
expect(types(pageStructuredData(parts({ kind: 'home' })))).toEqual(['organization', 'website']);
|
|
53
|
+
expect(types(pageStructuredData(parts({ kind: 'home', absolute: false })))).toEqual([]);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('writes only a BreadcrumbList on an ordinary page, Home first', () => {
|
|
57
|
+
const [entry, ...rest] = pageStructuredData(parts());
|
|
58
|
+
expect(rest).toEqual([]);
|
|
59
|
+
expect(entry!.type).toBe('breadcrumb');
|
|
60
|
+
expect(entry!.node.itemListElement).toMatchObject([
|
|
61
|
+
{ name: 'Home', item: `${ORIGIN}/` },
|
|
62
|
+
{ name: 'About', item: `${ORIGIN}/about` },
|
|
63
|
+
]);
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it('writes nothing for the not-found shell', () => {
|
|
67
|
+
expect(pageStructuredData(parts({ kind: 'notFound' }))).toEqual([]);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('builds a product from the same price, currency and stock the page renders', () => {
|
|
71
|
+
const entries = pageStructuredData(
|
|
72
|
+
parts({
|
|
73
|
+
kind: 'product',
|
|
74
|
+
title: 'Mug',
|
|
75
|
+
currentUrl: `${ORIGIN}/products/mug`,
|
|
76
|
+
trail: [{ label: 'Cups', path: '/categories/cups' }],
|
|
77
|
+
product: product({ brand: 'Acme' }),
|
|
78
|
+
})
|
|
79
|
+
);
|
|
80
|
+
expect(types(entries)).toEqual(['breadcrumb', 'product']);
|
|
81
|
+
expect(
|
|
82
|
+
(entries[0]!.node.itemListElement as Array<{ name: string }>).map((item) => item.name)
|
|
83
|
+
).toEqual(['Home', 'Cups', 'Mug']);
|
|
84
|
+
const node = entries[1]!.node as Record<string, any>;
|
|
85
|
+
expect(node).toMatchObject({
|
|
86
|
+
name: 'Mug',
|
|
87
|
+
description: 'A hand-thrown mug.',
|
|
88
|
+
image: ['https://cdn.test/mug.jpg'],
|
|
89
|
+
brand: { name: 'Acme' },
|
|
90
|
+
offers: { price: 2800, priceCurrency: 'ISK', availability: 'https://schema.org/InStock' },
|
|
91
|
+
});
|
|
92
|
+
// The number the markup states is the number the visible price is formatted from.
|
|
93
|
+
expect(formatMoney(node.offers.price, 'ISK', 'en-US')).toContain('2,800');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('maps stock states to availability and falls back to the shop name as brand', () => {
|
|
97
|
+
const availability = (stock: StorefrontProduct['stock']) =>
|
|
98
|
+
(
|
|
99
|
+
pageStructuredData(parts({ kind: 'product', product: product({ stock }) }))[1]!.node
|
|
100
|
+
.offers as Record<string, unknown>
|
|
101
|
+
).availability;
|
|
102
|
+
expect(availability('out')).toBe('https://schema.org/OutOfStock');
|
|
103
|
+
expect(availability('preorder')).toBe('https://schema.org/PreOrder');
|
|
104
|
+
expect(availability('low')).toBe('https://schema.org/LimitedAvailability');
|
|
105
|
+
expect(
|
|
106
|
+
pageStructuredData(parts({ kind: 'product', product: product() }))[1]!.node.brand
|
|
107
|
+
).toEqual({ '@type': 'Brand', name: 'Shop' });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('writes one offer per variant, AggregateOffer when prices differ, and drops a bad barcode', () => {
|
|
111
|
+
const entries = pageStructuredData(
|
|
112
|
+
parts({
|
|
113
|
+
kind: 'product',
|
|
114
|
+
product: product({
|
|
115
|
+
variants: [
|
|
116
|
+
{ sku: 'S', barcode: '4006381333931', price: 2000, compareAt: null, stock: 'in' },
|
|
117
|
+
{ sku: 'L', barcode: 'internal', price: 3000, compareAt: 3500, stock: 'out' },
|
|
118
|
+
],
|
|
119
|
+
}),
|
|
120
|
+
})
|
|
121
|
+
);
|
|
122
|
+
const offers = entries[1]!.node.offers as Record<string, any>;
|
|
123
|
+
expect(offers).toMatchObject({ '@type': 'AggregateOffer', lowPrice: 2000, highPrice: 3000 });
|
|
124
|
+
expect(offers.offers[0].gtin).toBe('4006381333931');
|
|
125
|
+
expect(offers.offers[1].gtin).toBeUndefined();
|
|
126
|
+
expect(offers.offers[1].availability).toBe('https://schema.org/OutOfStock');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('writes an ItemList on a listing page only when it shows products', () => {
|
|
130
|
+
const listing = (items: PageLdParts['items']) =>
|
|
131
|
+
types(pageStructuredData(parts({ kind: 'collection', items })));
|
|
132
|
+
expect(listing([])).toEqual(['breadcrumb']);
|
|
133
|
+
expect(listing([{ title: 'Mug', url: `${ORIGIN}/products/mug` }])).toEqual([
|
|
134
|
+
'breadcrumb',
|
|
135
|
+
'itemList',
|
|
136
|
+
]);
|
|
137
|
+
});
|
|
138
|
+
});
|