Skip to content

Commit d70289e

Browse files
WS-Temporal-PR-3: Refactor tests
1 parent ed5eac2 commit d70289e

2 files changed

Lines changed: 170 additions & 115 deletions

File tree

src/app/legacy/psammead/psammead-timestamp-container/src/utilities/temporalHelpers/index.test.ts

Lines changed: 169 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -2,144 +2,199 @@ import {
22
sanitiseDuration,
33
sanitiseLocale,
44
withArabicComma,
5+
translateDigits,
56
applyFormat,
67
} from '.';
78

8-
describe('sanitiseDuration', () => {
9-
describe('valid duration input', () => {
10-
it.each([
11-
['PT0S', 0],
12-
['PT45S', 45],
13-
['PT30M', 1800],
14-
['PT1H30M45S', 5445],
15-
])('parses %s to %i total seconds', (duration, expectedSeconds) => {
16-
expect(sanitiseDuration(duration).total({ unit: 'seconds' })).toEqual(
17-
expectedSeconds,
9+
describe('Temporal Helper functions', () => {
10+
describe('sanitiseDuration', () => {
11+
describe('valid duration input', () => {
12+
it.each([
13+
['PT0S', 0],
14+
['PT45S', 45],
15+
['PT30M', 1800],
16+
['PT1H30M45S', 5445],
17+
])('parses %s to %i total seconds', (duration, expectedSeconds) => {
18+
expect(sanitiseDuration(duration).total({ unit: 'seconds' })).toEqual(
19+
expectedSeconds,
20+
);
21+
});
22+
});
23+
24+
describe('invalid duration input', () => {
25+
it.each(['garbage', ''])(
26+
'falls back to 0 seconds for invalid duration %p',
27+
duration => {
28+
expect(sanitiseDuration(duration).total({ unit: 'seconds' })).toEqual(
29+
0,
30+
);
31+
},
1832
);
1933
});
20-
});
2134

22-
describe('invalid duration input', () => {
23-
it.each(['garbage', ''])(
24-
'falls back to 0 seconds for invalid duration %p',
25-
duration => {
26-
expect(sanitiseDuration(duration).total({ unit: 'seconds' })).toEqual(
35+
it('falls back to 0 seconds when Temporal is unavailable', () => {
36+
const originalTemporal = globalThis.Temporal;
37+
try {
38+
// @ts-expect-error - simulating an environment without Temporal support
39+
delete globalThis.Temporal;
40+
expect(sanitiseDuration('PT1H30M').total({ unit: 'seconds' })).toEqual(
2741
0,
2842
);
29-
},
30-
);
43+
} finally {
44+
globalThis.Temporal = originalTemporal;
45+
}
46+
});
3147
});
3248

33-
it('falls back to 0 seconds when Temporal is unavailable', () => {
34-
const originalTemporal = globalThis.Temporal;
35-
try {
36-
// @ts-expect-error - simulating an environment without Temporal support
37-
delete globalThis.Temporal;
38-
expect(sanitiseDuration('PT1H30M').total({ unit: 'seconds' })).toEqual(0);
39-
} finally {
40-
globalThis.Temporal = originalTemporal;
41-
}
49+
describe('sanitiseLocale', () => {
50+
describe('valid locale input', () => {
51+
it.each([
52+
['en-GB', 'en-GB'],
53+
['en-gb', 'en-GB'],
54+
['en_GB', 'en-GB'],
55+
['ar', 'ar'],
56+
['fa', 'fa'],
57+
['fa_af', 'fa-AF'],
58+
['fa-AF', 'fa-AF'],
59+
['fa-af', 'fa-AF'],
60+
])(
61+
'normalises %p to %p so the output is a valid BCP 47 language tag',
62+
(locale, expected) => {
63+
expect(sanitiseLocale(locale)).toEqual(expected);
64+
},
65+
);
66+
});
67+
68+
describe('invalid locale input', () => {
69+
it('falls back to en-GB for an unparseable locale', () => {
70+
expect(sanitiseLocale('not a locale!!')).toEqual('en-GB');
71+
});
72+
});
4273
});
43-
});
4474

45-
describe('sanitiseLocale', () => {
46-
describe('valid locale input', () => {
75+
describe('withArabicComma', () => {
76+
it('replaces commas with the Arabic comma', () => {
77+
expect(withArabicComma('1,234,567')).toEqual('1،234،567');
78+
});
79+
80+
it('replaces multiple adjacent commas', () => {
81+
expect(withArabicComma('1,,2')).toEqual('1،،2');
82+
});
83+
84+
it('returns the string unchanged when it has no commas', () => {
85+
expect(withArabicComma('no commas here')).toEqual('no commas here');
86+
});
87+
});
88+
89+
describe('translateDigits', () => {
4790
it.each([
48-
['en-GB', 'en-GB'],
49-
['en-gb', 'en-GB'],
50-
['en_GB', 'en-GB'],
51-
['ar', 'ar'],
52-
['fa', 'fa'],
53-
['fa_af', 'fa-AF'],
54-
['fa-AF', 'fa-AF'],
55-
['fa-af', 'fa-AF'],
91+
[5, 1, '5'],
92+
[5, 2, '05'],
93+
[5, 3, '005'],
94+
[12, 2, '12'],
5695
])(
57-
'normalises %p to %p so the output is a valid BCP 47 language tag',
58-
(locale, expected) => {
59-
expect(sanitiseLocale(locale)).toEqual(expected);
96+
'formats %i with minDigits %i as %p for en-GB',
97+
(value, minDigits, expected) => {
98+
expect(translateDigits(value, minDigits, 'en-GB')).toEqual(expected);
6099
},
61100
);
62-
});
63101

64-
describe('invalid locale input', () => {
65-
it('falls back to en-GB for an unparseable locale', () => {
66-
expect(sanitiseLocale('not a locale!!')).toEqual('en-GB');
102+
it.each([
103+
['my', '၀၅'],
104+
['bn', '০৫'],
105+
['ne', '०५'],
106+
['fa', '۰۵'],
107+
['fa-AF', '۰۵'],
108+
['ps', '۰۵'],
109+
])('translates digits into the %p numeral system', (locale, expected) => {
110+
expect(translateDigits(5, 2, locale)).toEqual(expected);
67111
});
68112
});
69-
});
70113

71-
describe('withArabicComma', () => {
72-
it('replaces commas with the Arabic comma', () => {
73-
expect(withArabicComma('1,234,567')).toEqual('1،234،567');
74-
});
114+
describe('applyFormat', () => {
115+
describe('when format is provided', () => {
116+
it.each([
117+
['mm,ss', '05,09'],
118+
['h:mm:ss', '1:05:09'],
119+
['m', '5'],
120+
])('replaces the %p format token', (format, expected) => {
121+
expect(
122+
applyFormat({
123+
format,
124+
hours: 1,
125+
minutes: 5,
126+
seconds: 9,
127+
sanitisedLocale: 'en-GB',
128+
}),
129+
).toEqual(expected);
130+
});
75131

76-
it('replaces multiple adjacent commas', () => {
77-
expect(withArabicComma('1,,2')).toEqual('1،،2');
78-
});
79-
80-
it('returns the string unchanged when it has no commas', () => {
81-
expect(withArabicComma('no commas here')).toEqual('no commas here');
82-
});
83-
});
132+
it.each([
133+
['mm,ss', '۰۵,۰۹'],
134+
['h:mm:ss', '۱:۰۵:۰۹'],
135+
['m', '۵'],
136+
])(
137+
'replaces the %p format token for a non en-GB locale',
138+
(format, expected) => {
139+
expect(
140+
applyFormat({
141+
format,
142+
hours: 1,
143+
minutes: 5,
144+
seconds: 9,
145+
sanitisedLocale: 'fa',
146+
}),
147+
).toEqual(expected);
148+
},
149+
);
150+
});
84151

85-
describe('applyFormat', () => {
86-
it('uses mm:ss when hours is 0', () => {
87-
expect(
88-
applyFormat({
89-
hours: 0,
90-
minutes: 5,
91-
seconds: 9,
92-
sanitisedLocale: 'en-GB',
93-
}),
94-
).toEqual('05:09');
95-
});
152+
describe('when format is not provided', () => {
153+
it('uses mm:ss when hours is 0', () => {
154+
expect(
155+
applyFormat({
156+
hours: 0,
157+
minutes: 5,
158+
seconds: 9,
159+
sanitisedLocale: 'en-GB',
160+
}),
161+
).toEqual('05:09');
162+
});
96163

97-
it('uses h:mm:ss when hours is greater than 0', () => {
98-
expect(
99-
applyFormat({
100-
hours: 1,
101-
minutes: 5,
102-
seconds: 9,
103-
sanitisedLocale: 'en-GB',
104-
}),
105-
).toEqual('1:05:09');
106-
});
164+
it('uses h:mm:ss when hours is greater than 0', () => {
165+
expect(
166+
applyFormat({
167+
hours: 1,
168+
minutes: 5,
169+
seconds: 9,
170+
sanitisedLocale: 'en-GB',
171+
}),
172+
).toEqual('1:05:09');
173+
});
107174

108-
it('pads minutes and seconds to 2 digits at the zero boundary', () => {
109-
expect(
110-
applyFormat({
111-
hours: 0,
112-
minutes: 0,
113-
seconds: 0,
114-
sanitisedLocale: 'en-GB',
115-
}),
116-
).toEqual('00:00');
117-
});
118-
119-
it.each([
120-
['mm,ss', '05,09'],
121-
['h:mm:ss', '1:05:09'],
122-
['m', '5'],
123-
])('replaces the %p format token', (format, expected) => {
124-
expect(
125-
applyFormat({
126-
format,
127-
hours: 1,
128-
minutes: 5,
129-
seconds: 9,
130-
sanitisedLocale: 'en-GB',
131-
}),
132-
).toEqual(expected);
133-
});
175+
it('pads minutes and seconds to 2 digits at the zero boundary', () => {
176+
expect(
177+
applyFormat({
178+
hours: 0,
179+
minutes: 0,
180+
seconds: 0,
181+
sanitisedLocale: 'en-GB',
182+
}),
183+
).toEqual('00:00');
184+
});
134185

135-
it.each([
136-
['my', '၀၅:၀၉'],
137-
['bn', '০৫:০৯'],
138-
['ne', '०५:०९'],
139-
['fa', '۰۵:۰۹'],
140-
])('translates digits for locale %p', (sanitisedLocale, expected) => {
141-
expect(
142-
applyFormat({ hours: 0, minutes: 5, seconds: 9, sanitisedLocale }),
143-
).toEqual(expected);
186+
it.each([
187+
['my', '၀၅:၀၉'],
188+
['bn', '০৫:০৯'],
189+
['ne', '०५:०९'],
190+
['fa', '۰۵:۰۹'],
191+
['fa-AF', '۰۵:۰۹'],
192+
['ps', '۰۵:۰۹'],
193+
])('translates digits for locale %p', (sanitisedLocale, expected) => {
194+
expect(
195+
applyFormat({ hours: 0, minutes: 5, seconds: 9, sanitisedLocale }),
196+
).toEqual(expected);
197+
});
198+
});
144199
});
145200
});

src/app/legacy/psammead/psammead-timestamp-container/src/utilities/temporalHelpers/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const withArabicComma = (string: string) => {
2828
return string.replace(/,/g, '،');
2929
};
3030

31-
const translateDigits = (
31+
export const translateDigits = (
3232
value: number,
3333
minDigits: number,
3434
sanitisedLocale: Locale,

0 commit comments

Comments
 (0)