|
| 1 | +import { MockedProvider, MockedResponse } from '@apollo/client/testing'; |
| 2 | +import { act, renderHook, waitFor } from '@testing-library/react'; |
| 3 | +import { PropsWithChildren } from 'react'; |
| 4 | +import { |
| 5 | + CreateRemoteWebhookDocument, |
| 6 | + DeleteRemoteWebhookDocument, |
| 7 | + RemoteWebhooksDocument, |
| 8 | + TestEditingRemoteWebhookDocument, |
| 9 | + TestRemoteWebhookDocument, |
| 10 | + UpdateRemoteWebhookDocument, |
| 11 | +} from '@/lib/notifyCompRemoteGraphql'; |
| 12 | +import { useNotifyCompRemoteWebhooks } from './useNotifyCompRemoteWebhooks'; |
| 13 | + |
| 14 | +const competitionId = 'ExampleComp2026'; |
| 15 | + |
| 16 | +const webhooksMock = (webhooks: unknown[]): MockedResponse => ({ |
| 17 | + request: { |
| 18 | + query: RemoteWebhooksDocument, |
| 19 | + variables: { competitionId }, |
| 20 | + }, |
| 21 | + result: { |
| 22 | + data: { |
| 23 | + competition: { |
| 24 | + __typename: 'Competition', |
| 25 | + id: competitionId, |
| 26 | + webhooks, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +const createWrapper = (mocks: MockedResponse[]) => |
| 33 | + function MockedApolloWrapper({ children }: PropsWithChildren) { |
| 34 | + return <MockedProvider mocks={mocks}>{children}</MockedProvider>; |
| 35 | + }; |
| 36 | + |
| 37 | +describe('useNotifyCompRemoteWebhooks', () => { |
| 38 | + it('loads competition webhooks', async () => { |
| 39 | + const { result } = renderHook(() => useNotifyCompRemoteWebhooks({ competitionId }), { |
| 40 | + wrapper: createWrapper([ |
| 41 | + webhooksMock([ |
| 42 | + { |
| 43 | + __typename: 'Webhook', |
| 44 | + id: 1, |
| 45 | + method: 'POST', |
| 46 | + url: 'https://example.com/notify', |
| 47 | + }, |
| 48 | + ]), |
| 49 | + ]), |
| 50 | + }); |
| 51 | + |
| 52 | + await waitFor(() => { |
| 53 | + expect(result.current.webhooks).toHaveLength(1); |
| 54 | + }); |
| 55 | + |
| 56 | + expect(result.current.webhooks[0]).toMatchObject({ |
| 57 | + method: 'POST', |
| 58 | + url: 'https://example.com/notify', |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('creates, updates, and deletes webhooks with NotifyComp variables', async () => { |
| 63 | + const createVariables = { |
| 64 | + competitionId, |
| 65 | + webhook: { |
| 66 | + method: 'POST' as const, |
| 67 | + url: 'https://example.com/created', |
| 68 | + }, |
| 69 | + }; |
| 70 | + const updateVariables = { |
| 71 | + id: 4, |
| 72 | + webhook: { |
| 73 | + method: 'GET' as const, |
| 74 | + url: 'https://example.com/updated', |
| 75 | + }, |
| 76 | + }; |
| 77 | + |
| 78 | + const { result } = renderHook(() => useNotifyCompRemoteWebhooks({ competitionId }), { |
| 79 | + wrapper: createWrapper([ |
| 80 | + webhooksMock([]), |
| 81 | + { |
| 82 | + request: { |
| 83 | + query: CreateRemoteWebhookDocument, |
| 84 | + variables: createVariables, |
| 85 | + }, |
| 86 | + result: { |
| 87 | + data: { |
| 88 | + createWebhook: { |
| 89 | + __typename: 'Webhook', |
| 90 | + id: 4, |
| 91 | + ...createVariables.webhook, |
| 92 | + }, |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + webhooksMock([ |
| 97 | + { |
| 98 | + __typename: 'Webhook', |
| 99 | + id: 4, |
| 100 | + ...createVariables.webhook, |
| 101 | + }, |
| 102 | + ]), |
| 103 | + { |
| 104 | + request: { |
| 105 | + query: UpdateRemoteWebhookDocument, |
| 106 | + variables: updateVariables, |
| 107 | + }, |
| 108 | + result: { |
| 109 | + data: { |
| 110 | + updateWebhook: { |
| 111 | + __typename: 'Webhook', |
| 112 | + id: 4, |
| 113 | + ...updateVariables.webhook, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + webhooksMock([ |
| 119 | + { |
| 120 | + __typename: 'Webhook', |
| 121 | + id: 4, |
| 122 | + ...updateVariables.webhook, |
| 123 | + }, |
| 124 | + ]), |
| 125 | + { |
| 126 | + request: { |
| 127 | + query: DeleteRemoteWebhookDocument, |
| 128 | + variables: { id: 4 }, |
| 129 | + }, |
| 130 | + result: { |
| 131 | + data: { |
| 132 | + deleteWebhook: null, |
| 133 | + }, |
| 134 | + }, |
| 135 | + }, |
| 136 | + webhooksMock([]), |
| 137 | + ]), |
| 138 | + }); |
| 139 | + |
| 140 | + await waitFor(() => { |
| 141 | + expect(result.current.isLoading).toBe(false); |
| 142 | + }); |
| 143 | + |
| 144 | + await act(async () => { |
| 145 | + await result.current.saveWebhook(createVariables.webhook); |
| 146 | + }); |
| 147 | + await act(async () => { |
| 148 | + await result.current.saveWebhook(updateVariables.webhook, 4); |
| 149 | + }); |
| 150 | + await act(async () => { |
| 151 | + await result.current.removeWebhook(4); |
| 152 | + }); |
| 153 | + |
| 154 | + expect(result.current.error).toBeNull(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('tests saved and unsaved webhook settings', async () => { |
| 158 | + const webhook = { |
| 159 | + method: 'PUT' as const, |
| 160 | + url: 'https://example.com/test', |
| 161 | + }; |
| 162 | + const { result } = renderHook(() => useNotifyCompRemoteWebhooks({ competitionId }), { |
| 163 | + wrapper: createWrapper([ |
| 164 | + webhooksMock([]), |
| 165 | + { |
| 166 | + request: { |
| 167 | + query: TestRemoteWebhookDocument, |
| 168 | + variables: { id: 8 }, |
| 169 | + }, |
| 170 | + result: { |
| 171 | + data: { |
| 172 | + testWebhook: { |
| 173 | + __typename: 'WebhookResponse', |
| 174 | + body: 'ok', |
| 175 | + status: 200, |
| 176 | + statusText: 'OK', |
| 177 | + url: webhook.url, |
| 178 | + }, |
| 179 | + }, |
| 180 | + }, |
| 181 | + }, |
| 182 | + { |
| 183 | + request: { |
| 184 | + query: TestEditingRemoteWebhookDocument, |
| 185 | + variables: { competitionId, webhook }, |
| 186 | + }, |
| 187 | + result: { |
| 188 | + data: { |
| 189 | + testEditingWebhook: { |
| 190 | + __typename: 'WebhookResponse', |
| 191 | + body: 'failed', |
| 192 | + status: 500, |
| 193 | + statusText: 'Server Error', |
| 194 | + url: webhook.url, |
| 195 | + }, |
| 196 | + }, |
| 197 | + }, |
| 198 | + }, |
| 199 | + ]), |
| 200 | + }); |
| 201 | + |
| 202 | + await waitFor(() => { |
| 203 | + expect(result.current.isLoading).toBe(false); |
| 204 | + }); |
| 205 | + |
| 206 | + await act(async () => { |
| 207 | + await result.current.testSavedWebhook(8); |
| 208 | + }); |
| 209 | + |
| 210 | + expect(result.current.testResult).toMatchObject({ |
| 211 | + status: 200, |
| 212 | + statusText: 'OK', |
| 213 | + }); |
| 214 | + |
| 215 | + await act(async () => { |
| 216 | + await result.current.testWebhookSettings(webhook); |
| 217 | + }); |
| 218 | + |
| 219 | + expect(result.current.testResult).toMatchObject({ |
| 220 | + status: 500, |
| 221 | + statusText: 'Server Error', |
| 222 | + }); |
| 223 | + }); |
| 224 | +}); |
0 commit comments