|
| 1 | +import { describe, it, expect, jest, beforeEach } from '@jest/globals'; |
| 2 | +import { buildCloseClientPayload, CloseClientFormValue, isValidDate } from './close-client.utils'; |
| 3 | + |
| 4 | +describe('buildCloseClientPayload', () => { |
| 5 | + const DEFAULT_LOCALE = 'en'; |
| 6 | + const DEFAULT_DATE_FORMAT = 'dd MMMM yyyy'; |
| 7 | + const MOCK_FORMATTED_DATE = '03 November 2025'; |
| 8 | + |
| 9 | + let formatDateSpy: jest.MockedFunction<(date: Date, format: string) => string>; |
| 10 | + |
| 11 | + const mockDateUtils = { |
| 12 | + formatDate: (date: Date, format: string) => formatDateSpy(date, format) |
| 13 | + }; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + formatDateSpy = jest.fn(() => MOCK_FORMATTED_DATE); |
| 17 | + }); |
| 18 | + |
| 19 | + const makeValidForm = (overrides: Partial<CloseClientFormValue> = {}): CloseClientFormValue => ({ |
| 20 | + closureDate: new Date(2025, 10, 3), |
| 21 | + closureReasonId: 1, |
| 22 | + ...overrides |
| 23 | + }); |
| 24 | + |
| 25 | + const callBuildPayload = ( |
| 26 | + formValue: CloseClientFormValue, |
| 27 | + options?: { |
| 28 | + locale?: string; |
| 29 | + dateFormat?: string; |
| 30 | + } |
| 31 | + ) => |
| 32 | + buildCloseClientPayload( |
| 33 | + formValue, |
| 34 | + mockDateUtils, |
| 35 | + options?.locale ?? DEFAULT_LOCALE, |
| 36 | + options?.dateFormat ?? DEFAULT_DATE_FORMAT |
| 37 | + ); |
| 38 | + |
| 39 | + describe('Valid inputs', () => { |
| 40 | + it('formats Date closureDate', () => { |
| 41 | + const date = new Date(2025, 10, 3); |
| 42 | + |
| 43 | + const result = callBuildPayload(makeValidForm({ closureDate: date })); |
| 44 | + |
| 45 | + expect(formatDateSpy).toHaveBeenCalledWith(date, DEFAULT_DATE_FORMAT); |
| 46 | + expect(formatDateSpy).toHaveBeenCalledTimes(1); |
| 47 | + expect(result.closureDate).toBe(MOCK_FORMATTED_DATE); |
| 48 | + }); |
| 49 | + |
| 50 | + it('passes through pre-formatted string closureDate', () => { |
| 51 | + const preFormattedDate = '03 November 2025'; |
| 52 | + |
| 53 | + const result = callBuildPayload(makeValidForm({ closureDate: preFormattedDate })); |
| 54 | + |
| 55 | + expect(formatDateSpy).not.toHaveBeenCalled(); |
| 56 | + expect(result.closureDate).toBe(preFormattedDate); |
| 57 | + }); |
| 58 | + |
| 59 | + it('supports custom locale', () => { |
| 60 | + const result = callBuildPayload(makeValidForm(), { |
| 61 | + locale: 'fr' |
| 62 | + }); |
| 63 | + |
| 64 | + expect(result.locale).toBe('fr'); |
| 65 | + }); |
| 66 | + |
| 67 | + it('supports custom dateFormat', () => { |
| 68 | + const customFormat = 'yyyy-MM-dd'; |
| 69 | + |
| 70 | + const result = callBuildPayload(makeValidForm(), { |
| 71 | + dateFormat: customFormat |
| 72 | + }); |
| 73 | + |
| 74 | + expect(result.dateFormat).toBe(customFormat); |
| 75 | + }); |
| 76 | + |
| 77 | + it('preserves closureReasonId', () => { |
| 78 | + const result = callBuildPayload(makeValidForm({ closureReasonId: 42 })); |
| 79 | + |
| 80 | + expect(result.closureReasonId).toBe(42); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('Immutability', () => { |
| 85 | + it('does not mutate input object', () => { |
| 86 | + const formValue = makeValidForm(); |
| 87 | + const snapshot = { ...formValue }; |
| 88 | + |
| 89 | + callBuildPayload(formValue); |
| 90 | + |
| 91 | + expect(formValue).toEqual(snapshot); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + describe('closureDate validation', () => { |
| 96 | + it('rejects invalid Date object', () => { |
| 97 | + const invalidDate = new Date('invalid'); |
| 98 | + |
| 99 | + expect(isValidDate(invalidDate)).toBe(false); |
| 100 | + |
| 101 | + expect(() => callBuildPayload(makeValidForm({ closureDate: invalidDate }))).toThrow(TypeError); |
| 102 | + |
| 103 | + expect(formatDateSpy).not.toHaveBeenCalled(); |
| 104 | + }); |
| 105 | + |
| 106 | + it.each([ |
| 107 | + null, |
| 108 | + undefined, |
| 109 | + 12345, |
| 110 | + {} as unknown as Date, |
| 111 | + '' |
| 112 | + ])('rejects invalid closureDate: %p', (value) => { |
| 113 | + expect(() => |
| 114 | + callBuildPayload( |
| 115 | + makeValidForm({ |
| 116 | + closureDate: value as unknown as Date |
| 117 | + }) |
| 118 | + ) |
| 119 | + ).toThrow(TypeError); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('closureReasonId validation', () => { |
| 124 | + it.each([ |
| 125 | + 0, |
| 126 | + -1, |
| 127 | + 1.5, |
| 128 | + Number.NaN |
| 129 | + ])('rejects invalid closureReasonId: %p', (value) => { |
| 130 | + expect(() => |
| 131 | + callBuildPayload( |
| 132 | + makeValidForm({ |
| 133 | + closureReasonId: value |
| 134 | + }) |
| 135 | + ) |
| 136 | + ).toThrow(TypeError); |
| 137 | + }); |
| 138 | + |
| 139 | + it('accepts valid boundary values', () => { |
| 140 | + expect(() => callBuildPayload(makeValidForm({ closureReasonId: 1 }))).not.toThrow(); |
| 141 | + |
| 142 | + expect(() => |
| 143 | + callBuildPayload( |
| 144 | + makeValidForm({ |
| 145 | + closureReasonId: Number.MAX_SAFE_INTEGER |
| 146 | + }) |
| 147 | + ) |
| 148 | + ).not.toThrow(); |
| 149 | + }); |
| 150 | + |
| 151 | + it('rejects missing closureReasonId', () => { |
| 152 | + expect(() => |
| 153 | + callBuildPayload({ |
| 154 | + closureDate: new Date(), |
| 155 | + closureReasonId: undefined as unknown as number |
| 156 | + }) |
| 157 | + ).toThrow(TypeError); |
| 158 | + }); |
| 159 | + }); |
| 160 | + |
| 161 | + describe('isValidDate', () => { |
| 162 | + it('returns true for valid Date objects', () => { |
| 163 | + expect(isValidDate(new Date())).toBe(true); |
| 164 | + expect(isValidDate(new Date(2025, 10, 3))).toBe(true); |
| 165 | + expect(isValidDate(new Date('2025-03-24'))).toBe(true); |
| 166 | + }); |
| 167 | + |
| 168 | + it('returns false for invalid Date objects', () => { |
| 169 | + expect(isValidDate(new Date('invalid'))).toBe(false); |
| 170 | + expect(isValidDate(new Date(Number.NaN))).toBe(false); |
| 171 | + }); |
| 172 | + |
| 173 | + it.each([ |
| 174 | + '2025-03-24', |
| 175 | + 123456, |
| 176 | + null, |
| 177 | + undefined, |
| 178 | + {} |
| 179 | + ])('returns false for non-Date values: %p', (value) => { |
| 180 | + expect(isValidDate(value)).toBe(false); |
| 181 | + }); |
| 182 | + }); |
| 183 | +}); |
0 commit comments