Skip to content

Commit 7b77645

Browse files
committed
feat: Add test cases for useDefaults composable
1 parent 34554da commit 7b77645

File tree

4 files changed

+90
-3
lines changed

4 files changed

+90
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { useDefaults } from '@packages/composables/useDefaults';
3+
import { PICKER_MODE } from '@packages/constants';
4+
import { getHours } from 'date-fns';
5+
6+
describe('useDefaults', () => {
7+
it('Should get default text input option', () => {
8+
const { getDefaultTextInputOptions } = useDefaults();
9+
10+
const disabledOptions = getDefaultTextInputOptions(false);
11+
expect(disabledOptions).toHaveProperty('enabled', false);
12+
13+
const enabledOptions = getDefaultTextInputOptions(true);
14+
expect(enabledOptions).toHaveProperty('enabled', true);
15+
16+
const mergedOptions = getDefaultTextInputOptions({ enterSubmit: false, tabSubmit: false });
17+
expect(mergedOptions).toHaveProperty('enterSubmit', false);
18+
expect(mergedOptions).toHaveProperty('tabSubmit', false);
19+
});
20+
21+
it('Should get default pattern', () => {
22+
const { getDefaultPattern } = useDefaults();
23+
24+
const pattern = getDefaultPattern(PICKER_MODE.DATE_PICKER, undefined, { is24: true }, undefined);
25+
expect(pattern).toEqual('MM/dd/yyyy, HH:mm');
26+
27+
const withoutTimePattern = getDefaultPattern(PICKER_MODE.DATE_PICKER, undefined, false, undefined);
28+
expect(withoutTimePattern).toEqual('MM/dd/yyyy');
29+
30+
const customPattern = getDefaultPattern(PICKER_MODE.DATE_PICKER, 'dd/yyyy', { is24: true }, undefined);
31+
expect(customPattern).toEqual('dd/yyyy');
32+
33+
const defaultedPattern = getDefaultPattern(PICKER_MODE.DATE_PICKER, () => '', { is24: true }, undefined);
34+
expect(defaultedPattern).toEqual('MM/dd/yyyy, HH:mm');
35+
36+
const monthPickerFormat = getDefaultPattern(PICKER_MODE.MONTH_PICKER, undefined, true, undefined);
37+
expect(monthPickerFormat).toEqual('MM/yyyy');
38+
39+
const yearPickerFormat = getDefaultPattern(PICKER_MODE.YEAR_PICKER, undefined, true, undefined);
40+
expect(yearPickerFormat).toEqual('yyyy');
41+
42+
const weekPickerFormat = getDefaultPattern(PICKER_MODE.WEEK_PICKER, undefined, true, undefined);
43+
expect(weekPickerFormat).toEqual('ww-RR');
44+
45+
const quarterPickerFormat = getDefaultPattern(PICKER_MODE.QUARTER_PICKER, undefined, true, undefined);
46+
expect(quarterPickerFormat).toEqual('QQQ/yyyy');
47+
48+
const timePickerFormat = getDefaultPattern(PICKER_MODE.TIME_PICKER, undefined, true, undefined);
49+
expect(timePickerFormat).toEqual('HH:mm');
50+
51+
const timePickerSeconds = getDefaultPattern(
52+
PICKER_MODE.TIME_PICKER,
53+
undefined,
54+
{ enableSeconds: true },
55+
undefined,
56+
);
57+
expect(timePickerSeconds).toEqual('HH:mm:ss');
58+
});
59+
60+
it('Should get default start time', () => {
61+
const { getDefaultStartTime } = useDefaults();
62+
63+
const time = getDefaultStartTime(false, { startTime: { hours: 1, minutes: 2 } });
64+
65+
const currentHours = getHours(new Date());
66+
67+
expect(time).toHaveProperty('hours', 1);
68+
expect(time).toHaveProperty('minutes', 2);
69+
70+
const minutesOnly = getDefaultStartTime(false, { startTime: { minutes: 15 } });
71+
72+
expect(minutesOnly).toHaveProperty('hours', currentHours);
73+
expect(minutesOnly).toHaveProperty('minutes', 15);
74+
});
75+
76+
it('Should get default filters', () => {
77+
const { getDefaultFilters } = useDefaults();
78+
79+
const filters = getDefaultFilters(undefined);
80+
81+
expect(filters).toHaveProperty('months', []);
82+
expect(filters).toHaveProperty('years', []);
83+
84+
const filterMonths = getDefaultFilters({ months: [0, 1] });
85+
expect(filterMonths).toHaveProperty('months', [0, 1]);
86+
});
87+
});

src/packages/composables/useDefaults.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export const useDefaults = () => {
5353
const defaultOptions = {
5454
is24: true,
5555
enableMinutes: true,
56-
enableSeconds: true,
56+
enableSeconds: false,
5757
startTime: undefined,
5858
};
5959
if (typeof opts === 'object') {

src/packages/types/props.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export interface TimeOptions {
4040
enableSeconds: boolean;
4141
enableMinutes: boolean;
4242
is24: boolean;
43-
startTime?: Time;
43+
startTime?: Partial<Time>;
4444
}
4545

4646
export type TimeProp = boolean | Partial<TimeOptions>;

src/packages/utils/date.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const assignTime = (time: Partial<Time>) => {
77
const defaultTime = {
88
hours: getHours(getDate()),
99
minutes: getMinutes(getDate()),
10-
seconds: getSeconds(getDate()) ?? 0,
10+
seconds: getSeconds(getDate()),
1111
};
1212
return Object.assign(defaultTime, time);
1313
};

0 commit comments

Comments
 (0)