@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
|
@@ -8,6 +8,7 @@ import { execa } from 'execa';
|
|
|
8
8
|
import { scanTheme } from '@eldrajs/vite-plugin-theme/scan';
|
|
9
9
|
import { encodeStega } from '@eldrajs/theme-core/stega';
|
|
10
10
|
import { safeHref } from '../app/utils/links';
|
|
11
|
+
import { starterFormSeeds } from '../app/formSeeds';
|
|
11
12
|
import { starterSeeds, starterTemplateRoles, stripSeedMedia } from '../app/templates';
|
|
12
13
|
|
|
13
14
|
const templateDir = fileURLToPath(new URL('..', import.meta.url));
|
|
@@ -35,6 +36,7 @@ const expectedBlocks = [
|
|
|
35
36
|
'faq',
|
|
36
37
|
'feature-grid',
|
|
37
38
|
'footer',
|
|
39
|
+
'form',
|
|
38
40
|
'gallery',
|
|
39
41
|
'hero',
|
|
40
42
|
'image',
|
|
@@ -102,7 +104,7 @@ describe('starter theme', () => {
|
|
|
102
104
|
])
|
|
103
105
|
);
|
|
104
106
|
const expectedFields: Record<string, string[]> = {
|
|
105
|
-
// Every one of the
|
|
107
|
+
// Every one of the 35 blocks has its field list here — the map used to cover 31, leaving
|
|
106
108
|
// `announcement-bar` and `newsletter` with only the manifest version rule behind a field
|
|
107
109
|
// rename or reorder, not the explicit list every sibling block has.
|
|
108
110
|
'announcement-bar': ['variant', 'message', 'linkLabel', 'linkHref', 'dismissable'],
|
|
@@ -118,7 +120,17 @@ describe('starter theme', () => {
|
|
|
118
120
|
'consentCheckboxLabel',
|
|
119
121
|
'successTitle',
|
|
120
122
|
'successText',
|
|
121
|
-
'
|
|
123
|
+
'formKey',
|
|
124
|
+
'sectionBackground',
|
|
125
|
+
],
|
|
126
|
+
form: [
|
|
127
|
+
'preset',
|
|
128
|
+
'formKey',
|
|
129
|
+
'heading',
|
|
130
|
+
'text',
|
|
131
|
+
'buttonLabel',
|
|
132
|
+
'consent',
|
|
133
|
+
'successTitle',
|
|
122
134
|
'sectionBackground',
|
|
123
135
|
],
|
|
124
136
|
hero: [
|
|
@@ -297,7 +309,6 @@ describe('starter theme', () => {
|
|
|
297
309
|
'formTitle',
|
|
298
310
|
'topics',
|
|
299
311
|
'showOrderNumber',
|
|
300
|
-
'recipient',
|
|
301
312
|
'successText',
|
|
302
313
|
],
|
|
303
314
|
'video-embed': [
|
|
@@ -528,7 +539,7 @@ describe('starter theme', () => {
|
|
|
528
539
|
'footer',
|
|
529
540
|
]);
|
|
530
541
|
expect(readFileSync(output('_headers'), 'utf8')).toContain(
|
|
531
|
-
"frame-ancestors 'self' https://
|
|
542
|
+
"frame-ancestors 'self' https://eldra.app"
|
|
532
543
|
);
|
|
533
544
|
// A build with no credentials cannot know what the store sells in, and says so in the one place
|
|
534
545
|
// that matters: the runtime config baked into every page carries no currency, rather than one
|
|
@@ -875,6 +886,8 @@ describe('seeded templates and pages (app/templates.ts)', () => {
|
|
|
875
886
|
// page seeds in one list, exactly as `eldra.templates` takes them.
|
|
876
887
|
templates: starterSeeds(),
|
|
877
888
|
templateRoles: starterTemplateRoles(),
|
|
889
|
+
// What `nuxt.config.ts` passes as `eldra.formSeeds`.
|
|
890
|
+
formSeeds: starterFormSeeds(),
|
|
878
891
|
});
|
|
879
892
|
|
|
880
893
|
/** The blocks of one `pages/<name>.page.json` fixture in order, minus the two role blocks. */
|
|
@@ -887,6 +900,31 @@ describe('seeded templates and pages (app/templates.ts)', () => {
|
|
|
887
900
|
.filter((block) => block.apiId !== 'navigation' && block.apiId !== 'footer')
|
|
888
901
|
.map((block) => `${block.apiId}#${block.id}`);
|
|
889
902
|
|
|
903
|
+
/**
|
|
904
|
+
* The forms the blocks submit to must exist on a fresh site: `contact` (the contact block) and
|
|
905
|
+
* `newsletter` (the newsletter block's default and the footer's sign-up zone). A block that
|
|
906
|
+
* names a form no seed creates renders "this form can't be shown" until a merchant builds it.
|
|
907
|
+
*/
|
|
908
|
+
it('seeds the contact and newsletter forms the starter blocks submit to', () => {
|
|
909
|
+
expect(scanned.errors).toEqual([]);
|
|
910
|
+
const forms = scanned.manifest!.formSeeds!;
|
|
911
|
+
expect(forms.map((form) => form.key)).toEqual(['contact', 'newsletter']);
|
|
912
|
+
// The newsletter carries its consent sentence in both shipped locales, with the shop placeholder.
|
|
913
|
+
expect(forms[1]!.actions.subscribe?.list).toBe('newsletter');
|
|
914
|
+
expect(Object.keys(forms[1]!.actions.subscribe?.consentText ?? {})).toEqual(['en-US', 'is-IS']);
|
|
915
|
+
for (const text of Object.values(forms[1]!.actions.subscribe?.consentText ?? {})) {
|
|
916
|
+
expect(text).toContain('{shop}');
|
|
917
|
+
}
|
|
918
|
+
// The contact block sends exactly these values (`blocks/contact/Block.vue`).
|
|
919
|
+
expect(forms[0]!.fields.map((field) => field.id)).toEqual([
|
|
920
|
+
'name',
|
|
921
|
+
'email',
|
|
922
|
+
'topic',
|
|
923
|
+
'orderNumber',
|
|
924
|
+
'message',
|
|
925
|
+
]);
|
|
926
|
+
});
|
|
927
|
+
|
|
890
928
|
/**
|
|
891
929
|
* The emitted page-seed wire shape, which Core decodes strictly — an unknown key fails the whole
|
|
892
930
|
* manifest ingest, so this asserts the *exact* object rather than a subset.
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { nextTick, reactive, ref } from 'vue';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
cartIdFromCheckoutUrl,
|
|
5
|
+
formatValue,
|
|
6
|
+
SEARCH_DEBOUNCE_MS,
|
|
7
|
+
withAnalytics,
|
|
8
|
+
} from '../../app/storefront/analytics';
|
|
9
|
+
import { createCartStore, type CartOps } from '../../app/storefront/cart';
|
|
10
|
+
import type {
|
|
11
|
+
StorefrontCartLine,
|
|
12
|
+
StorefrontProduct,
|
|
13
|
+
StorefrontResult,
|
|
14
|
+
StorefrontSearchResponse,
|
|
15
|
+
StorefrontSource,
|
|
16
|
+
} from '../../app/storefront/types';
|
|
17
|
+
|
|
18
|
+
const line: StorefrontCartLine = {
|
|
19
|
+
id: 'line-1',
|
|
20
|
+
productId: 'p1',
|
|
21
|
+
variantId: 'v1',
|
|
22
|
+
title: 'Mug',
|
|
23
|
+
url: '/products/mug',
|
|
24
|
+
variantLabel: '',
|
|
25
|
+
quantity: 2,
|
|
26
|
+
unitPrice: 12.5,
|
|
27
|
+
lineTotal: 25,
|
|
28
|
+
image: null,
|
|
29
|
+
max: null,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function result<T>(initial: T | null): StorefrontResult<T> {
|
|
33
|
+
return {
|
|
34
|
+
data: ref(initial) as StorefrontResult<T>['data'],
|
|
35
|
+
pending: ref(false),
|
|
36
|
+
error: ref(null),
|
|
37
|
+
loading: ref(false),
|
|
38
|
+
revalidating: ref(new Set()),
|
|
39
|
+
refresh: () => Promise.resolve(),
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function makeSource(overrides: { product?: StorefrontResult<StorefrontProduct> } = {}) {
|
|
44
|
+
const lines = [line];
|
|
45
|
+
const ops: CartOps = {
|
|
46
|
+
init: () => Promise.resolve({ lines: [], totals: { subtotal: 0, shipping: null, total: 0 } }),
|
|
47
|
+
add: () => Promise.resolve({ lines, totals: { subtotal: 25, shipping: null, total: 25 } }),
|
|
48
|
+
setQuantity: (_id, quantity) =>
|
|
49
|
+
Promise.resolve({
|
|
50
|
+
lines: [{ ...line, quantity }],
|
|
51
|
+
totals: { subtotal: 12.5 * quantity, shipping: null, total: 12.5 * quantity },
|
|
52
|
+
}),
|
|
53
|
+
remove: () => Promise.resolve({ lines: [], totals: { subtotal: 0, shipping: null, total: 0 } }),
|
|
54
|
+
applyDiscount: () => Promise.reject(new Error('unused')),
|
|
55
|
+
removeDiscount: () => Promise.reject(new Error('unused')),
|
|
56
|
+
checkoutUrl: ref('https://pay.example/checkout/org-1/cart-9'),
|
|
57
|
+
};
|
|
58
|
+
const cart = createCartStore(ops);
|
|
59
|
+
const route = reactive({ productHandle: 'mug' as string | null });
|
|
60
|
+
const searchResult = result<StorefrontSearchResponse>(null);
|
|
61
|
+
const product = overrides.product ?? result<StorefrontProduct>(null);
|
|
62
|
+
const source = {
|
|
63
|
+
commerce: { currency: 'USD', taxInclusivePricing: false, defaultTaxRate: 0 },
|
|
64
|
+
route,
|
|
65
|
+
cart,
|
|
66
|
+
catalog: { product: () => product },
|
|
67
|
+
search: { run: () => searchResult },
|
|
68
|
+
} as unknown as StorefrontSource;
|
|
69
|
+
return { source, cart, route, searchResult, product };
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
describe('formatValue / cartIdFromCheckoutUrl', () => {
|
|
73
|
+
it('writes major units with a dot', () => {
|
|
74
|
+
expect(formatValue(28)).toBe('28');
|
|
75
|
+
expect(formatValue(28.5)).toBe('28.5');
|
|
76
|
+
expect(formatValue(0.1 + 0.2)).toBe('0.3');
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('reads the cart id off the platform checkout URL', () => {
|
|
80
|
+
expect(cartIdFromCheckoutUrl('https://pay.example/checkout/org-1/cart-9')).toBe('cart-9');
|
|
81
|
+
expect(cartIdFromCheckoutUrl('https://pay.example/elsewhere')).toBeUndefined();
|
|
82
|
+
expect(cartIdFromCheckoutUrl(null)).toBeUndefined();
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
describe('withAnalytics', () => {
|
|
87
|
+
beforeEach(() => vi.useFakeTimers());
|
|
88
|
+
afterEach(() => vi.useRealTimers());
|
|
89
|
+
|
|
90
|
+
it('tracks add_to_cart only when the add succeeded', async () => {
|
|
91
|
+
const { source } = makeSource();
|
|
92
|
+
const track = vi.fn();
|
|
93
|
+
const wrapped = withAnalytics(source, track);
|
|
94
|
+
await wrapped.cart.add({ productId: 'p1', variantId: 'v1', quantity: 2 });
|
|
95
|
+
expect(track).toHaveBeenCalledWith('add_to_cart', {
|
|
96
|
+
product_id: 'p1',
|
|
97
|
+
variant_id: 'v1',
|
|
98
|
+
quantity: 2,
|
|
99
|
+
value: '25',
|
|
100
|
+
currency: 'USD',
|
|
101
|
+
});
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('does not track a refused add', async () => {
|
|
105
|
+
const { source } = makeSource();
|
|
106
|
+
const track = vi.fn();
|
|
107
|
+
const failing = {
|
|
108
|
+
...source,
|
|
109
|
+
cart: { ...source.cart, add: () => Promise.resolve({ message: 'no' }) },
|
|
110
|
+
} as unknown as StorefrontSource;
|
|
111
|
+
await withAnalytics(failing, track).cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
112
|
+
expect(track).not.toHaveBeenCalled();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('tracks remove_from_cart with the removed line', async () => {
|
|
116
|
+
const { source, cart } = makeSource();
|
|
117
|
+
const track = vi.fn();
|
|
118
|
+
const wrapped = withAnalytics(source, track);
|
|
119
|
+
await cart.add({ productId: 'p1', variantId: 'v1', quantity: 2 });
|
|
120
|
+
await wrapped.cart.remove('line-1');
|
|
121
|
+
expect(track).toHaveBeenCalledWith('remove_from_cart', {
|
|
122
|
+
product_id: 'p1',
|
|
123
|
+
variant_id: 'v1',
|
|
124
|
+
quantity: 2,
|
|
125
|
+
value: '25',
|
|
126
|
+
currency: 'USD',
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('tracks a quantity change as the difference', async () => {
|
|
131
|
+
const { source, cart } = makeSource();
|
|
132
|
+
const track = vi.fn();
|
|
133
|
+
const wrapped = withAnalytics(source, track);
|
|
134
|
+
await cart.add({ productId: 'p1', variantId: 'v1', quantity: 2 });
|
|
135
|
+
await wrapped.cart.setQuantity('line-1', 5);
|
|
136
|
+
expect(track).toHaveBeenLastCalledWith('add_to_cart', {
|
|
137
|
+
product_id: 'p1',
|
|
138
|
+
variant_id: 'v1',
|
|
139
|
+
quantity: 3,
|
|
140
|
+
value: '37.5',
|
|
141
|
+
currency: 'USD',
|
|
142
|
+
});
|
|
143
|
+
await wrapped.cart.setQuantity('line-1', 4);
|
|
144
|
+
expect(track).toHaveBeenLastCalledWith('remove_from_cart', {
|
|
145
|
+
product_id: 'p1',
|
|
146
|
+
variant_id: 'v1',
|
|
147
|
+
quantity: 1,
|
|
148
|
+
value: '12.5',
|
|
149
|
+
currency: 'USD',
|
|
150
|
+
});
|
|
151
|
+
await wrapped.cart.setQuantity('line-1', 4);
|
|
152
|
+
expect(track).toHaveBeenCalledTimes(2);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('tracks an undone removal as an add', async () => {
|
|
156
|
+
const { source, cart } = makeSource();
|
|
157
|
+
const track = vi.fn();
|
|
158
|
+
const wrapped = withAnalytics(source, track);
|
|
159
|
+
await cart.add({ productId: 'p1', variantId: 'v1', quantity: 2 });
|
|
160
|
+
await wrapped.cart.remove('line-1');
|
|
161
|
+
await wrapped.cart.undoRemove();
|
|
162
|
+
expect(track).toHaveBeenCalledTimes(2);
|
|
163
|
+
expect(track).toHaveBeenLastCalledWith('add_to_cart', {
|
|
164
|
+
product_id: 'p1',
|
|
165
|
+
variant_id: 'v1',
|
|
166
|
+
quantity: 2,
|
|
167
|
+
value: '25',
|
|
168
|
+
currency: 'USD',
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('tracks begin_checkout from the cart state when the shopper follows the link', async () => {
|
|
173
|
+
const { source, cart } = makeSource();
|
|
174
|
+
const track = vi.fn();
|
|
175
|
+
await cart.add({ productId: 'p1', variantId: 'v1', quantity: 2 });
|
|
176
|
+
withAnalytics(source, track).cart.beginCheckout();
|
|
177
|
+
expect(track).toHaveBeenCalledWith('begin_checkout', {
|
|
178
|
+
cart_id: 'cart-9',
|
|
179
|
+
item_count: 2,
|
|
180
|
+
value: '25',
|
|
181
|
+
currency: 'USD',
|
|
182
|
+
});
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('tracks product_view once per visit, and again after leaving the product', async () => {
|
|
186
|
+
const product = result<StorefrontProduct>({
|
|
187
|
+
handle: 'mug',
|
|
188
|
+
productId: 'p1',
|
|
189
|
+
variantId: 'v1',
|
|
190
|
+
price: { amount: 12.5 },
|
|
191
|
+
} as StorefrontProduct);
|
|
192
|
+
const { source, route } = makeSource({ product });
|
|
193
|
+
const track = vi.fn();
|
|
194
|
+
const wrapped = withAnalytics(source, track);
|
|
195
|
+
wrapped.catalog.product(ref('mug'));
|
|
196
|
+
wrapped.catalog.product(ref('mug'));
|
|
197
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
198
|
+
expect(track).toHaveBeenCalledWith('product_view', {
|
|
199
|
+
product_id: 'p1',
|
|
200
|
+
handle: 'mug',
|
|
201
|
+
variant_id: 'v1',
|
|
202
|
+
value: '12.5',
|
|
203
|
+
currency: 'USD',
|
|
204
|
+
});
|
|
205
|
+
// A refresh re-resolves the same product: no second view.
|
|
206
|
+
product.data.value = { ...product.data.value! };
|
|
207
|
+
await nextTick();
|
|
208
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
209
|
+
// Leaving and coming back is a new visit.
|
|
210
|
+
route.productHandle = null;
|
|
211
|
+
await nextTick();
|
|
212
|
+
route.productHandle = 'mug';
|
|
213
|
+
await nextTick();
|
|
214
|
+
product.data.value = { ...product.data.value! };
|
|
215
|
+
await nextTick();
|
|
216
|
+
expect(track).toHaveBeenCalledTimes(2);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('does not count a related product as a view', () => {
|
|
220
|
+
const product = result<StorefrontProduct>({
|
|
221
|
+
handle: 'other',
|
|
222
|
+
productId: 'p2',
|
|
223
|
+
variantId: 'v2',
|
|
224
|
+
price: { amount: 1 },
|
|
225
|
+
} as StorefrontProduct);
|
|
226
|
+
const { source } = makeSource({ product });
|
|
227
|
+
const track = vi.fn();
|
|
228
|
+
withAnalytics(source, track).catalog.product(ref('other'));
|
|
229
|
+
expect(track).not.toHaveBeenCalled();
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('debounces search and sends the settled query once', async () => {
|
|
233
|
+
const { source, searchResult } = makeSource();
|
|
234
|
+
const track = vi.fn();
|
|
235
|
+
const query = ref('');
|
|
236
|
+
withAnalytics(source, track).search.run(query);
|
|
237
|
+
for (const text of ['m', 'mu', ' mug ']) {
|
|
238
|
+
query.value = text;
|
|
239
|
+
searchResult.data.value = { query: text, total: 3 } as StorefrontSearchResponse;
|
|
240
|
+
await nextTick();
|
|
241
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS / 4);
|
|
242
|
+
}
|
|
243
|
+
expect(track).not.toHaveBeenCalled();
|
|
244
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS);
|
|
245
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
246
|
+
expect(track).toHaveBeenCalledWith('search', { query: 'mug', result_count: 3 });
|
|
247
|
+
// The same settled query is not sent again.
|
|
248
|
+
searchResult.data.value = { query: 'mug', total: 3 } as StorefrontSearchResponse;
|
|
249
|
+
await nextTick();
|
|
250
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS * 2);
|
|
251
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
252
|
+
});
|
|
253
|
+
|
|
254
|
+
it('sends a query once when the header panel and the search page both run it', async () => {
|
|
255
|
+
const { source, searchResult } = makeSource();
|
|
256
|
+
const track = vi.fn();
|
|
257
|
+
const wrapped = withAnalytics(source, track);
|
|
258
|
+
const panel = ref('mug');
|
|
259
|
+
wrapped.search.run(panel);
|
|
260
|
+
searchResult.data.value = { query: 'mug', total: 3 } as StorefrontSearchResponse;
|
|
261
|
+
await nextTick();
|
|
262
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS * 2);
|
|
263
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
264
|
+
// The results page mounts with the same query.
|
|
265
|
+
wrapped.search.run(ref('mug'));
|
|
266
|
+
await nextTick();
|
|
267
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS * 2);
|
|
268
|
+
expect(track).toHaveBeenCalledTimes(1);
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
it('ignores an empty query and a result for a different query', async () => {
|
|
272
|
+
const { source, searchResult } = makeSource();
|
|
273
|
+
const track = vi.fn();
|
|
274
|
+
const query = ref('mug');
|
|
275
|
+
withAnalytics(source, track).search.run(query);
|
|
276
|
+
searchResult.data.value = { query: 'cup', total: 1 } as StorefrontSearchResponse;
|
|
277
|
+
await nextTick();
|
|
278
|
+
query.value = '';
|
|
279
|
+
await nextTick();
|
|
280
|
+
vi.advanceTimersByTime(SEARCH_DEBOUNCE_MS * 2);
|
|
281
|
+
expect(track).not.toHaveBeenCalled();
|
|
282
|
+
});
|
|
283
|
+
});
|