Skip to content

Commit 656e701

Browse files
chore: add tests
1 parent ed74ea6 commit 656e701

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

packages/use-intl/test/core/createFormatter.test.tsx

+40-28
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,74 @@
1-
import {parseISO} from 'date-fns';
2-
import {it, expect} from 'vitest';
3-
import {createFormatter} from '../../src';
1+
import { parseISO } from 'date-fns';
2+
import { it, expect } from 'vitest';
3+
import { createFormatter } from '../../src';
44

55
it('formats a date and time', () => {
66
const formatter = createFormatter({
77
locale: 'en',
8-
timeZone: 'Europe/Berlin'
8+
timeZone: 'Europe/Berlin',
99
});
1010
expect(
1111
formatter.dateTime(parseISO('2020-11-20T10:36:01.516Z'), {
12-
dateStyle: 'medium'
12+
dateStyle: 'medium',
13+
})
14+
).toBe('Nov 20, 2020');
15+
});
16+
17+
it('formats a ISO 8601 datetime string', () => {
18+
const formatter = createFormatter({
19+
locale: 'en',
20+
timeZone: 'Europe/Berlin',
21+
});
22+
expect(
23+
formatter.dateTime('2020-11-20T10:36:01.516Z', {
24+
dateStyle: 'medium',
1325
})
1426
).toBe('Nov 20, 2020');
1527
});
1628

1729
it('formats a number', () => {
1830
const formatter = createFormatter({
1931
locale: 'en',
20-
timeZone: 'Europe/Berlin'
32+
timeZone: 'Europe/Berlin',
2133
});
2234
expect(formatter.number(123456)).toBe('123,456');
2335
});
2436

2537
it('formats a bigint', () => {
2638
const formatter = createFormatter({
2739
locale: 'en',
28-
timeZone: 'Europe/Berlin'
40+
timeZone: 'Europe/Berlin',
2941
});
3042
expect(formatter.number(123456789123456789n)).toBe('123,456,789,123,456,789');
3143
});
3244

3345
it('formats a number as currency', () => {
3446
const formatter = createFormatter({
3547
locale: 'en',
36-
timeZone: 'Europe/Berlin'
48+
timeZone: 'Europe/Berlin',
3749
});
3850
expect(
39-
formatter.number(123456.789, {style: 'currency', currency: 'USD'})
51+
formatter.number(123456.789, { style: 'currency', currency: 'USD' })
4052
).toBe('$123,456.79');
4153
});
4254

4355
it('formats a bigint as currency', () => {
4456
const formatter = createFormatter({
4557
locale: 'en',
46-
timeZone: 'Europe/Berlin'
58+
timeZone: 'Europe/Berlin',
4759
});
4860
expect(
4961
formatter.number(123456789123456789n, {
5062
style: 'currency',
51-
currency: 'USD'
63+
currency: 'USD',
5264
})
5365
).toBe('$123,456,789,123,456,789.00');
5466
});
5567

