@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
|
@@ -40,7 +40,13 @@
|
|
|
40
40
|
* (the three `watch`es), which is what lets a corrected field's `aria-invalid` go false in time for
|
|
41
41
|
* that next submit to get past `FormLayout`'s own scan.
|
|
42
42
|
*
|
|
43
|
-
* **
|
|
43
|
+
* **Submitting** is the platform's own form, `forms.submit('contact', { values })`: the seeded
|
|
44
|
+
* `contact` form (`app/formSeeds.ts`) names the fields this block sends (`name`, `email`, `topic`,
|
|
45
|
+
* `orderNumber`, `message`), and who is notified is the merchant's setting on the Forms page, not
|
|
46
|
+
* block data. A `422` marks the field it names (the same three error refs a local check sets);
|
|
47
|
+
* every other failure shows the server summary.
|
|
48
|
+
*
|
|
49
|
+
* **Success.** `forms.submit` resolving `{ ok: true }` swaps the card's content for a
|
|
44
50
|
* `role="status" tabindex="-1"` panel that receives focus (spec: "the card content is replaced").
|
|
45
51
|
* "Send another message" restores an empty form and focuses the Name field (spec: "moves focus to
|
|
46
52
|
* its first field").
|
|
@@ -64,10 +70,12 @@ import {
|
|
|
64
70
|
} from '@eldrajs/ui';
|
|
65
71
|
import { useBlockData } from '../../app/composables/useBlockData';
|
|
66
72
|
import { useEditing } from '../../app/composables/useEditing';
|
|
73
|
+
import { useFormSpam } from '../../app/composables/useFormSpam';
|
|
67
74
|
import { useStorefront } from '../../app/composables/useStorefront';
|
|
68
75
|
import { useI18n } from 'vue-i18n';
|
|
69
76
|
import { useUiId } from '../../app/composables/useUiId';
|
|
70
77
|
import EldraIcon from '../../app/components/EldraIcon.vue';
|
|
78
|
+
import FormHoneypot from '../../app/components/FormHoneypot.vue';
|
|
71
79
|
import EldraRouterLink from '../../app/components/EldraRouterLink.vue';
|
|
72
80
|
import UiImage from '../../app/components/ui/UiImage.vue';
|
|
73
81
|
import { isInternalHref, safeHref } from '../../app/utils/links';
|
|
@@ -272,11 +280,13 @@ watch(message, () => {
|
|
|
272
280
|
if (messageError.value) messageError.value = null;
|
|
273
281
|
});
|
|
274
282
|
|
|
283
|
+
const spam = useFormSpam(computed(() => (hasForm.value ? 'contact' : '')));
|
|
284
|
+
const { honeypot, honeypotField, siteKey, turnstileRoot } = spam;
|
|
275
285
|
const sending = ref(false);
|
|
276
286
|
const serverError = ref(false);
|
|
287
|
+
/** The spam check (Turnstile) is on and the visitor has not passed it. */
|
|
288
|
+
const challengeError = ref<string | null>(null);
|
|
277
289
|
const sent = ref(false);
|
|
278
|
-
const sentFirstName = ref('');
|
|
279
|
-
const sentEmail = ref('');
|
|
280
290
|
|
|
281
291
|
const summaryRef = ref<HTMLElement | null>(null);
|
|
282
292
|
const statusRef = ref<HTMLElement | null>(null);
|
|
@@ -284,7 +294,9 @@ const statusRef = ref<HTMLElement | null>(null);
|
|
|
284
294
|
const invalidCount = computed(
|
|
285
295
|
() => [nameError.value, emailError.value, messageError.value].filter(Boolean).length
|
|
286
296
|
);
|
|
287
|
-
const hasFormError = computed(
|
|
297
|
+
const hasFormError = computed(
|
|
298
|
+
() => invalidCount.value > 0 || serverError.value || challengeError.value !== null
|
|
299
|
+
);
|
|
288
300
|
const summaryTitle = computed(() =>
|
|
289
301
|
invalidCount.value === 1
|
|
290
302
|
? t('contact.summaryTitleOne')
|
|
@@ -311,10 +323,14 @@ function focusField(id: string): void {
|
|
|
311
323
|
|
|
312
324
|
async function sendForm(): Promise<void> {
|
|
313
325
|
serverError.value = false;
|
|
326
|
+
challengeError.value = spam.challenge();
|
|
327
|
+
if (challengeError.value !== null) {
|
|
328
|
+
await focusSummary();
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
314
331
|
sending.value = true;
|
|
315
332
|
|
|
316
333
|
const payload: Record<string, string> = {
|
|
317
|
-
recipient: data.value.recipient,
|
|
318
334
|
name: name.value.trim(),
|
|
319
335
|
email: emailValue.value.trim(),
|
|
320
336
|
message: message.value.trim(),
|
|
@@ -324,17 +340,25 @@ async function sendForm(): Promise<void> {
|
|
|
324
340
|
}
|
|
325
341
|
if (hasTopics.value && topic.value !== '') payload.topic = topic.value;
|
|
326
342
|
|
|
327
|
-
const result = await storefront.forms.
|
|
343
|
+
const result = await storefront.forms.submit('contact', { values: payload, ...spam.signals() });
|
|
328
344
|
sending.value = false;
|
|
345
|
+
// A Turnstile token is single-use, whatever the outcome.
|
|
346
|
+
spam.reset();
|
|
329
347
|
|
|
330
348
|
if (!result.ok) {
|
|
331
|
-
|
|
349
|
+
if (result.reason === 'invalid') {
|
|
350
|
+
// The platform's own message for each field it refused, shown where a local check's would be.
|
|
351
|
+
nameError.value = result.errors.name?.[0] ?? null;
|
|
352
|
+
emailError.value = result.errors.email?.[0] ?? null;
|
|
353
|
+
messageError.value = result.errors.message?.[0] ?? null;
|
|
354
|
+
}
|
|
355
|
+
// A refusal that named none of the three fields (another field, the form as a whole, or no
|
|
356
|
+
// reason at all) would otherwise leave nothing on screen.
|
|
357
|
+
serverError.value = !nameError.value && !emailError.value && !messageError.value;
|
|
332
358
|
await focusSummary();
|
|
333
359
|
return;
|
|
334
360
|
}
|
|
335
361
|
|
|
336
|
-
sentFirstName.value = name.value.trim().split(/\s+/)[0] ?? '';
|
|
337
|
-
sentEmail.value = emailValue.value.trim();
|
|
338
362
|
sent.value = true;
|
|
339
363
|
await nextTick();
|
|
340
364
|
statusRef.value?.focus();
|
|
@@ -365,20 +389,15 @@ async function onSendAnother(): Promise<void> {
|
|
|
365
389
|
emailError.value = null;
|
|
366
390
|
messageError.value = null;
|
|
367
391
|
serverError.value = false;
|
|
392
|
+
challengeError.value = null;
|
|
393
|
+
honeypot.value = '';
|
|
368
394
|
sent.value = false;
|
|
369
395
|
await nextTick();
|
|
370
396
|
focusField(nameFieldId);
|
|
371
397
|
}
|
|
372
398
|
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
Object.hasOwn(params, key) ? params[key]! : match
|
|
376
|
-
);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
const successText = computed(() =>
|
|
380
|
-
interpolate(data.value.successText || t('contact.successTextDefault'), { email: sentEmail.value })
|
|
381
|
-
);
|
|
399
|
+
/** The receipt never echoes what the visitor typed (no name, no address): it is shown to whoever is at the screen. */
|
|
400
|
+
const successText = computed(() => data.value.successText || t('contact.successTextDefault'));
|
|
382
401
|
</script>
|
|
383
402
|
|
|
384
403
|
<template>
|
|
@@ -468,8 +487,15 @@ const successText = computed(() =>
|
|
|
468
487
|
>
|
|
469
488
|
<template v-if="hasFormError" #errorSummary>
|
|
470
489
|
<div ref="summaryRef" tabindex="-1">
|
|
471
|
-
<
|
|
472
|
-
|
|
490
|
+
<p v-if="challengeError">{{ challengeError }}</p>
|
|
491
|
+
<template v-else-if="serverError">
|
|
492
|
+
<p>
|
|
493
|
+
{{
|
|
494
|
+
email
|
|
495
|
+
? t('contact.serverError', { email })
|
|
496
|
+
: t('contact.serverErrorGeneric')
|
|
497
|
+
}}
|
|
498
|
+
</p>
|
|
473
499
|
</template>
|
|
474
500
|
<template v-else>
|
|
475
501
|
<p class="font-semibold">{{ summaryTitle }}</p>
|
|
@@ -553,6 +579,9 @@ const successText = computed(() =>
|
|
|
553
579
|
/>
|
|
554
580
|
</FieldWrapper>
|
|
555
581
|
|
|
582
|
+
<FormHoneypot v-model="honeypot" :name="honeypotField" />
|
|
583
|
+
<div v-if="siteKey" ref="turnstileRoot" class="@two-col:col-span-2" />
|
|
584
|
+
|
|
556
585
|
<template #actions>
|
|
557
586
|
<Button variant="primary" type="submit" size="lg" :label="t('contact.sending')">{{
|
|
558
587
|
t('contact.sendMessage')
|
|
@@ -564,7 +593,7 @@ const successText = computed(() =>
|
|
|
564
593
|
<div v-else ref="statusRef" role="status" tabindex="-1" class="grid gap-4 text-center">
|
|
565
594
|
<EldraIcon name="circle-check" size="xl" class="text-success mx-auto" />
|
|
566
595
|
<p class="text-h4 font-heading font-semibold">
|
|
567
|
-
{{ t('contact.successTitle'
|
|
596
|
+
{{ t('contact.successTitle') }}
|
|
568
597
|
</p>
|
|
569
598
|
<p class="text-muted text-base">{{ successText }}</p>
|
|
570
599
|
<Button variant="outline" type="button" class="mx-auto" @click="onSendAnother">{{
|
|
@@ -1,19 +1,34 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { flushPromises, mount } from '@vue/test-utils';
|
|
3
|
-
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
import { axe } from '../../../test/support/axe';
|
|
5
5
|
import Block from '../Block.vue';
|
|
6
6
|
import mock from '../mock.json';
|
|
7
7
|
import preview from '../preview.json';
|
|
8
8
|
import { mountOptions } from '../../../test/support/mountBlock';
|
|
9
9
|
import { STOREFRONT_KEY } from '../../../app/storefront/types';
|
|
10
|
+
import type { StorefrontSource } from '../../../app/storefront/types';
|
|
10
11
|
import { createDemoStorefront } from '../../../app/storefront/demo';
|
|
11
12
|
import enUS from '../../../i18n/en-US.json';
|
|
12
13
|
import { renderedMessage } from '../../../test/support/i18nText';
|
|
14
|
+
import {
|
|
15
|
+
cleanUpTurnstile,
|
|
16
|
+
storefrontWithTurnstile,
|
|
17
|
+
stubTurnstile,
|
|
18
|
+
} from '../../../test/support/turnstileForms';
|
|
19
|
+
|
|
20
|
+
afterEach(cleanUpTurnstile);
|
|
13
21
|
|
|
14
22
|
const merged = { ...mock, ...preview };
|
|
15
23
|
|
|
16
|
-
function mountContact(
|
|
24
|
+
function mountContact(
|
|
25
|
+
data: Record<string, unknown>,
|
|
26
|
+
options: {
|
|
27
|
+
failForms?: boolean;
|
|
28
|
+
submit?: StorefrontSource['forms']['submit'];
|
|
29
|
+
storefront?: StorefrontSource;
|
|
30
|
+
} = {}
|
|
31
|
+
) {
|
|
17
32
|
const base = mountOptions({ entry: { id: 'e1', data } });
|
|
18
33
|
return mount(Block, {
|
|
19
34
|
...base,
|
|
@@ -28,6 +43,15 @@ function mountContact(data: Record<string, unknown>, options: { failForms?: bool
|
|
|
28
43
|
...(options.failForms
|
|
29
44
|
? { [STOREFRONT_KEY]: createDemoStorefront({ failForms: true }) }
|
|
30
45
|
: {}),
|
|
46
|
+
...(options.submit
|
|
47
|
+
? {
|
|
48
|
+
[STOREFRONT_KEY]: (() => {
|
|
49
|
+
const base = createDemoStorefront();
|
|
50
|
+
return { ...base, forms: { ...base.forms, submit: options.submit } };
|
|
51
|
+
})(),
|
|
52
|
+
}
|
|
53
|
+
: {}),
|
|
54
|
+
...(options.storefront ? { [STOREFRONT_KEY]: options.storefront } : {}),
|
|
31
55
|
},
|
|
32
56
|
},
|
|
33
57
|
});
|
|
@@ -177,18 +201,50 @@ describe('contact block', () => {
|
|
|
177
201
|
expect(document.activeElement).toBe(messageField.element);
|
|
178
202
|
});
|
|
179
203
|
|
|
180
|
-
it('a valid submit
|
|
181
|
-
const
|
|
204
|
+
it('a valid submit posts the seeded contact form’s fields to forms.submit and shows a role="status" success panel that receives focus, replacing the form', async () => {
|
|
205
|
+
const submit = vi.fn(async () => ({ ok: true as const, pending: false }));
|
|
206
|
+
const wrapper = mountContact(mock, { submit });
|
|
182
207
|
await submitValid(wrapper);
|
|
183
208
|
|
|
209
|
+
expect(submit).toHaveBeenCalledWith('contact', {
|
|
210
|
+
values: {
|
|
211
|
+
name: 'Hannah Reyes',
|
|
212
|
+
email: 'hannah@example.com',
|
|
213
|
+
topic: mock.topics[0]!.label,
|
|
214
|
+
message: 'Do you carry the moss glaze in stock right now?',
|
|
215
|
+
},
|
|
216
|
+
});
|
|
184
217
|
expect(wrapper.find('form').exists()).toBe(false);
|
|
185
218
|
const status = wrapper.find('[role="status"]');
|
|
186
219
|
expect(status.exists()).toBe(true);
|
|
187
|
-
|
|
188
|
-
expect(status.text()).toContain('
|
|
220
|
+
// A receipt never echoes what the visitor typed.
|
|
221
|
+
expect(status.text()).not.toContain('Hannah');
|
|
222
|
+
expect(status.text()).not.toContain('hannah@example.com');
|
|
189
223
|
expect(document.activeElement).toBe(status.element);
|
|
190
224
|
});
|
|
191
225
|
|
|
226
|
+
it('with Turnstile on, renders the widget and honeypot, refuses to send before the check, then sends the token and honeypot', async () => {
|
|
227
|
+
const turnstile = stubTurnstile();
|
|
228
|
+
const submit = vi.fn(async () => ({ ok: true as const, pending: false }));
|
|
229
|
+
const wrapper = mountContact(mock, { storefront: storefrontWithTurnstile(submit) });
|
|
230
|
+
await flushPromises();
|
|
231
|
+
expect(turnstile.render).toHaveBeenCalledOnce();
|
|
232
|
+
|
|
233
|
+
await submitValid(wrapper);
|
|
234
|
+
expect(submit).not.toHaveBeenCalled();
|
|
235
|
+
expect(wrapper.text()).toContain(enUS.form.turnstileRequired);
|
|
236
|
+
|
|
237
|
+
turnstile.solve('token-1');
|
|
238
|
+
await wrapper.find('input[name="website"]').setValue('bot');
|
|
239
|
+
await wrapper.find('form').trigger('submit');
|
|
240
|
+
await flushPromises();
|
|
241
|
+
expect(submit).toHaveBeenCalledWith(
|
|
242
|
+
'contact',
|
|
243
|
+
expect.objectContaining({ turnstileToken: 'token-1', hp: 'bot' })
|
|
244
|
+
);
|
|
245
|
+
expect(turnstile.reset).toHaveBeenCalled();
|
|
246
|
+
});
|
|
247
|
+
|
|
192
248
|
it('"Send another message" restores an empty form and focuses the Name field', async () => {
|
|
193
249
|
const wrapper = mountContact(mock);
|
|
194
250
|
await submitValid(wrapper);
|
|
@@ -206,12 +262,37 @@ describe('contact block', () => {
|
|
|
206
262
|
expect(document.activeElement).toBe(name.element);
|
|
207
263
|
});
|
|
208
264
|
|
|
209
|
-
it('a server failure (forms.
|
|
265
|
+
it('a server failure (forms.submit ok:false) shows the summary again and keeps the form', async () => {
|
|
210
266
|
const wrapper = mountContact(mock, { failForms: true });
|
|
211
267
|
await submitValid(wrapper);
|
|
212
268
|
|
|
213
269
|
expect(wrapper.find('form').exists()).toBe(true);
|
|
214
|
-
expect(wrapper.find('[role="alert"]').text()).toContain(mock.
|
|
270
|
+
expect(wrapper.find('[role="alert"]').text()).toContain(mock.email);
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('a server failure with no contact email on the block still says what happened', async () => {
|
|
274
|
+
const { email: _omit, ...withoutEmail } = mock;
|
|
275
|
+
const wrapper = mountContact(withoutEmail, { failForms: true });
|
|
276
|
+
await submitValid(wrapper);
|
|
277
|
+
|
|
278
|
+
expect(wrapper.find('[role="alert"]').text()).toBe(enUS.contact.serverErrorGeneric);
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
it('a 422 marks the field the platform names, with its own message, and focuses the summary', async () => {
|
|
282
|
+
const submit = vi.fn(async () => ({
|
|
283
|
+
ok: false as const,
|
|
284
|
+
reason: 'invalid' as const,
|
|
285
|
+
errors: { email: ['That domain does not accept mail.'] },
|
|
286
|
+
form: [],
|
|
287
|
+
}));
|
|
288
|
+
const wrapper = mountContact(mock, { submit });
|
|
289
|
+
await submitValid(wrapper);
|
|
290
|
+
|
|
291
|
+
expect(wrapper.find('form').exists()).toBe(true);
|
|
292
|
+
expect(wrapper.find('input[type="email"]').attributes('aria-invalid')).toBe('true');
|
|
293
|
+
expect(wrapper.text()).toContain('That domain does not accept mail.');
|
|
294
|
+
expect(wrapper.find('input[autocomplete="name"]').attributes('aria-invalid')).toBeUndefined();
|
|
295
|
+
expect(await axe(wrapper.element)).toHaveNoViolations();
|
|
215
296
|
});
|
|
216
297
|
|
|
217
298
|
it.each(['Enter', ' ', 'ArrowDown'])(
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"description": "The shop's address, opening hours, phone and email beside a short enquiry form, with a map that always offers an \"Open in maps\" link outside any embed.",
|
|
5
5
|
"icon": "map-pin",
|
|
6
6
|
"category": "marketing",
|
|
7
|
-
"version":
|
|
7
|
+
"version": 2,
|
|
8
8
|
"fields": [
|
|
9
9
|
{
|
|
10
10
|
"fieldId": "variant",
|
|
@@ -156,19 +156,12 @@
|
|
|
156
156
|
"type": "bool",
|
|
157
157
|
"default": false
|
|
158
158
|
},
|
|
159
|
-
{
|
|
160
|
-
"fieldId": "recipient",
|
|
161
|
-
"name": "Recipient",
|
|
162
|
-
"type": "string",
|
|
163
|
-
"helpText": "Where submissions go. Not shown.",
|
|
164
|
-
"validators": { "required": true }
|
|
165
|
-
},
|
|
166
159
|
{
|
|
167
160
|
"fieldId": "successText",
|
|
168
161
|
"name": "Success text",
|
|
169
162
|
"type": "text",
|
|
170
163
|
"localized": true,
|
|
171
|
-
"helpText": "Shown after a successful submit.
|
|
164
|
+
"helpText": "Shown after a successful submit. It never echoes what the visitor typed."
|
|
172
165
|
}
|
|
173
166
|
]
|
|
174
167
|
}
|
|
@@ -23,6 +23,5 @@
|
|
|
23
23
|
{ "label": "Something else" }
|
|
24
24
|
],
|
|
25
25
|
"showOrderNumber": true,
|
|
26
|
-
"
|
|
27
|
-
"successText": "We reply within one business day, usually sooner. A copy is on its way to {email}."
|
|
26
|
+
"successText": "We reply within one business day, usually sooner."
|
|
28
27
|
}
|
|
@@ -48,7 +48,7 @@
|
|
|
48
48
|
* The currency selector is different: the spec was written for a multi-currency store, but the
|
|
49
49
|
* platform supports exactly one currency per store today. So its options are derived from the
|
|
50
50
|
* store (`useMoney().currency`, `app/storefront/money.ts`), not from a constant list, and there is
|
|
51
|
-
* at most one of them — `currencyLabel()` names it "ISK kr" style (code + the
|
|
51
|
+
* at most one of them — `currencyLabel()` names it "ISK kr." style (code + the sign
|
|
52
52
|
* `@eldrajs/ui` writes the store's prices with in the content locale, falling back to the code
|
|
53
53
|
* alone when the locale has no sign distinct from it). With exactly one option
|
|
54
54
|
* there is nothing to select, so it renders as plain text with the same leading icon and a visually
|
|
@@ -57,7 +57,8 @@
|
|
|
57
57
|
* slot renders nothing. The `Select` branch stays for the day the derived list carries two or more
|
|
58
58
|
* entries; which branch renders is driven by that list's length, not by a separate flag.
|
|
59
59
|
*
|
|
60
|
-
* The newsletter zone posts through `useStorefront().forms.
|
|
60
|
+
* The newsletter zone posts through `useStorefront().forms.submit('newsletter', …)` — the platform's
|
|
61
|
+
* seeded `newsletter` form, the same one the newsletter block defaults to. Email format is validated
|
|
61
62
|
* locally (empty/malformed never reaches the storefront); a backend `{ ok: false }` shows the same
|
|
62
63
|
* error slot. Both failure paths return focus to the email field — `FormLayout`'s own submit
|
|
63
64
|
* handler only refuses a *second* submit while a field is already marked invalid, so the first
|
|
@@ -96,8 +97,10 @@ import { useBlockData } from '../../app/composables/useBlockData';
|
|
|
96
97
|
import { useEditing } from '../../app/composables/useEditing';
|
|
97
98
|
import { useI18n } from 'vue-i18n';
|
|
98
99
|
import { useUiId } from '../../app/composables/useUiId';
|
|
100
|
+
import { useFormSpam } from '../../app/composables/useFormSpam';
|
|
99
101
|
import { useStorefront } from '../../app/composables/useStorefront';
|
|
100
102
|
import EldraIcon from '../../app/components/EldraIcon.vue';
|
|
103
|
+
import FormHoneypot from '../../app/components/FormHoneypot.vue';
|
|
101
104
|
import EldraRouterLink from '../../app/components/EldraRouterLink.vue';
|
|
102
105
|
import type { ThemeIconName } from '../../app/icons';
|
|
103
106
|
import { currencyLabel, useMoney } from '../../app/storefront/money';
|
|
@@ -109,6 +112,9 @@ type FooterLink = {
|
|
|
109
112
|
as: typeof EldraRouterLink | undefined;
|
|
110
113
|
};
|
|
111
114
|
|
|
115
|
+
/** The seeded form the footer's sign-up zone submits to (`app/formSeeds.ts`). */
|
|
116
|
+
const NEWSLETTER_FORM_KEY = 'newsletter';
|
|
117
|
+
|
|
112
118
|
const props = defineProps<{ entry: EldraBlockEntry<'footer'> }>();
|
|
113
119
|
const { data } = useBlockData(props, 'footer');
|
|
114
120
|
const { t } = useI18n();
|
|
@@ -254,7 +260,7 @@ const showCurrency = computed(() => data.value.showCurrency ?? true);
|
|
|
254
260
|
* The currency selector's option list, derived from the store rather than the spec's hard-coded
|
|
255
261
|
* USD/CAD/EUR demo list: the platform supports exactly one currency per store today, so this is
|
|
256
262
|
* either empty (the store publishes none) or a single entry — the store's currency, named "ISK
|
|
257
|
-
* kr" style by `currencyLabel()` (code + the same
|
|
263
|
+
* kr." style by `currencyLabel()` (code + the same sign every price on the page is written
|
|
258
264
|
* with). Kept as a list, not a single value, so the day the platform supports more than one
|
|
259
265
|
* currency, this grows to match and the template's `Select` branch (below) is already there.
|
|
260
266
|
*/
|
|
@@ -308,6 +314,18 @@ const emailFieldRoot = ref<HTMLElement | null>(null);
|
|
|
308
314
|
const showNewsletter = computed(
|
|
309
315
|
() => variant.value === 'default' && (data.value.showNewsletter ?? true)
|
|
310
316
|
);
|
|
317
|
+
// Only a footer that shows the sign-up reads the form's definition (its honeypot and Turnstile).
|
|
318
|
+
const {
|
|
319
|
+
honeypot,
|
|
320
|
+
honeypotField,
|
|
321
|
+
siteKey,
|
|
322
|
+
consentSentence,
|
|
323
|
+
turnstileRoot,
|
|
324
|
+
challenge,
|
|
325
|
+
signals,
|
|
326
|
+
reset,
|
|
327
|
+
} = useFormSpam(computed(() => (showNewsletter.value ? NEWSLETTER_FORM_KEY : '')));
|
|
328
|
+
const consentSentenceId = `footer-consent-sentence-${useUiId()}`;
|
|
311
329
|
|
|
312
330
|
/** Mirrors `blocks/newsletter/Block.vue`'s own comment: an error has to clear itself the moment
|
|
313
331
|
* its own condition is fixed (the visitor edits the address), not only inside a later successful
|
|
@@ -353,9 +371,23 @@ async function onNewsletterSubmit(payload: FormLayoutSubmitPayload): Promise<voi
|
|
|
353
371
|
return;
|
|
354
372
|
}
|
|
355
373
|
|
|
374
|
+
// Turnstile is on and not passed: nothing about the address is wrong, so a retry is welcome.
|
|
375
|
+
const blocked = challenge();
|
|
376
|
+
if (blocked !== null) {
|
|
377
|
+
retryable.value = true;
|
|
378
|
+
emailError.value = blocked;
|
|
379
|
+
await focusEmailField();
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
|
|
356
383
|
emailError.value = null;
|
|
357
384
|
newsletterState.value = 'submitting';
|
|
358
|
-
const result = await storefront.forms.
|
|
385
|
+
const result = await storefront.forms.submit(NEWSLETTER_FORM_KEY, {
|
|
386
|
+
values: { email: value },
|
|
387
|
+
...signals(),
|
|
388
|
+
});
|
|
389
|
+
// A Turnstile token is single-use, whatever the outcome.
|
|
390
|
+
reset();
|
|
359
391
|
|
|
360
392
|
if (result.ok) {
|
|
361
393
|
newsletterState.value = 'success';
|
|
@@ -365,7 +397,9 @@ async function onNewsletterSubmit(payload: FormLayoutSubmitPayload): Promise<voi
|
|
|
365
397
|
newsletterState.value = 'idle';
|
|
366
398
|
const serviceFailed = result.reason !== 'invalid';
|
|
367
399
|
retryable.value = serviceFailed;
|
|
368
|
-
emailError.value = serviceFailed
|
|
400
|
+
emailError.value = serviceFailed
|
|
401
|
+
? t('footer.emailError')
|
|
402
|
+
: (result.errors.email?.[0] ?? t('footer.emailInvalid'));
|
|
369
403
|
await focusEmailField();
|
|
370
404
|
}
|
|
371
405
|
</script>
|
|
@@ -511,10 +545,26 @@ async function onNewsletterSubmit(payload: FormLayoutSubmitPayload): Promise<voi
|
|
|
511
545
|
/>
|
|
512
546
|
</FieldWrapper>
|
|
513
547
|
</div>
|
|
548
|
+
<FormHoneypot v-model="honeypot" :name="honeypotField" />
|
|
549
|
+
<div v-if="siteKey" ref="turnstileRoot" class="w-full" />
|
|
514
550
|
<template #actions>
|
|
515
|
-
<
|
|
516
|
-
|
|
517
|
-
|
|
551
|
+
<div class="grid w-full gap-2">
|
|
552
|
+
<Button
|
|
553
|
+
type="submit"
|
|
554
|
+
variant="primary"
|
|
555
|
+
:label="t('footer.subscribing')"
|
|
556
|
+
:aria-describedby="consentSentence ? consentSentenceId : undefined"
|
|
557
|
+
>{{ t('footer.subscribe') }}</Button
|
|
558
|
+
>
|
|
559
|
+
<p
|
|
560
|
+
v-if="consentSentence"
|
|
561
|
+
:id="consentSentenceId"
|
|
562
|
+
data-part="consent-sentence"
|
|
563
|
+
class="text-muted text-body-sm"
|
|
564
|
+
>
|
|
565
|
+
{{ consentSentence }}
|
|
566
|
+
</p>
|
|
567
|
+
</div>
|
|
518
568
|
</template>
|
|
519
569
|
</FormLayout>
|
|
520
570
|
</template>
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
import { flushPromises, mount } from '@vue/test-utils';
|
|
3
|
-
import { describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
4
4
|
import { ELDRA_KEY, type EldraLocaleState } from '@eldrajs/theme-vue';
|
|
5
5
|
import { EditorPlaceholder } from '@eldrajs/ui';
|
|
6
6
|
import { axe } from '../../../test/support/axe';
|
|
@@ -8,11 +8,22 @@ import Block from '../Block.vue';
|
|
|
8
8
|
import mock from '../mock.json';
|
|
9
9
|
import { mountOptions } from '../../../test/support/mountBlock';
|
|
10
10
|
import { STOREFRONT_KEY } from '../../../app/storefront/types';
|
|
11
|
-
import type {
|
|
11
|
+
import type {
|
|
12
|
+
StorefrontCommerce,
|
|
13
|
+
StorefrontFormSubmitResult,
|
|
14
|
+
StorefrontForms,
|
|
15
|
+
} from '../../../app/storefront/types';
|
|
12
16
|
import { createDemoStorefront } from '../../../app/storefront/demo';
|
|
13
17
|
import { currencyLabel } from '../../../app/storefront/money';
|
|
14
18
|
import enUS from '../../../i18n/en-US.json';
|
|
15
19
|
import isIS from '../../../i18n/is-IS.json';
|
|
20
|
+
import {
|
|
21
|
+
cleanUpTurnstile,
|
|
22
|
+
storefrontWithTurnstile,
|
|
23
|
+
stubTurnstile,
|
|
24
|
+
} from '../../../test/support/turnstileForms';
|
|
25
|
+
|
|
26
|
+
afterEach(cleanUpTurnstile);
|
|
16
27
|
|
|
17
28
|
/**
|
|
18
29
|
* `mock.json` names a collection or product by handle, because a theme cannot know an
|
|
@@ -67,7 +78,9 @@ function mountFooter(
|
|
|
67
78
|
data: Record<string, unknown>,
|
|
68
79
|
options: {
|
|
69
80
|
failForms?: boolean;
|
|
70
|
-
subscribe?: StorefrontForms['
|
|
81
|
+
subscribe?: StorefrontForms['submit'];
|
|
82
|
+
/** Turn Cloudflare Turnstile on for the sign-up form (with `subscribe` as its submit). */
|
|
83
|
+
turnstile?: boolean;
|
|
71
84
|
/** Studio's edit mode — what `useEditing()` reads, and the only state that shows hints. */
|
|
72
85
|
editing?: boolean;
|
|
73
86
|
/** The content locale — `@eldrajs/ui`'s number locale follows it (`mountOptions`). */
|
|
@@ -99,12 +112,15 @@ function mountFooter(
|
|
|
99
112
|
context.preview.active = true;
|
|
100
113
|
context.preview.mode = 'edit';
|
|
101
114
|
}
|
|
102
|
-
const storefront =
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
115
|
+
const storefront =
|
|
116
|
+
options.subscribe && options.turnstile
|
|
117
|
+
? storefrontWithTurnstile(options.subscribe)
|
|
118
|
+
: options.subscribe
|
|
119
|
+
? (() => {
|
|
120
|
+
const demo = createDemoStorefront();
|
|
121
|
+
return { ...demo, forms: { ...demo.forms, submit: options.subscribe! } };
|
|
122
|
+
})()
|
|
123
|
+
: undefined;
|
|
108
124
|
return mount(Block, {
|
|
109
125
|
...base,
|
|
110
126
|
// Real focus tracking (`document.activeElement`, and `Select`'s own focus-return behaviour)
|
|
@@ -303,6 +319,18 @@ describe('footer block', () => {
|
|
|
303
319
|
expect(label.classes()).toContain('sr-only');
|
|
304
320
|
});
|
|
305
321
|
|
|
322
|
+
it('shows the platform consent sentence under the sign-up button, tied to it with aria-describedby', async () => {
|
|
323
|
+
const wrapper = mountFooter(mock);
|
|
324
|
+
await flushPromises();
|
|
325
|
+
const sentence = wrapper.get('[data-part="consent-sentence"]');
|
|
326
|
+
expect(sentence.text()).toBe(
|
|
327
|
+
'I want to receive the newsletter from Northwind Goods. I can unsubscribe at any time.'
|
|
328
|
+
);
|
|
329
|
+
expect(wrapper.get('button[type="submit"]').attributes('aria-describedby')).toBe(
|
|
330
|
+
sentence.attributes('id')
|
|
331
|
+
);
|
|
332
|
+
});
|
|
333
|
+
|
|
306
334
|
it('an invalid (malformed) email submit sets aria-invalid, shows the error linked by aria-describedby, and keeps focus in the field', async () => {
|
|
307
335
|
const wrapper = mountFooter(mock);
|
|
308
336
|
const input = wrapper.find('input[type="email"]');
|
|
@@ -330,7 +358,7 @@ describe('footer block', () => {
|
|
|
330
358
|
expect(wrapper.text()).toContain(enUS.footer.emailInvalid);
|
|
331
359
|
});
|
|
332
360
|
|
|
333
|
-
it('a valid submit calls forms.
|
|
361
|
+
it('a valid submit calls forms.submit and announces success through role="status"', async () => {
|
|
334
362
|
const wrapper = mountFooter(mock);
|
|
335
363
|
const input = wrapper.find('input[type="email"]');
|
|
336
364
|
await input.setValue('reader@example.com');
|
|
@@ -345,6 +373,30 @@ describe('footer block', () => {
|
|
|
345
373
|
expect(wrapper.find('form').exists()).toBe(false);
|
|
346
374
|
});
|
|
347
375
|
|
|
376
|
+
it('with Turnstile on, renders the widget and honeypot, refuses to send before the check, then sends the token and honeypot', async () => {
|
|
377
|
+
const turnstile = stubTurnstile();
|
|
378
|
+
const subscribe = vi.fn(async () => ({ ok: true as const, pending: false }));
|
|
379
|
+
const wrapper = mountFooter(mock, { subscribe, turnstile: true });
|
|
380
|
+
await flushPromises();
|
|
381
|
+
expect(turnstile.render).toHaveBeenCalledOnce();
|
|
382
|
+
|
|
383
|
+
await wrapper.find('input[type="email"]').setValue('reader@example.com');
|
|
384
|
+
await wrapper.find('form').trigger('submit');
|
|
385
|
+
await flushPromises();
|
|
386
|
+
expect(subscribe).not.toHaveBeenCalled();
|
|
387
|
+
expect(wrapper.text()).toContain(enUS.form.turnstileRequired);
|
|
388
|
+
|
|
389
|
+
turnstile.solve('token-1');
|
|
390
|
+
await wrapper.find('input[name="website"]').setValue('bot');
|
|
391
|
+
await wrapper.find('form').trigger('submit');
|
|
392
|
+
await flushPromises();
|
|
393
|
+
expect(subscribe).toHaveBeenCalledWith('newsletter', {
|
|
394
|
+
values: { email: 'reader@example.com' },
|
|
395
|
+
turnstileToken: 'token-1',
|
|
396
|
+
hp: 'bot',
|
|
397
|
+
});
|
|
398
|
+
});
|
|
399
|
+
|
|
348
400
|
it('a subscribe failure (backend ok:false) shows an error and keeps focus in the field', async () => {
|
|
349
401
|
const wrapper = mountFooter(mock, { failForms: true });
|
|
350
402
|
const input = wrapper.find('input[type="email"]');
|
|
@@ -367,8 +419,11 @@ describe('footer block', () => {
|
|
|
367
419
|
// removing `@invalid="onNewsletterInvalid"` (or `retryable.value = serviceFailed` in
|
|
368
420
|
// `onNewsletterSubmit`) makes `subscribe` stay called once and the field stay marked invalid
|
|
369
421
|
// forever — confirmed by temporarily reverting each change and observing this test fail.
|
|
370
|
-
const results = [
|
|
371
|
-
|
|
422
|
+
const results: StorefrontFormSubmitResult[] = [
|
|
423
|
+
{ ok: false, reason: 'unavailable' },
|
|
424
|
+
{ ok: true, pending: true },
|
|
425
|
+
];
|
|
426
|
+
const subscribe = vi.fn(async () => results.shift() ?? { ok: true as const, pending: true });
|
|
372
427
|
const wrapper = mountFooter(mock, { subscribe });
|
|
373
428
|
const input = wrapper.find('input[type="email"]');
|
|
374
429
|
await input.setValue('reader@example.com');
|
|
@@ -383,9 +438,8 @@ describe('footer block', () => {
|
|
|
383
438
|
await flushPromises();
|
|
384
439
|
|
|
385
440
|
expect(subscribe).toHaveBeenCalledTimes(2);
|
|
386
|
-
expect(subscribe).toHaveBeenLastCalledWith({
|
|
387
|
-
email: 'reader@example.com',
|
|
388
|
-
list: 'footer-newsletter',
|
|
441
|
+
expect(subscribe).toHaveBeenLastCalledWith('newsletter', {
|
|
442
|
+
values: { email: 'reader@example.com' },
|
|
389
443
|
});
|
|
390
444
|
expect(wrapper.find('form').exists()).toBe(false);
|
|
391
445
|
expect(wrapper.find('[role="status"]').text()).toContain(enUS.footer.subscribed);
|
|
@@ -393,7 +447,7 @@ describe('footer block', () => {
|
|
|
393
447
|
|
|
394
448
|
it('a backend {ok:false, reason:"invalid"} shows the exact same invalid-email message (not the retryable one)', async () => {
|
|
395
449
|
const wrapper = mountFooter(mock, {
|
|
396
|
-
subscribe: async () => ({ ok: false, reason: 'invalid' }),
|
|
450
|
+
subscribe: async () => ({ ok: false, reason: 'invalid', errors: {}, form: [] }),
|
|
397
451
|
});
|
|
398
452
|
const input = wrapper.find('input[type="email"]');
|
|
399
453
|
await input.setValue('reader@example.com');
|
|
Binary file
|