|
| 1 | +import { act } from '@testing-library/react'; |
| 2 | + |
| 3 | +import { useQueryClient } from '@tanstack/react-query'; |
| 4 | +import { HttpResponse } from 'msw'; |
| 5 | + |
| 6 | +import { renderHook, waitFor } from '@/test-utils/rtl'; |
| 7 | + |
| 8 | +import useCreateSchedule from '../use-create-schedule'; |
| 9 | +import { type UseCreateScheduleVariables } from '../use-create-schedule.types'; |
| 10 | + |
| 11 | +jest.mock('@tanstack/react-query', () => ({ |
| 12 | + ...jest.requireActual('@tanstack/react-query'), |
| 13 | + useQueryClient: jest.fn(), |
| 14 | +})); |
| 15 | + |
| 16 | +const MOCK_DOMAIN = 'test-domain'; |
| 17 | +const MOCK_CLUSTER = 'test-cluster'; |
| 18 | + |
| 19 | +const VALID_CREATE_SCHEDULE_BODY: UseCreateScheduleVariables = { |
| 20 | + scheduleId: 'my-schedule', |
| 21 | + spec: { |
| 22 | + cronExpression: '0 9 * * *', |
| 23 | + }, |
| 24 | + action: { |
| 25 | + startWorkflow: { |
| 26 | + workflowType: { name: 'DemoWorkflow' }, |
| 27 | + taskList: { name: 'demo-task-list' }, |
| 28 | + workerSDKLanguage: 'GO', |
| 29 | + workflowIdPrefix: 'scheduled-demo-', |
| 30 | + executionStartToCloseTimeoutSeconds: 3600, |
| 31 | + }, |
| 32 | + }, |
| 33 | +}; |
| 34 | + |
| 35 | +describe(useCreateSchedule.name, () => { |
| 36 | + let mockInvalidateQueries: jest.Mock; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + mockInvalidateQueries = jest.fn(); |
| 40 | + (useQueryClient as jest.Mock).mockReturnValue({ |
| 41 | + invalidateQueries: mockInvalidateQueries, |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + it('exposes isPending as false initially', () => { |
| 46 | + const { result } = setup({}); |
| 47 | + |
| 48 | + expect(result.current.isPending).toBe(false); |
| 49 | + }); |
| 50 | + |
| 51 | + it('sets isPending to true while mutation is in-flight', async () => { |
| 52 | + let resolveRequest: (() => void) | undefined; |
| 53 | + const blockingPromise = new Promise<void>((resolve) => { |
| 54 | + resolveRequest = resolve; |
| 55 | + }); |
| 56 | + |
| 57 | + const { result } = setup({ |
| 58 | + httpResolver: async () => { |
| 59 | + await blockingPromise; |
| 60 | + return HttpResponse.json({}); |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + act(() => { |
| 65 | + result.current.mutate(VALID_CREATE_SCHEDULE_BODY); |
| 66 | + }); |
| 67 | + |
| 68 | + await waitFor(() => { |
| 69 | + expect(result.current.isPending).toBe(true); |
| 70 | + }); |
| 71 | + |
| 72 | + act(() => { |
| 73 | + resolveRequest?.(); |
| 74 | + }); |
| 75 | + |
| 76 | + await waitFor(() => { |
| 77 | + expect(result.current.isPending).toBe(false); |
| 78 | + }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('invalidates listSchedules queries on success', async () => { |
| 82 | + const { result } = setup({}); |
| 83 | + |
| 84 | + await act(async () => { |
| 85 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 86 | + }); |
| 87 | + |
| 88 | + expect(mockInvalidateQueries).toHaveBeenCalledWith( |
| 89 | + expect.objectContaining({ |
| 90 | + queryKey: [ |
| 91 | + 'listSchedules', |
| 92 | + { domain: MOCK_DOMAIN, cluster: MOCK_CLUSTER }, |
| 93 | + ], |
| 94 | + }) |
| 95 | + ); |
| 96 | + }); |
| 97 | + |
| 98 | + it('sets isSuccess to true after successful mutation', async () => { |
| 99 | + const { result } = setup({}); |
| 100 | + |
| 101 | + await act(async () => { |
| 102 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 103 | + }); |
| 104 | + |
| 105 | + expect(result.current.isSuccess).toBe(true); |
| 106 | + }); |
| 107 | + |
| 108 | + it('surfaces error from API on 400 failure', async () => { |
| 109 | + const { result } = setup({ |
| 110 | + httpResolver: async () => |
| 111 | + HttpResponse.json( |
| 112 | + { |
| 113 | + message: 'Invalid values provided for schedule create', |
| 114 | + validationErrors: [ |
| 115 | + { code: 'too_small', path: ['scheduleId'], message: 'Too small' }, |
| 116 | + ], |
| 117 | + }, |
| 118 | + { status: 400 } |
| 119 | + ), |
| 120 | + }); |
| 121 | + |
| 122 | + await act(async () => { |
| 123 | + try { |
| 124 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 125 | + } catch { |
| 126 | + // expected to throw |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + await waitFor(() => { |
| 131 | + expect(result.current.error).not.toBeNull(); |
| 132 | + }); |
| 133 | + |
| 134 | + expect(result.current.error?.status).toBe(400); |
| 135 | + expect(result.current.error?.message).toBe( |
| 136 | + 'Invalid values provided for schedule create' |
| 137 | + ); |
| 138 | + expect(result.current.error?.validationErrors).toHaveLength(1); |
| 139 | + }); |
| 140 | + |
| 141 | + it('surfaces error from API on 409 conflict', async () => { |
| 142 | + const { result } = setup({ |
| 143 | + httpResolver: async () => |
| 144 | + HttpResponse.json( |
| 145 | + { message: 'Schedule already exists' }, |
| 146 | + { status: 409 } |
| 147 | + ), |
| 148 | + }); |
| 149 | + |
| 150 | + await act(async () => { |
| 151 | + try { |
| 152 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 153 | + } catch { |
| 154 | + // expected to throw |
| 155 | + } |
| 156 | + }); |
| 157 | + |
| 158 | + await waitFor(() => { |
| 159 | + expect(result.current.error).not.toBeNull(); |
| 160 | + }); |
| 161 | + |
| 162 | + expect(result.current.error?.status).toBe(409); |
| 163 | + expect(result.current.error?.message).toBe('Schedule already exists'); |
| 164 | + }); |
| 165 | + |
| 166 | + it('resets mutation state after calling reset()', async () => { |
| 167 | + const { result } = setup({ |
| 168 | + httpResolver: async () => |
| 169 | + HttpResponse.json( |
| 170 | + { message: 'Schedule already exists' }, |
| 171 | + { status: 409 } |
| 172 | + ), |
| 173 | + }); |
| 174 | + |
| 175 | + await act(async () => { |
| 176 | + try { |
| 177 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 178 | + } catch { |
| 179 | + // expected to throw |
| 180 | + } |
| 181 | + }); |
| 182 | + |
| 183 | + await waitFor(() => { |
| 184 | + expect(result.current.error).not.toBeNull(); |
| 185 | + }); |
| 186 | + |
| 187 | + await act(async () => { |
| 188 | + result.current.reset(); |
| 189 | + }); |
| 190 | + |
| 191 | + await waitFor(() => { |
| 192 | + expect(result.current.error).toBeNull(); |
| 193 | + }); |
| 194 | + expect(result.current.isSuccess).toBe(false); |
| 195 | + }); |
| 196 | + |
| 197 | + it('does not invalidate queries on failure', async () => { |
| 198 | + const { result } = setup({ |
| 199 | + httpResolver: async () => |
| 200 | + HttpResponse.json( |
| 201 | + { message: 'Schedule already exists' }, |
| 202 | + { status: 409 } |
| 203 | + ), |
| 204 | + }); |
| 205 | + |
| 206 | + await act(async () => { |
| 207 | + try { |
| 208 | + await result.current.mutateAsync(VALID_CREATE_SCHEDULE_BODY); |
| 209 | + } catch { |
| 210 | + // expected to throw |
| 211 | + } |
| 212 | + }); |
| 213 | + |
| 214 | + await waitFor(() => { |
| 215 | + expect(result.current.error).not.toBeNull(); |
| 216 | + }); |
| 217 | + |
| 218 | + expect(mockInvalidateQueries).not.toHaveBeenCalled(); |
| 219 | + }); |
| 220 | +}); |
| 221 | + |
| 222 | +function setup({ |
| 223 | + httpResolver, |
| 224 | +}: { |
| 225 | + httpResolver?: () => ReturnType<typeof HttpResponse.json> | Promise<ReturnType<typeof HttpResponse.json>>; |
| 226 | +} = {}) { |
| 227 | + const { result } = renderHook( |
| 228 | + () => useCreateSchedule({ domain: MOCK_DOMAIN, cluster: MOCK_CLUSTER }), |
| 229 | + { |
| 230 | + endpointsMocks: [ |
| 231 | + { |
| 232 | + path: `/api/domains/${MOCK_DOMAIN}/${MOCK_CLUSTER}/schedules`, |
| 233 | + httpMethod: 'POST', |
| 234 | + mockOnce: false, |
| 235 | + httpResolver: httpResolver ?? (async () => HttpResponse.json({})), |
| 236 | + }, |
| 237 | + ], |
| 238 | + } |
| 239 | + ); |
| 240 | + |
| 241 | + return { result }; |
| 242 | +} |
0 commit comments