5668
it('formats a relative time with the second unit', () => {
5769
const formatter = createFormatter({
5870
locale: 'en',
59-
timeZone: 'Europe/Berlin'
71+
timeZone: 'Europe/Berlin',
6072
});
6173
expect(
6274
formatter.relativeTime(
@@ -69,7 +81,7 @@ it('formats a relative time with the second unit', () => {
6981
it('formats a relative time with the minute unit', () => {
7082
const formatter = createFormatter({
7183
locale: 'en',
72-
timeZone: 'Europe/Berlin'
84+
timeZone: 'Europe/Berlin',
7385
});
7486
expect(
7587
formatter.relativeTime(
@@ -82,7 +94,7 @@ it('formats a relative time with the minute unit', () => {
8294
it('formats a relative time with the hour unit', () => {
8395
const formatter = createFormatter({
8496
locale: 'en',
85-
timeZone: 'Europe/Berlin'
97+
timeZone: 'Europe/Berlin',
8698
});
8799
expect(
88100
formatter.relativeTime(
@@ -95,7 +107,7 @@ it('formats a relative time with the hour unit', () => {
95107
it('formats a relative time with the day unit', () => {
96108
const formatter = createFormatter({
97109
locale: 'en',
98-
timeZone: 'Europe/Berlin'
110+
timeZone: 'Europe/Berlin',
99111
});
100112
expect(
101113
formatter.relativeTime(
@@ -108,7 +120,7 @@ it('formats a relative time with the day unit', () => {
108120
it('formats a relative time with the month unit', () => {
109121
const formatter = createFormatter({
110122
locale: 'en',
111-
timeZone: 'Europe/Berlin'
123+
timeZone: 'Europe/Berlin',
112124
});
113125
expect(
114126
formatter.relativeTime(
@@ -121,7 +133,7 @@ it('formats a relative time with the month unit', () => {
121133
it('formats a relative time with the year unit', () => {
122134
const formatter = createFormatter({
123135
locale: 'en',
124-
timeZone: 'Europe/Berlin'
136+
timeZone: 'Europe/Berlin',
125137
});
126138
expect(
127139
formatter.relativeTime(
@@ -134,7 +146,7 @@ it('formats a relative time with the year unit', () => {
134146
it('supports the future relative time', () => {
135147
const formatter = createFormatter({
136148
locale: 'en',
137-
timeZone: 'Europe/Berlin'
149+
timeZone: 'Europe/Berlin',
138150
});
139151
expect(
140152
formatter.relativeTime(
@@ -147,25 +159,25 @@ it('supports the future relative time', () => {
147159
it('formats a relative time with options', () => {
148160
const formatter = createFormatter({
149161
locale: 'en',
150-
timeZone: 'Europe/Berlin'
162+
timeZone: 'Europe/Berlin',
151163
});
152164
expect(
153165
formatter.relativeTime(parseISO('2020-11-20T08:30:00.000Z'), {
154166
now: parseISO('2020-11-20T10:36:00.000Z'),
155-
unit: 'day'
167+
unit: 'day',
156168
})
157169
).toBe('today');
158170
});
159171

160172
it('supports the quarter unit', () => {
161173
const formatter = createFormatter({
162174
locale: 'en',
163-
timeZone: 'Europe/Berlin'
175+
timeZone: 'Europe/Berlin',
164176
});
165177
expect(
166178
formatter.relativeTime(parseISO('2020-01-01T00:00:00.000Z'), {
167179
now: parseISO('2020-11-01T01:00:00.000Z'),
168-
unit: 'quarter'
180+
unit: 'quarter',
169181
})
170182
).toBe('3 quarters ago');
171183
});
@@ -174,33 +186,33 @@ it('formats a relative time with a globally defined `now`', () => {
174186
const formatter = createFormatter({
175187
locale: 'en',
176188
now: parseISO('2020-11-20T01:00:00.000Z'),
177-
timeZone: 'Europe/Berlin'
189+
timeZone: 'Europe/Berlin',
178190
});
179191
expect(
180192
formatter.relativeTime(parseISO('2020-11-20T00:00:00.000Z'), {
181-
unit: 'day'
193+
unit: 'day',
182194
})
183195
).toBe('today');
184196
});
185197

186198
it('formats a list', () => {
187199
const formatter = createFormatter({
188200
locale: 'en',
189-
timeZone: 'Europe/Berlin'
201+
timeZone: 'Europe/Berlin',
190202
});
191203
expect(
192-
formatter.list(['apple', 'banana', 'orange'], {type: 'disjunction'})
204+
formatter.list(['apple', 'banana', 'orange'], { type: 'disjunction' })
193205
).toBe('apple, banana, or orange');
194206
});
195207

196208
it('formats a set', () => {
197209
const formatter = createFormatter({
198210
locale: 'en',
199-
timeZone: 'Europe/Berlin'
211+
timeZone: 'Europe/Berlin',
200212
});
201213
expect(
202214
formatter.list(new Set(['apple', 'banana', 'orange']), {
203-
type: 'disjunction'
215+
type: 'disjunction',
204216
})
205217
).toBe('apple, banana, or orange');
206218
});

0 commit comments

Comments
 (0)