-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsvelte-currency-input.test.ts
551 lines (468 loc) · 25.3 KB
/
svelte-currency-input.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
import { expect, test, type Page } from '@playwright/test';
const DELAY_FOR_FORMATTED_VALUE_IN_MS = 25;
const isMacOs = process.platform === 'darwin';
const selectAll = async (page: Page) => {
isMacOs ? await page.keyboard.press('Meta+A') : await page.keyboard.press('Control+A');
};
// HACK:
// This is a workaround because Playwright starts running the assertions immediately
// after the DOM loads but the component is updated a few milliseconds later.
// This causes a race condition in some tests causing assertions to fail.
//
// The real solution would be to figure out why some fields are not already updated
// when the component is mounted, or why is it triggering a re-render.
// REF: https://github.com/fmaclen/svelte-currency-input/issues/62
const waitForInitialLoad = async (page: Page) => {
const DELAY_IN_MS = 100;
await page.waitForTimeout(DELAY_IN_MS);
}
test.describe('CurrencyInput', () => {
test.beforeEach(async ({ page }) => {
await page.goto('/');
});
test('Default behavior is correct', async ({ page }) => {
// Test field with "zero" value
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
await expect(colonUnformattedInput).not.toBeDisabled();
await expect(colonUnformattedInput).toHaveAttribute('type', 'hidden');
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).not.toBeDisabled();
await expect(colonFormattedInput).toHaveValue('');
await expect(colonFormattedInput).toHaveAttribute('type', 'text');
await expect(colonFormattedInput).toHaveAttribute('placeholder', '₡0,00');
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
await expect(colonFormattedInput).toHaveClass(/currencyInput__formatted--zero/);
// Test field with "positive" value
const yenUnformattedInput = page.locator('.currencyInput__unformatted[name=yen]');
const yenFormattedInput = page.locator('.currencyInput__formatted[name="formatted-yen"]');
await expect(yenUnformattedInput).not.toBeDisabled();
await expect(yenUnformattedInput).toHaveAttribute('type', 'hidden');
await expect(yenUnformattedInput).toHaveValue('5678.9');
await expect(yenFormattedInput).not.toBeDisabled();
await expect(yenFormattedInput).toHaveValue('¥5,678.90');
await expect(yenFormattedInput).toHaveAttribute('type', 'text');
await expect(yenFormattedInput).toHaveAttribute('placeholder', '¥0.00');
await expect(yenFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
await expect(yenFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
await expect(yenFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
// Test field with "negative" value
const defaultUnformattedInput = page.locator('.currencyInput__unformatted[name=default]');
const defaultFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-default"]'
);
await expect(yenUnformattedInput).not.toBeDisabled();
await expect(defaultUnformattedInput).toHaveAttribute('type', 'hidden');
await expect(defaultUnformattedInput).toHaveValue('-42069.69');
await expect(defaultFormattedInput).not.toBeDisabled();
await expect(defaultFormattedInput).toHaveValue('-$42,069.69');
await expect(defaultFormattedInput).toHaveAttribute('type', 'text');
await expect(defaultFormattedInput).toHaveAttribute('placeholder', '$0.00');
await expect(defaultFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
await expect(defaultFormattedInput).toHaveClass(/currencyInput__formatted--negative/);
await expect(defaultFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
// Test field that is "disabled"
const shekelUnformattedInput = page.locator('.currencyInput__unformatted[name=shekel]');
const shekelFormattedInput = page.locator('.currencyInput__formatted[name="formatted-shekel"]');
await expect(shekelUnformattedInput).toBeDisabled();
await expect(shekelUnformattedInput).toHaveAttribute('type', 'hidden');
await expect(shekelUnformattedInput).toHaveValue('97532.95');
await expect(shekelFormattedInput).toHaveValue('₪97,532.95');
await expect(shekelFormattedInput).toBeDisabled();
await expect(shekelFormattedInput).toHaveAttribute('type', 'text');
await expect(shekelFormattedInput).toHaveAttribute('placeholder', '₪0.00');
await expect(shekelFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
await expect(shekelFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
await expect(shekelFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
// Submitting a form returns the correct values
const demoSubmitForm = page.locator('button#submit-form');
const demoOutput = page.locator('pre.demoForm__pre');
await demoSubmitForm.click();
expect(await demoOutput.textContent()).toMatch(
JSON.stringify(
{
default: '-42069.69',
'formatted-default': '-$42,069.69',
colon: '0',
'formatted-colon': '',
pound: '1234.56',
'formatted-pound': '£1,234.56',
bitcoin: '0.87654321',
'formatted-bitcoin': '฿0.87654321',
yen: '5678.9',
'formatted-yen': '¥5,678.90',
euro: '-42069.69',
'formatted-euro': '€ -42.069,69',
won: '0',
'formatted-won': '',
pesos: '999',
'formatted-pesos': '$ 999,00',
rupees: '678',
'formatted-rupees': '₹678.000',
soles: '0',
'formatted-soles': 'S/ 0.00',
dinars: '0',
'formatted-dinars': '',
'chained-east-caribbean-dollar': '10000',
'formatted-chained-east-caribbean-dollar': 'EC$10,000.0000',
'chained-euros': '10000',
'formatted-chained-euros': '€ 10.000,00',
'chained-dollars': '10000',
'formatted-chained-dollars': '$10,000'
},
null,
2
)
);
});
test('Updating an input has the correct behavior', async ({ page }) => {
await waitForInitialLoad(page);
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
// Check the there is no value in the input
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
await colonFormattedInput.focus();
await page.keyboard.type('420,69', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(colonFormattedInput).toHaveValue('₡420,69');
await expect(colonUnformattedInput).toHaveValue('420.69');
await expect(colonFormattedInput).toHaveClass(/currencyInput__formatted--positive/);
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--negative/);
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
// Use arrow keys to go back to the first character
for (let i = 0; i < '₡420,69'.length; i++) await page.keyboard.press('ArrowLeft');
await page.keyboard.type('-');
await expect(colonFormattedInput).toHaveValue('-₡420,69');
await expect(colonUnformattedInput).toHaveValue('-420.69');
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--positive/);
await expect(colonFormattedInput).toHaveClass(/currencyInput__formatted--negative/);
await expect(colonFormattedInput).not.toHaveClass(/currencyInput__formatted--zero/);
// Use right arrow keys to position cusror at the end of the input
for (let i = 0; i < '₡420,69'.length; i++) await page.keyboard.press('ArrowRight');
// Delete the number but keep the currency symbol and sign
for (let i = 1; i < '420,69'.length; i++) await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(colonFormattedInput).toHaveValue('-₡');
// FIXME: at this point the hidden value should be set to 0 but without formatting `colonFormattedInput`
await expect(colonUnformattedInput).toHaveValue('-4');
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(colonFormattedInput).toHaveValue('-');
// FIXME: at this point the hidden value should be set to 0 but without formatting `colonFormattedInput`
await expect(colonUnformattedInput).toHaveValue('-4');
await page.keyboard.type('69,42', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(colonFormattedInput).toHaveValue('-₡69,42');
await expect(colonUnformattedInput).toHaveValue('-69.42');
for (let i = 0; i < '-₡69,42'.length; i++) await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(colonUnformattedInput).toHaveValue('0');
});
test("Incorrect characters can't be entered", async ({ page }) => {
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
// Check the there is no value in the input
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
// Check typing letters doesn't do anything
await colonFormattedInput.focus();
await page.keyboard.type('abc');
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
// Check keyboard combinations don't do anything
await page.keyboard.press('Shift+A');
await expect(colonFormattedInput).toHaveValue('');
// Check keyboard shortcuts are allowed
await page.keyboard.type('420,69', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(colonFormattedInput).toHaveValue('₡420,69');
await expect(colonUnformattedInput).toHaveValue('420.69');
// Check "Backspace" works
await selectAll(page);
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
// Add data to the field again
await page.keyboard.type('-420,69', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(colonFormattedInput).toHaveValue('-₡420,69');
await expect(colonUnformattedInput).toHaveValue('-420.69');
// Check "Delete" also works
await selectAll(page);
await page.keyboard.press('Delete');
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
});
test('Placeholders can be overriden', async ({ page }) => {
// Default placeholder
const defaultFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-default"]'
);
await expect(defaultFormattedInput).toHaveAttribute('placeholder', '$0.00');
// Null placeholder
const poundFormattedInput = page.locator('.currencyInput__formatted[name="formatted-pound"]');
await expect(poundFormattedInput).toHaveAttribute('placeholder', '');
// Overriden placeholder
const wonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-won"]');
await expect(wonFormattedInput).toHaveAttribute('placeholder', '₩1,234.56');
});
test('Fraction digits can be overriden', async ({ page }) => {
await waitForInitialLoad(page);
const bitcoinUnformattedInput = page.locator('.currencyInput__unformatted[name=bitcoin]');
const bitcoinFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-bitcoin"]'
);
await expect(bitcoinUnformattedInput).toHaveValue('0.87654321');
await expect(bitcoinFormattedInput).toHaveValue('฿0.87654321');
await expect(bitcoinFormattedInput).toHaveAttribute('placeholder', '฿0.00000000');
await bitcoinFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(bitcoinUnformattedInput).toHaveValue('0');
await expect(bitcoinFormattedInput).toHaveValue('');
// Decimals beyond the maximum allowed are rounded
await page.keyboard.type('-0.987654329', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(bitcoinUnformattedInput).toHaveValue('-0.98765433');
await expect(bitcoinFormattedInput).toHaveValue('-฿0.98765433');
});
test.describe('Pressing the comma or period keys have the correct behavior', async () => {
test('Pressing "." gets converted to ","', async ({ page }) => {
await waitForInitialLoad(page);
const euroFormattedInput = page.locator('.currencyInput__formatted[name="formatted-euro"]');
const euroUnformattedInput = page.locator('.currencyInput__unformatted[name=euro]');
await euroFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(euroUnformattedInput).toHaveValue('0');
await page.keyboard.type('-111222.33', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(euroFormattedInput).toHaveValue('€ -111.222,33');
await expect(euroUnformattedInput).toHaveValue('-111222.33');
});
test('Pressing "," gets converted to "."', async ({ page }) => {
await waitForInitialLoad(page);
const bitcoinUnformattedInput = page.locator('.currencyInput__unformatted[name=bitcoin]');
const bitcoinFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-bitcoin"]'
);
await bitcoinFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await expect(bitcoinUnformattedInput).toHaveValue('0');
await page.keyboard.type('444555,66', { delay: DELAY_FOR_FORMATTED_VALUE_IN_MS });
await expect(bitcoinFormattedInput).toHaveValue('฿444,555.66');
await expect(bitcoinUnformattedInput).toHaveValue('444555.66');
});
});
test('Formatting is applied on:blur', async ({ page }) => {
const euroFormattedInput = page.locator('.currencyInput__formatted[name="formatted-euro"]');
const euroUnformattedInput = page.locator('.currencyInput__unformatted[name=euro]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
// The old value should remain because `-` doesn't override it
await euroFormattedInput.focus();
await selectAll(page);
await page.keyboard.type('-');
await colonFormattedInput.focus();
await expect(euroFormattedInput).toHaveValue('€ -42.069,69');
await expect(euroUnformattedInput).toHaveValue('-42069.69');
// The value is reset to 0 because Backspace overrides it
await euroFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await page.waitForTimeout(DELAY_FOR_FORMATTED_VALUE_IN_MS);
await page.keyboard.type('-');
await colonFormattedInput.focus();
await expect(euroFormattedInput).toHaveValue('');
await expect(euroUnformattedInput).toHaveValue('0');
});
test('Pressing Tab has the correct behavior', async ({ page }, testInfo) => {
// Tabbing in Webkit is broken: https://github.com/Canutin/svelte-currency-input/issues/40
if (testInfo.project.name !== 'webkit') {
const formattedInputs = page.locator('.currencyInput__formatted');
expect(await formattedInputs.count()).toBe(15);
await formattedInputs.first().focus();
await expect(formattedInputs.nth(0)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(1)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(2)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(3)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(4)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(5)).not.toBeFocused(); // The fifth input is disabled
await expect(formattedInputs.nth(6)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(7)).toBeFocused();
await page.keyboard.press('Tab');
await expect(formattedInputs.nth(8)).toBeFocused();
}
});
test('Class names can be overwritten', async ({ page }) => {
const customWrapperClass = page.locator('.custom-wrapper-class');
const customUnformattedClass = page.locator('.custom-unformatted-class');
await expect(customWrapperClass).toBeVisible();
await expect(customWrapperClass).toHaveClass(/currencyInput/); // We don't override the default class
await expect(customUnformattedClass).toHaveValue('0');
await expect(customUnformattedClass).not.toHaveClass(/currencyInput__unformatted/); // We override the default class
});
test('A callback function is fired when value changes', async ({ page }) => {
const pesosFormattedInput = page.locator('.currencyInput__formatted[name="formatted-pesos"]');
// Prepare to assert and accept dialog
page.on('dialog', (dialog) => {
expect(dialog.message()).not.toMatch('The value for ARS has changed to: 999');
expect(dialog.message()).toMatch('The value for ARS has changed to: 99');
dialog.accept();
});
await expect(pesosFormattedInput).toBeVisible();
await pesosFormattedInput.focus();
await page.keyboard.press('Backspace');
});
test('Autocomplete attribute can be set', async ({ page }) => {
const pesosFormattedInput = page.locator('.currencyInput__formatted[name="formatted-pesos"]');
await expect(pesosFormattedInput).toHaveAttribute('autocomplete', 'off');
});
test('A value with zero cents and more than 1 fraction digits gets formatted on blur', async ({ page }) => {
const rupeesFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rupees"]');
const rupeesUnformattedInput = page.locator('.currencyInput__unformatted[name="rupees"]');
await expect(rupeesFormattedInput).toHaveValue('₹678.000');
await expect(rupeesUnformattedInput).toHaveValue('678');
await rupeesFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await page.keyboard.type('123');
await expect(rupeesFormattedInput).toHaveValue('₹123');
await expect(rupeesFormattedInput).not.toHaveValue('₹123.000');
await page.locator('body').click(); // Click outside the input to trigger formatting
await expect(rupeesFormattedInput).toHaveValue('₹123.000');
await expect(rupeesUnformattedInput).toHaveValue('123');
});
test("isZeroNullish doesn't render placeholder when the value is 0", async ({ page }) => {
const solesUnformattedInput = page.locator('.currencyInput__unformatted[name="soles"]');
const solesFormattedInput = page.locator('.currencyInput__formatted[name="formatted-soles"]');
await expect(solesUnformattedInput).toHaveValue('0');
await expect(solesFormattedInput).toHaveValue('S/ 0.00');
await expect(solesFormattedInput).toHaveAttribute('placeholder', '');
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).not.toHaveValue('₡0,00');
await expect(colonFormattedInput).toHaveAttribute('placeholder', '₡0,00');
});
test("A custom placeholder can be set", async ({ page }) => {
const dinarsUnformattedInput = page.locator('.currencyInput__unformatted[name="dinars"]');
const dinarsFormattedInput = page.locator('.currencyInput__formatted[name="formatted-dinars"]');
await expect(dinarsUnformattedInput).toHaveValue('0');
await expect(dinarsFormattedInput).toHaveValue('');
await expect(dinarsFormattedInput).toHaveAttribute('placeholder', 'How many Dinars?');
});
test('Prevent duplicated decimal points', async ({ page }) => {
// Periods as decimals
const poundUnformattedInput = page.locator('.currencyInput__unformatted[name=pound]');
const poundFormattedInput = page.locator('.currencyInput__formatted[name="formatted-pound"]');
await expect(poundUnformattedInput).toHaveValue('1234.56');
await expect(poundFormattedInput).toHaveValue('£1,234.56');
await poundFormattedInput.focus();
await page.keyboard.type('....');
await expect(poundUnformattedInput).toHaveValue('1234.56');
await expect(poundFormattedInput).toHaveValue('£1,234.56');
// Commas as decimals
const colonUnformattedInput = page.locator('.currencyInput__unformatted[name=colon]');
const colonFormattedInput = page.locator('.currencyInput__formatted[name="formatted-colon"]');
await expect(colonUnformattedInput).toHaveValue('0');
await expect(colonFormattedInput).toHaveValue('');
await colonFormattedInput.focus();
await page.keyboard.type('123,,,,,');
await expect(colonUnformattedInput).toHaveValue('123');
await expect(colonFormattedInput).toHaveValue('₡123,');
// Pressing multiple commas when locale for decimals is a period
const dinarsUnformattedInput = page.locator('.currencyInput__unformatted[name="dinars"]');
const dinarsFormattedInput = page.locator('.currencyInput__formatted[name="formatted-dinars"]');
await expect(dinarsUnformattedInput).toHaveValue('0');
await expect(dinarsFormattedInput).toHaveValue('');
await dinarsFormattedInput.focus();
await page.keyboard.type('123,,,,,');
await expect(dinarsUnformattedInput).toHaveValue('123');
await expect(dinarsFormattedInput).toHaveValue('RSD 123.');
});
test("inputmode is set correctly based on fractionDigits", async ({ page }) => {
// fractionDigits == undefined (defaults to 2)
const solesFormattedInput = page.locator('.currencyInput__formatted[name="formatted-soles"]');
await expect(solesFormattedInput).toHaveAttribute('inputmode', 'decimal');
// fractionDigits == 3
const rupeesFormattedInput = page.locator('.currencyInput__formatted[name="formatted-rupees"]');
await expect(rupeesFormattedInput).toHaveAttribute('inputmode', 'decimal');
// fractionDigits == 0
const dinarsFormattedInput = page.locator('.currencyInput__formatted[name="formatted-dinars"]');
await expect(dinarsFormattedInput).toHaveAttribute('inputmode', 'numeric');
});
test('Updating chained inputs have the correct behavior', async ({ page }) => {
const chainedDollarsUnformattedInput = page.locator(
'.currencyInput__unformatted[name="chained-dollars"]'
);
const chainedDollarsFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-chained-dollars"]'
);
const chainedEurosUnformattedInput = page.locator(
'.currencyInput__unformatted[name="chained-euros"]'
);
const chainedEurosFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-chained-euros"]'
);
const chainedEastCaribbeanDollarUnformattedInput = page.locator(
'.currencyInput__unformatted[name="chained-east-caribbean-dollar"]'
);
const chainedEastCaribbeanDollarFormattedInput = page.locator(
'.currencyInput__formatted[name="formatted-chained-east-caribbean-dollar"]'
);
const chainedValueButton = page.locator('button#set-chained-value');
// The default chained value is `9999.99` but because `chainedDollars` has
// fraction digits set to `0` it gets rounded to `10_000` onMount(),
// thus updating the other chained inputs to `10_000` as well.
await expect(chainedDollarsUnformattedInput).toHaveValue('10000');
await expect(chainedDollarsFormattedInput).toHaveValue('$10,000');
await expect(chainedEurosUnformattedInput).toHaveValue('10000');
await expect(chainedEurosFormattedInput).toHaveValue('€ 10.000,00');
await expect(chainedEastCaribbeanDollarUnformattedInput).toHaveValue('10000');
await expect(chainedEastCaribbeanDollarFormattedInput).toHaveValue('EC$10,000.0000');
// Set a new chained value by clicking a button
await chainedValueButton.click();
// USD input has fraction digits is 0
await expect(chainedDollarsUnformattedInput).toHaveValue('421');
await expect(chainedDollarsFormattedInput).toHaveValue('$421');
// EUR input has fraction digits is 2
await expect(chainedEurosFormattedInput).toHaveValue('€ 420,69');
await expect(chainedEurosUnformattedInput).toHaveValue('420.69');
// XCD input has fraction digits is 4
await expect(chainedEastCaribbeanDollarUnformattedInput).toHaveValue('420.69');
await expect(chainedEastCaribbeanDollarFormattedInput).toHaveValue('EC$420.6900');
// Set a new chained value by deleting the value in the USD input
await chainedDollarsFormattedInput.focus();
await selectAll(page);
await page.keyboard.press('Backspace');
await expect(chainedDollarsUnformattedInput).toHaveValue('0');
await expect(chainedDollarsFormattedInput).toHaveValue('');
await expect(chainedEurosUnformattedInput).toHaveValue('0');
await expect(chainedEurosFormattedInput).toHaveValue('');
await expect(chainedEastCaribbeanDollarUnformattedInput).toHaveValue('0');
await expect(chainedEastCaribbeanDollarFormattedInput).toHaveValue('');
});
test('an id can be set', async ({ page }) => {
const fourTwentySixNineInput = page.locator('#four-twenty-six-nine');
await expect(fourTwentySixNineInput).toBeVisible();
await expect(fourTwentySixNineInput).toHaveValue('-$42,069.69');
});
test('pressing enter submits the form', async ({ page }) => {
const preTag = page.locator('.demoForm__pre');
await expect(preTag).toContainText('Submit form to see a JSON output of the values');
await expect(preTag).not.toContainText('bitcoin');
const allInputs = page.locator('.currencyInput__formatted');
await allInputs.first().focus();
await expect(allInputs.first()).toHaveValue('-$42,069.69');
await page.keyboard.press('Enter');
await expect(preTag).not.toContainText('Submit form to see a JSON output of the values');
await expect(preTag).toContainText('bitcoin');
});
});