@eldrajs/theme-cli 0.2.5 → 0.2.7
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 +1 -0
- package/package.json +3 -3
- package/template/.eldra/block-types.d.ts +1 -1
- package/template/.eldra/manifest.json +32 -2
- package/template/.eldra/previews.json +35 -35
- package/template/app/app.vue +15 -0
- package/template/app/composables/useStorefront.ts +7 -2
- package/template/app/plugins/eldra-storefront.ts +9 -0
- package/template/app/storefront/analytics.ts +15 -7
- package/template/app/storefront/cart.ts +228 -51
- package/template/app/storefront/commerce.ts +12 -2
- package/template/app/storefront/currency.ts +272 -0
- package/template/app/storefront/demo.ts +10 -2
- package/template/app/storefront/feedback.ts +3 -0
- package/template/app/storefront/gateway.ts +153 -35
- package/template/app/storefront/money.ts +6 -18
- package/template/app/storefront/structuredData.ts +16 -5
- package/template/app/storefront/toProductCard.ts +14 -14
- package/template/app/storefront/types.ts +13 -0
- package/template/app/storefront/volatile.ts +4 -0
- package/template/app/utils/seoInput.ts +4 -3
- package/template/blocks/announcement-bar/preview.png +0 -0
- package/template/blocks/article/preview.png +0 -0
- package/template/blocks/article-list/preview.png +0 -0
- package/template/blocks/breadcrumbs/preview.png +0 -0
- package/template/blocks/cart/Block.vue +41 -22
- package/template/blocks/cart/__tests__/Block.spec.ts +2 -3
- package/template/blocks/cart/__tests__/loadFailed.spec.ts +135 -0
- package/template/blocks/cart/__tests__/notSold.spec.ts +150 -0
- package/template/blocks/cart/parts/CartLines.vue +14 -2
- package/template/blocks/cart/parts/CheckoutButton.vue +45 -0
- package/template/blocks/cart/parts/LoadFailed.vue +37 -0
- package/template/blocks/cart/parts/NotSoldNotice.vue +74 -0
- package/template/blocks/cart/parts/Summary.vue +9 -21
- package/template/blocks/cart/parts/notSold.ts +19 -0
- package/template/blocks/cart/preview.png +0 -0
- package/template/blocks/collection-grid/Block.vue +15 -2
- package/template/blocks/collection-grid/__tests__/Block.spec.ts +14 -0
- package/template/blocks/collection-grid/preview.png +0 -0
- package/template/blocks/collection-header/preview.png +0 -0
- package/template/blocks/contact/preview.png +0 -0
- package/template/blocks/cta/preview.png +0 -0
- package/template/blocks/faq/preview.png +0 -0
- package/template/blocks/feature-grid/preview.png +0 -0
- package/template/blocks/footer/Block.vue +20 -45
- package/template/blocks/footer/preview.png +0 -0
- package/template/blocks/form/preview.png +0 -0
- package/template/blocks/gallery/preview.png +0 -0
- package/template/blocks/hero/preview.png +0 -0
- package/template/blocks/image/preview.png +0 -0
- package/template/blocks/logo-cloud/preview.png +0 -0
- package/template/blocks/navigation/Block.vue +39 -3
- package/template/blocks/navigation/preview.png +0 -0
- package/template/blocks/newsletter/preview.png +0 -0
- package/template/blocks/order-status/Block.vue +15 -6
- package/template/blocks/order-status/preview.png +0 -0
- package/template/blocks/pricing-table/preview.png +0 -0
- package/template/blocks/product-carousel/Block.vue +5 -2
- package/template/blocks/product-carousel/preview.png +0 -0
- package/template/blocks/product-detail/Block.vue +68 -10
- package/template/blocks/product-detail/__tests__/Block.spec.ts +190 -2
- package/template/blocks/product-detail/preview.png +0 -0
- package/template/blocks/quote/preview.png +0 -0
- package/template/blocks/rich-text/preview.png +0 -0
- package/template/blocks/search/Block.vue +8 -2
- package/template/blocks/search/TypeSection.vue +8 -2
- package/template/blocks/search/preview.png +0 -0
- package/template/blocks/search/results.ts +5 -2
- package/template/blocks/split-content/preview.png +0 -0
- package/template/blocks/stats/preview.png +0 -0
- package/template/blocks/tabs/preview.png +0 -0
- package/template/blocks/team/preview.png +0 -0
- package/template/blocks/testimonials/preview.png +0 -0
- package/template/blocks/timeline/preview.png +0 -0
- package/template/blocks/trust-strip/preview.png +0 -0
- package/template/blocks/video-embed/preview.png +0 -0
- package/template/blocks/wishlist/Block.vue +5 -1
- package/template/blocks/wishlist/preview.png +0 -0
- package/template/i18n/en-US.json +20 -3
- package/template/i18n/is-IS.json +20 -3
- package/template/test/currencyPicker.spec.ts +66 -0
- package/template/test/seoInput.spec.ts +22 -0
- package/template/test/storefront/analytics.spec.ts +15 -0
- package/template/test/storefront/cart.spec.ts +4 -4
- package/template/test/storefront/currency.spec.ts +549 -0
- package/template/test/storefront/currencyDefault.spec.ts +475 -0
- package/template/test/storefront/currencyStore.spec.ts +845 -0
- package/template/test/storefront/gateway.spec.ts +22 -0
- package/template/test/storefront/money.spec.ts +2 -8
- package/template/test/storefront/structuredData.spec.ts +45 -0
- package/template/test/storefront/toProductCard.spec.ts +1 -1
- package/template/test/storefront/volatile.spec.ts +11 -0
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import { ref, watch } from 'vue';
|
|
3
|
+
import { EldraHttpError, type EldraClient } from '@eldrajs/sdk';
|
|
4
|
+
import { toStorefrontCommerce } from '../../app/storefront/commerce';
|
|
5
|
+
import { failureMessageKey } from '../../app/storefront/feedback';
|
|
6
|
+
import { createGatewayStorefront } from '../../app/storefront/gateway';
|
|
7
|
+
import type { StorefrontCommerce, StorefrontRoute } from '../../app/storefront/types';
|
|
8
|
+
|
|
9
|
+
const PREFERENCE_KEY = 'eldra.currency';
|
|
10
|
+
const CART_KEY = 'eldra.cartId';
|
|
11
|
+
|
|
12
|
+
const ISK_OFF: StorefrontCommerce = {
|
|
13
|
+
currency: 'ISK',
|
|
14
|
+
taxInclusivePricing: true,
|
|
15
|
+
defaultTaxRate: 0.24,
|
|
16
|
+
currencies: ['EUR', 'USD'],
|
|
17
|
+
defaultCurrency: 'EUR',
|
|
18
|
+
};
|
|
19
|
+
const EUR_ONLY: StorefrontCommerce = {
|
|
20
|
+
currency: 'ISK',
|
|
21
|
+
taxInclusivePricing: true,
|
|
22
|
+
defaultTaxRate: 0.24,
|
|
23
|
+
defaultCurrency: 'EUR',
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
function route(): StorefrontRoute {
|
|
27
|
+
return {
|
|
28
|
+
productHandle: null,
|
|
29
|
+
collectionHandle: null,
|
|
30
|
+
categorySlug: null,
|
|
31
|
+
categoryPath: null,
|
|
32
|
+
orderToken: null,
|
|
33
|
+
query: null,
|
|
34
|
+
page: 1,
|
|
35
|
+
sort: null,
|
|
36
|
+
columns: null,
|
|
37
|
+
filters: {},
|
|
38
|
+
setQuery: () => {},
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function installStorage(initial: Record<string, string> = {}): Map<string, string> {
|
|
43
|
+
const data = new Map(Object.entries(initial));
|
|
44
|
+
(globalThis as { localStorage?: Storage }).localStorage = {
|
|
45
|
+
getItem: (key: string) => data.get(key) ?? null,
|
|
46
|
+
setItem: (key: string, value: string) => void data.set(key, value),
|
|
47
|
+
removeItem: (key: string) => void data.delete(key),
|
|
48
|
+
clear: () => data.clear(),
|
|
49
|
+
key: () => null,
|
|
50
|
+
get length() {
|
|
51
|
+
return data.size;
|
|
52
|
+
},
|
|
53
|
+
} as Storage;
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
delete (globalThis as { localStorage?: Storage }).localStorage;
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
function cartAnswer(currency: string): Record<string, unknown> {
|
|
62
|
+
return {
|
|
63
|
+
id: 'cart-1',
|
|
64
|
+
currency,
|
|
65
|
+
items: [
|
|
66
|
+
{
|
|
67
|
+
id: 'line-1',
|
|
68
|
+
productId: 'p1',
|
|
69
|
+
variantId: 'v1',
|
|
70
|
+
title: 'Mug',
|
|
71
|
+
price: currency === 'ISK' ? 2900 : 19.9,
|
|
72
|
+
quantity: 1,
|
|
73
|
+
soldInCurrency: true,
|
|
74
|
+
},
|
|
75
|
+
],
|
|
76
|
+
totals: { subtotal: 0, discount: 0, taxAmount: 0, total: 0 },
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function notSold(): EldraHttpError {
|
|
81
|
+
return new EldraHttpError(new Response('{}', { status: 400, statusText: 'Bad Request' }), {
|
|
82
|
+
errorId: 'CART_CURRENCY_NOT_SOLD',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function shopClient(cart: Record<string, unknown> | null = null) {
|
|
87
|
+
const productOptions: unknown[] = [];
|
|
88
|
+
const calls: Array<{ method: string; args: unknown[] }> = [];
|
|
89
|
+
let current = cart;
|
|
90
|
+
const client = {
|
|
91
|
+
catalog: {
|
|
92
|
+
getProduct: async (_handle: string, options: Record<string, unknown> | undefined) => {
|
|
93
|
+
productOptions.push(options);
|
|
94
|
+
return {
|
|
95
|
+
id: 'p1',
|
|
96
|
+
slug: 'mug',
|
|
97
|
+
title: 'Mug',
|
|
98
|
+
status: 'ACTIVE',
|
|
99
|
+
options: [],
|
|
100
|
+
categoryIds: [],
|
|
101
|
+
mediaLinks: [],
|
|
102
|
+
variants: [
|
|
103
|
+
{
|
|
104
|
+
id: 'v1',
|
|
105
|
+
sku: 'M',
|
|
106
|
+
price: 19.9,
|
|
107
|
+
currency: options?.currency ?? 'ISK',
|
|
108
|
+
soldInCurrency: true,
|
|
109
|
+
status: 'ACTIVE',
|
|
110
|
+
},
|
|
111
|
+
],
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
listCategories: async () => [],
|
|
115
|
+
},
|
|
116
|
+
inventory: { availability: async () => [] },
|
|
117
|
+
cart: {
|
|
118
|
+
get: async () => current,
|
|
119
|
+
addItem: async (input: Record<string, unknown>) => {
|
|
120
|
+
calls.push({ method: 'addItem', args: [input] });
|
|
121
|
+
if (input.currency === 'ISK') throw notSold();
|
|
122
|
+
current = cartAnswer((input.currency as string | undefined) ?? 'ISK');
|
|
123
|
+
return current;
|
|
124
|
+
},
|
|
125
|
+
setCurrency: async (cartId: string, currency: string) => {
|
|
126
|
+
calls.push({ method: 'setCurrency', args: [cartId, currency] });
|
|
127
|
+
if (currency === 'ISK') throw notSold();
|
|
128
|
+
current = cartAnswer(currency);
|
|
129
|
+
return current;
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
checkout: { url: async () => 'https://checkout.example/x' },
|
|
133
|
+
} as unknown as EldraClient;
|
|
134
|
+
return { client, productOptions, calls };
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
describe('a store with its own currency turned off', () => {
|
|
138
|
+
it('builds pages in the default currency: every read names it, before and after start', async () => {
|
|
139
|
+
installStorage();
|
|
140
|
+
const { client, productOptions } = shopClient();
|
|
141
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
142
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
143
|
+
const product = storefront.catalog.product(ref('mug'));
|
|
144
|
+
await vi.waitFor(() => expect(product.data.value).not.toBeNull());
|
|
145
|
+
expect(productOptions[0]).toEqual({ currency: 'EUR' });
|
|
146
|
+
expect(product.data.value?.price.currency).toBe('EUR');
|
|
147
|
+
|
|
148
|
+
await storefront.currency.start(async () => null);
|
|
149
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
150
|
+
expect(storefront.currency.requested.value).toBe('EUR');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('never offers the store currency in the picker', () => {
|
|
154
|
+
installStorage();
|
|
155
|
+
const { client } = shopClient();
|
|
156
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
157
|
+
expect(storefront.currency.offered.value).toEqual(['EUR', 'USD']);
|
|
158
|
+
expect(storefront.currency.defaultCurrency).toBe('EUR');
|
|
159
|
+
expect(storefront.currency.base).toBe('ISK');
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('starts an Icelandic visitor, whose country currency is not offered, in the default', async () => {
|
|
163
|
+
installStorage();
|
|
164
|
+
const { client } = shopClient();
|
|
165
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
166
|
+
await storefront.currency.start(async () => 'IS');
|
|
167
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('drops a saved ISK preference and says where prices are now', async () => {
|
|
171
|
+
const data = installStorage({ [PREFERENCE_KEY]: 'ISK' });
|
|
172
|
+
const { client } = shopClient();
|
|
173
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
174
|
+
await storefront.currency.start(async () => null);
|
|
175
|
+
expect(data.has(PREFERENCE_KEY)).toBe(false);
|
|
176
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
177
|
+
expect(storefront.currency.movedTo.value).toBe('EUR');
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('drops a saved ISK preference in a store offering one currency, with no notice', async () => {
|
|
181
|
+
const data = installStorage({ [PREFERENCE_KEY]: 'ISK' });
|
|
182
|
+
const { client } = shopClient();
|
|
183
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: EUR_ONLY });
|
|
184
|
+
expect(storefront.currency.offered.value).toEqual(['EUR']);
|
|
185
|
+
await storefront.currency.start(async () => null);
|
|
186
|
+
expect(data.has(PREFERENCE_KEY)).toBe(false);
|
|
187
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
188
|
+
expect(storefront.currency.movedTo.value).toBeNull();
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('leaves a saved choice alone in a store selling in its own currency only, as before', async () => {
|
|
192
|
+
const data = installStorage({ [PREFERENCE_KEY]: 'EUR' });
|
|
193
|
+
const { client } = shopClient();
|
|
194
|
+
const storefront = createGatewayStorefront(client, {
|
|
195
|
+
route: route(),
|
|
196
|
+
commerce: { currency: 'ISK', taxInclusivePricing: true, defaultTaxRate: 0.24 },
|
|
197
|
+
});
|
|
198
|
+
await storefront.currency.start(async () => null);
|
|
199
|
+
expect(data.get(PREFERENCE_KEY)).toBe('EUR');
|
|
200
|
+
expect(storefront.currency.active.value).toBe('ISK');
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('creates a new cart in the default currency', async () => {
|
|
204
|
+
installStorage();
|
|
205
|
+
const { client, calls } = shopClient();
|
|
206
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: EUR_ONLY });
|
|
207
|
+
await storefront.currency.start(async () => null);
|
|
208
|
+
await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
209
|
+
expect(calls[0]?.args[0]).toMatchObject({ currency: 'EUR' });
|
|
210
|
+
expect(storefront.cart.currency.value).toBe('EUR');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('moves a cart from before ISK was turned off to the default currency', async () => {
|
|
214
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
215
|
+
const { client, calls } = shopClient(cartAnswer('ISK'));
|
|
216
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
217
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
218
|
+
await storefront.currency.start(async () => null);
|
|
219
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
220
|
+
expect(calls).toEqual([{ method: 'setCurrency', args: ['cart-1', 'EUR'] }]);
|
|
221
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
222
|
+
expect(storefront.currency.movedTo.value).toBe('EUR');
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('says prices are in the default only once, after moving a cart left in ISK', async () => {
|
|
226
|
+
installStorage({ [CART_KEY]: 'cart-1', [PREFERENCE_KEY]: 'ISK' });
|
|
227
|
+
const { client } = shopClient(cartAnswer('ISK'));
|
|
228
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
229
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
230
|
+
const notices: Array<string | null> = [];
|
|
231
|
+
watch(storefront.currency.movedTo, (code) => notices.push(code), { flush: 'sync' });
|
|
232
|
+
await storefront.currency.start(async () => null);
|
|
233
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
234
|
+
expect(notices).toEqual(['EUR']);
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
it('never withdraws the default when a move to it is refused', async () => {
|
|
238
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
239
|
+
const { client } = shopClient(cartAnswer('USD'));
|
|
240
|
+
client.cart.setCurrency = (async () => {
|
|
241
|
+
throw notSold();
|
|
242
|
+
}) as typeof client.cart.setCurrency;
|
|
243
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
244
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('USD'));
|
|
245
|
+
await storefront.currency.start(async () => null);
|
|
246
|
+
storefront.currency.chooseBase();
|
|
247
|
+
await vi.waitFor(() => expect(storefront.currency.failure.value).not.toBeNull());
|
|
248
|
+
expect(storefront.currency.offered.value).toEqual(['EUR', 'USD']);
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
it('withdraws ISK when the store refuses a move to it', async () => {
|
|
252
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
253
|
+
const { client } = shopClient(cartAnswer('EUR'));
|
|
254
|
+
const storefront = createGatewayStorefront(client, {
|
|
255
|
+
route: route(),
|
|
256
|
+
commerce: { ...ISK_OFF, currencies: ['EUR', 'ISK'] },
|
|
257
|
+
});
|
|
258
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
259
|
+
await storefront.currency.start(async () => null);
|
|
260
|
+
storefront.currency.choose('ISK');
|
|
261
|
+
await vi.waitFor(() => expect(storefront.currency.offered.value).toEqual(['EUR']));
|
|
262
|
+
expect(storefront.currency.active.value).toBe('EUR');
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
describe('toStorefrontCommerce', () => {
|
|
267
|
+
it('keeps the default currency the build wrote', () => {
|
|
268
|
+
expect(toStorefrontCommerce({ ...EUR_ONLY })).toEqual(EUR_ONLY);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe('a business customer, whose cart the store keeps in ISK', () => {
|
|
273
|
+
function iskOnly(): EldraHttpError {
|
|
274
|
+
return new EldraHttpError(new Response('{}', { status: 409, statusText: 'Conflict' }), {
|
|
275
|
+
errorId: 'CART_CURRENCY_CUSTOMER_ISK_ONLY',
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function businessClient(cart: Record<string, unknown> | null) {
|
|
280
|
+
const built = shopClient(cart);
|
|
281
|
+
let current = cart;
|
|
282
|
+
built.client.cart.get = (async () => current) as typeof built.client.cart.get;
|
|
283
|
+
built.client.cart.addItem = (async (input: Record<string, unknown>) => {
|
|
284
|
+
built.calls.push({ method: 'addItem', args: [input] });
|
|
285
|
+
const into = (input.currency as string | undefined) ?? (current?.currency as string) ?? 'ISK';
|
|
286
|
+
if (into !== 'ISK') throw iskOnly();
|
|
287
|
+
current = cartAnswer('ISK');
|
|
288
|
+
return current;
|
|
289
|
+
}) as typeof built.client.cart.addItem;
|
|
290
|
+
built.client.cart.setCurrency = (async (cartId: string, currency: string) => {
|
|
291
|
+
built.calls.push({ method: 'setCurrency', args: [cartId, currency] });
|
|
292
|
+
if (currency !== 'ISK') throw iskOnly();
|
|
293
|
+
current = cartAnswer('ISK');
|
|
294
|
+
return current;
|
|
295
|
+
}) as typeof built.client.cart.setCurrency;
|
|
296
|
+
return built;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
it('keeps their ISK cart after one refused move, quietly, with no picker', async () => {
|
|
300
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
301
|
+
const { client, calls } = businessClient(cartAnswer('ISK'));
|
|
302
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
303
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
304
|
+
await storefront.currency.start(async () => null);
|
|
305
|
+
await vi.waitFor(() => expect(storefront.currency.ownCurrencyOnly.value).toBe(true));
|
|
306
|
+
|
|
307
|
+
expect(storefront.currency.active.value).toBe('ISK');
|
|
308
|
+
expect(storefront.currency.requested.value).toBeUndefined();
|
|
309
|
+
expect(storefront.currency.offered.value).toEqual(['ISK']);
|
|
310
|
+
expect(storefront.currency.failure.value).toBeNull();
|
|
311
|
+
expect(storefront.currency.movedTo.value).toBeNull();
|
|
312
|
+
|
|
313
|
+
await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
314
|
+
expect(calls.filter((call) => call.method === 'setCurrency')).toHaveLength(1);
|
|
315
|
+
expect(storefront.cart.currency.value).toBe('ISK');
|
|
316
|
+
});
|
|
317
|
+
|
|
318
|
+
it('creates their cart in ISK when the default is refused for them', async () => {
|
|
319
|
+
installStorage();
|
|
320
|
+
const { client, calls } = businessClient(null);
|
|
321
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
322
|
+
await storefront.currency.start(async () => null);
|
|
323
|
+
|
|
324
|
+
const failure = await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
325
|
+
|
|
326
|
+
expect(failure).toBeNull();
|
|
327
|
+
expect(calls.map((call) => (call.args[0] as { currency?: string }).currency)).toEqual([
|
|
328
|
+
'EUR',
|
|
329
|
+
undefined,
|
|
330
|
+
]);
|
|
331
|
+
expect(storefront.cart.currency.value).toBe('ISK');
|
|
332
|
+
expect(storefront.currency.offered.value).toEqual(['ISK']);
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
it('moves a signed-in business customer’s cart to ISK when the theme says so', async () => {
|
|
336
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
337
|
+
const { client, calls } = businessClient(cartAnswer('EUR'));
|
|
338
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
339
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
340
|
+
await storefront.currency.start(async () => null);
|
|
341
|
+
storefront.currency.keepOwnCurrency();
|
|
342
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
343
|
+
|
|
344
|
+
expect(calls).toEqual([{ method: 'setCurrency', args: ['cart-1', 'ISK'] }]);
|
|
345
|
+
expect(storefront.currency.active.value).toBe('ISK');
|
|
346
|
+
expect(storefront.currency.offered.value).toEqual(['ISK']);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it('says why when they pick a currency the store keeps from them', async () => {
|
|
350
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
351
|
+
const { client } = businessClient(cartAnswer('ISK'));
|
|
352
|
+
const storefront = createGatewayStorefront(client, {
|
|
353
|
+
route: route(),
|
|
354
|
+
commerce: { ...ISK_OFF, currencies: ['ISK', 'EUR'], defaultCurrency: 'ISK' },
|
|
355
|
+
});
|
|
356
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
357
|
+
await storefront.currency.start(async () => null);
|
|
358
|
+
storefront.currency.choose('EUR');
|
|
359
|
+
await vi.waitFor(() => expect(storefront.currency.failure.value).not.toBeNull());
|
|
360
|
+
expect(failureMessageKey(storefront.currency.failure.value)).toBe(
|
|
361
|
+
'storefront.businessOwnCurrency'
|
|
362
|
+
);
|
|
363
|
+
expect(storefront.currency.offered.value).toEqual(['ISK']);
|
|
364
|
+
expect(storefront.cart.currency.value).toBe('ISK');
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('moves their cart to ISK when a pick of theirs is refused, and keeps ISK', async () => {
|
|
368
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
369
|
+
const { client } = businessClient(cartAnswer('EUR'));
|
|
370
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
371
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
372
|
+
await storefront.currency.start(async () => null);
|
|
373
|
+
storefront.currency.choose('USD');
|
|
374
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
375
|
+
expect(storefront.currency.offered.value).toEqual(['ISK']);
|
|
376
|
+
expect(storefront.currency.active.value).toBe('ISK');
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
it('moves their cart to ISK and adds again when an add to it is refused', async () => {
|
|
380
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
381
|
+
const { client, calls } = businessClient(cartAnswer('EUR'));
|
|
382
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
383
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
384
|
+
await storefront.currency.start(async () => null);
|
|
385
|
+
const failure = await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
386
|
+
expect(failure).toBeNull();
|
|
387
|
+
expect(calls.map((call) => call.method)).toEqual(['addItem', 'setCurrency', 'addItem']);
|
|
388
|
+
expect(storefront.cart.currency.value).toBe('ISK');
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('keeps no ISK choice for whoever uses the browser next', async () => {
|
|
392
|
+
const data = installStorage({ [CART_KEY]: 'cart-1' });
|
|
393
|
+
const { client } = businessClient(cartAnswer('EUR'));
|
|
394
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
395
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
396
|
+
await storefront.currency.start(async () => null);
|
|
397
|
+
storefront.currency.keepOwnCurrency();
|
|
398
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
399
|
+
expect(data.has(PREFERENCE_KEY)).toBe(false);
|
|
400
|
+
});
|
|
401
|
+
|
|
402
|
+
it('goes back to what the store offers when the store sees no business customer', async () => {
|
|
403
|
+
installStorage();
|
|
404
|
+
const { client, calls } = shopClient();
|
|
405
|
+
client.cart.addItem = (async (input: Record<string, unknown>) => {
|
|
406
|
+
calls.push({ method: 'addItem', args: [input] });
|
|
407
|
+
return cartAnswer((input.currency as string | undefined) ?? 'EUR');
|
|
408
|
+
}) as typeof client.cart.addItem;
|
|
409
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
410
|
+
storefront.currency.keepOwnCurrency();
|
|
411
|
+
await storefront.currency.start(async () => null);
|
|
412
|
+
await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
413
|
+
await vi.waitFor(() => expect(storefront.currency.ownCurrencyOnly.value).toBe(false));
|
|
414
|
+
expect(storefront.currency.offered.value).toEqual(['EUR', 'USD']);
|
|
415
|
+
expect(storefront.currency.failure.value).toBeNull();
|
|
416
|
+
await storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
417
|
+
expect(calls.filter((call) => call.method === 'setCurrency')).toHaveLength(1);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
it('lets go on sign-out and moves an ISK cart to the default', async () => {
|
|
421
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
422
|
+
const { client } = businessClient(cartAnswer('ISK'));
|
|
423
|
+
client.cart.setCurrency = (async (_cartId: string, currency: string) =>
|
|
424
|
+
cartAnswer(currency)) as typeof client.cart.setCurrency;
|
|
425
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
426
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('ISK'));
|
|
427
|
+
storefront.currency.keepOwnCurrency();
|
|
428
|
+
await storefront.currency.start(async () => null);
|
|
429
|
+
expect(storefront.cart.currency.value).toBe('ISK');
|
|
430
|
+
storefront.currency.keepOwnCurrency(false);
|
|
431
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
432
|
+
expect(storefront.currency.offered.value).toEqual(['EUR', 'USD']);
|
|
433
|
+
});
|
|
434
|
+
|
|
435
|
+
it('ignores a refusal that answers for a visitor who has signed out since', async () => {
|
|
436
|
+
installStorage({ [CART_KEY]: 'cart-1' });
|
|
437
|
+
const { client } = businessClient(cartAnswer('EUR'));
|
|
438
|
+
let refuse: (() => void) | null = null;
|
|
439
|
+
client.cart.setCurrency = (() =>
|
|
440
|
+
new Promise((_, reject) => {
|
|
441
|
+
refuse = () => reject(iskOnly());
|
|
442
|
+
})) as typeof client.cart.setCurrency;
|
|
443
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
444
|
+
await vi.waitFor(() => expect(storefront.cart.currency.value).toBe('EUR'));
|
|
445
|
+
await storefront.currency.start(async () => null);
|
|
446
|
+
storefront.currency.choose('USD');
|
|
447
|
+
await vi.waitFor(() => expect(refuse).not.toBeNull());
|
|
448
|
+
storefront.currency.keepOwnCurrency(false);
|
|
449
|
+
refuse!();
|
|
450
|
+
await vi.waitFor(() => expect(storefront.currency.busy.value).toBe(false));
|
|
451
|
+
expect(storefront.currency.ownCurrencyOnly.value).toBe(false);
|
|
452
|
+
expect(storefront.currency.offered.value).toEqual(['EUR', 'USD']);
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
it('neither keeps nor retries in ISK when an add answers after the visitor signed out', async () => {
|
|
456
|
+
installStorage();
|
|
457
|
+
const { client, calls } = businessClient(null);
|
|
458
|
+
let refuse: (() => void) | null = null;
|
|
459
|
+
client.cart.addItem = ((input: Record<string, unknown>) => {
|
|
460
|
+
calls.push({ method: 'addItem', args: [input] });
|
|
461
|
+
return new Promise((_, reject) => {
|
|
462
|
+
refuse = () => reject(iskOnly());
|
|
463
|
+
});
|
|
464
|
+
}) as typeof client.cart.addItem;
|
|
465
|
+
const storefront = createGatewayStorefront(client, { route: route(), commerce: ISK_OFF });
|
|
466
|
+
await storefront.currency.start(async () => null);
|
|
467
|
+
const adding = storefront.cart.add({ productId: 'p1', variantId: 'v1', quantity: 1 });
|
|
468
|
+
await vi.waitFor(() => expect(refuse).not.toBeNull());
|
|
469
|
+
storefront.currency.keepOwnCurrency(false);
|
|
470
|
+
refuse!();
|
|
471
|
+
await adding;
|
|
472
|
+
expect(storefront.currency.ownCurrencyOnly.value).toBe(false);
|
|
473
|
+
expect(calls).toHaveLength(1);
|
|
474
|
+
});
|
|
475
|
+
});
|