|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { ref } from 'vue' |
| 3 | +import { EnhancedDay, Props } from '~/types' |
| 4 | +import { useDatePicker } from './useDatePicker' |
| 5 | + |
| 6 | +// Vueのライフサイクルフックだけをモック |
| 7 | +vi.mock('vue', async (importOriginal) => { |
| 8 | + const actual = (await importOriginal()) as typeof import('vue') |
| 9 | + return { |
| 10 | + ...actual, |
| 11 | + onBeforeUnmount: vi.fn(), // onBeforeUnmountだけをモック |
| 12 | + } |
| 13 | +}) |
| 14 | + |
| 15 | +describe('useDatePicker', () => { |
| 16 | + const mockEmit = vi.fn() |
| 17 | + |
| 18 | + const props: Required<Props> = { |
| 19 | + modelValue: '2023-09-30', |
| 20 | + displayFormat: 'yyyy-MM-dd', |
| 21 | + isDateDisabled: vi.fn(() => false), |
| 22 | + weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'], |
| 23 | + startWeekOnSunday: false, |
| 24 | + yearFirst: false, |
| 25 | + months: [ |
| 26 | + 'January', |
| 27 | + 'February', |
| 28 | + 'March', |
| 29 | + 'April', |
| 30 | + 'May', |
| 31 | + 'June', |
| 32 | + 'July', |
| 33 | + 'August', |
| 34 | + 'September', |
| 35 | + 'October', |
| 36 | + 'November', |
| 37 | + 'December', |
| 38 | + ], |
| 39 | + disabled: false, |
| 40 | + placeholder: 'Please select a date', |
| 41 | + yearContent: '', |
| 42 | + } |
| 43 | + |
| 44 | + const el = ref(null) |
| 45 | + const outerWrap = ref(null) |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + vi.clearAllMocks() |
| 49 | + vi.clearAllTimers() |
| 50 | + }) |
| 51 | + |
| 52 | + it('should initialize correctly', () => { |
| 53 | + const { inputValue, currentPeriod, opened } = useDatePicker( |
| 54 | + props, |
| 55 | + el, |
| 56 | + outerWrap, |
| 57 | + mockEmit |
| 58 | + ) |
| 59 | + |
| 60 | + expect(inputValue.value).toBe('2023-09-30') |
| 61 | + expect(currentPeriod.value).toEqual({ month: 8, year: 2023 }) |
| 62 | + expect(opened.value).toBe(false) |
| 63 | + }) |
| 64 | + |
| 65 | + it('should open the date picker', () => { |
| 66 | + const { openUpdated, opened } = useDatePicker( |
| 67 | + props, |
| 68 | + el, |
| 69 | + outerWrap, |
| 70 | + mockEmit |
| 71 | + ) |
| 72 | + |
| 73 | + openUpdated() |
| 74 | + expect(opened.value).toBe(true) |
| 75 | + }) |
| 76 | + |
| 77 | + it('should close the date picker', () => { |
| 78 | + const { closeUpdated, opened } = useDatePicker( |
| 79 | + props, |
| 80 | + el, |
| 81 | + outerWrap, |
| 82 | + mockEmit |
| 83 | + ) |
| 84 | + |
| 85 | + closeUpdated() |
| 86 | + expect(opened.value).toBe(false) |
| 87 | + }) |
| 88 | + |
| 89 | + it('should update the modelValue when a valid date is selected', () => { |
| 90 | + const { selectDateItem } = useDatePicker(props, el, outerWrap, mockEmit) |
| 91 | + |
| 92 | + const item: EnhancedDay = { |
| 93 | + date: new Date('2023-10-01'), |
| 94 | + disabled: false, |
| 95 | + } |
| 96 | + selectDateItem(item) |
| 97 | + |
| 98 | + expect(mockEmit).toHaveBeenCalledWith('update:modelValue', '2023-10-01') |
| 99 | + }) |
| 100 | + |
| 101 | + it('should not select a disabled date', () => { |
| 102 | + const { selectDateItem } = useDatePicker(props, el, outerWrap, mockEmit) |
| 103 | + |
| 104 | + const item: EnhancedDay = { |
| 105 | + date: new Date('2023-10-01'), |
| 106 | + disabled: true, |
| 107 | + } |
| 108 | + selectDateItem(item) |
| 109 | + |
| 110 | + expect(mockEmit).not.toHaveBeenCalled() |
| 111 | + }) |
| 112 | + |
| 113 | + it('should handle input correctly', () => { |
| 114 | + const { handleInput, handleFocusOut } = useDatePicker( |
| 115 | + props, |
| 116 | + el, |
| 117 | + outerWrap, |
| 118 | + mockEmit |
| 119 | + ) |
| 120 | + |
| 121 | + const inputElement = document.createElement('input') |
| 122 | + inputElement.value = '2023-10-01' |
| 123 | + const event = { target: inputElement } as unknown as Event |
| 124 | + handleInput(event) |
| 125 | + handleFocusOut() |
| 126 | + |
| 127 | + expect(mockEmit).toHaveBeenCalledWith('update:modelValue', '2023-10-01') |
| 128 | + }) |
| 129 | + |
| 130 | + it('should handle focus out and emit correct value', async () => { |
| 131 | + const { handleFocusOut, inputValue } = useDatePicker( |
| 132 | + props, |
| 133 | + el, |
| 134 | + outerWrap, |
| 135 | + mockEmit |
| 136 | + ) |
| 137 | + |
| 138 | + inputValue.value = '2023-10-01' |
| 139 | + handleFocusOut() |
| 140 | + |
| 141 | + expect(mockEmit).toHaveBeenCalledWith('update:modelValue', '2023-10-01') |
| 142 | + }) |
| 143 | + |
| 144 | + it('should close via overlay click', () => { |
| 145 | + const { closeViaOverlay, opened } = useDatePicker( |
| 146 | + props, |
| 147 | + el, |
| 148 | + outerWrap, |
| 149 | + mockEmit |
| 150 | + ) |
| 151 | + |
| 152 | + const event = { target: outerWrap.value } as Event |
| 153 | + closeViaOverlay(event) |
| 154 | + |
| 155 | + expect(opened.value).toBe(false) |
| 156 | + }) |
| 157 | +}) |
0 commit comments