Skip to content

Commit fec9bf5

Browse files
committed
test: Unify duplicated code
1 parent 02810fd commit fec9bf5

File tree

1 file changed

+17
-37
lines changed

1 file changed

+17
-37
lines changed

tests/unit/utils.spec.ts

+17-37
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ import type { AllPropsType } from '@/props';
5757
import { defaultMultiCalendars, defaultTransitions, mapPropDates } from '@/utils/defaults';
5858
import { de } from 'date-fns/locale';
5959
import { localToTz } from '@/utils/timezone';
60-
import type { TimeZoneConfig } from '@/interfaces';
60+
import type { IDefaultSelect, TimeZoneConfig } from '@/interfaces';
6161

6262
const getCurrentTime = () => {
6363
return {
@@ -85,6 +85,13 @@ const getMapDatesOpts = (date: Date, timezone: TimeZoneConfig, highlightFn: (dat
8585
};
8686
};
8787

88+
const validateMonthNames = (months: IDefaultSelect[], values: string[]) => {
89+
expect(months).toHaveLength(12);
90+
expect(months[0].text).toEqual(values[0]);
91+
expect(months[1].text).toEqual(values[1]);
92+
expect(months[2].text).toEqual(values[2]);
93+
};
94+
8895
describe('Utils and date utils formatting', () => {
8996
it('Should group array by 3', () => {
9097
const items = getArrayInArray(Array.from(Array(10).keys()));
@@ -124,58 +131,31 @@ describe('Utils and date utils formatting', () => {
124131

125132
it('Should get long month values according to locale', () => {
126133
const months = getMonths(null, 'en', 'long');
127-
128-
expect(months).toHaveLength(12);
129-
expect(months[0].text).toEqual('January');
130-
expect(months[1].text).toEqual('February');
131-
expect(months[2].text).toEqual('March');
134+
validateMonthNames(months, ['January', 'February', 'March']);
132135
});
133136

134137
it('Should get short month values according to locale', () => {
135138
const months = getMonths(null, 'en', 'short');
136-
137-
expect(months).toHaveLength(12);
138-
expect(months[0].text).toEqual('Jan');
139-
expect(months[1].text).toEqual('Feb');
140-
expect(months[2].text).toEqual('Mar');
139+
validateMonthNames(months, ['Jan', 'Feb', 'Mar']);
141140
});
142141

143-
it('Should get long month values according to formatLocale', () => {
142+
it('Should get long month values according to formatLocale and fallback locale', () => {
144143
const months = getMonths(de, 'en', 'long');
145-
146-
expect(months).toHaveLength(12);
147-
expect(months[0].text).toEqual('Januar');
148-
expect(months[1].text).toEqual('Februar');
149-
expect(months[2].text).toEqual('März');
150-
});
151-
152-
it('Should get long month values by fallback to locale', () => {
153-
// Pass incorrect formatLocale
154-
const months = getMonths(null, 'de', 'long');
155-
156-
expect(months).toHaveLength(12);
157-
expect(months[0].text).toEqual('Januar');
158-
expect(months[1].text).toEqual('Februar');
159-
expect(months[2].text).toEqual('März');
144+
const namesArr = ['Januar', 'Februar', 'März'];
145+
validateMonthNames(months, namesArr);
146+
const monthsWithFallback = getMonths(null, 'de', 'long');
147+
validateMonthNames(monthsWithFallback, namesArr);
160148
});
161149

162150
it('Should get short month values according to formatLocale', () => {
163151
const months = getMonths(de, 'en', 'short');
164-
165-
expect(months).toHaveLength(12);
166-
expect(months[0].text).toEqual('Jan');
167-
expect(months[1].text).toEqual('Feb');
168-
expect(months[2].text).toEqual('Mär');
152+
validateMonthNames(months, ['Jan', 'Feb', 'Mär']);
169153
});
170154

171155
it('Should get short month values by fallback to locale', () => {
172156
// @ts-expect-error Pass incorrect formatLocale
173157
const months = getMonths({}, 'de', 'short');
174-
175-
expect(months).toHaveLength(12);
176-
expect(months[0].text).toEqual('Jan');
177-
expect(months[1].text).toEqual('Feb');
178-
expect(months[2].text).toEqual('Mär');
158+
validateMonthNames(months, ['Jan', 'Feb', 'Mär']);
179159
});
180160

181161
it('Should get default pattern', () => {

0 commit comments

Comments
 (0)