|
| 1 | +/*! |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/* global describe, test, expect, beforeEach, afterEach, jest */ |
| 21 | + |
| 22 | +import React from 'react'; |
| 23 | +import { renderHook } from '@testing-library/react-hooks'; |
| 24 | +import { QueryClient, QueryClientProvider } from 'react-query'; |
| 25 | +import nock from 'nock'; |
| 26 | + |
| 27 | +import useTasks from './useTasks'; |
| 28 | +import * as metaUtils from '../../utils'; |
| 29 | + |
| 30 | +const Wrapper = ({ children }) => { |
| 31 | + const queryClient = new QueryClient({ |
| 32 | + defaultOptions: { |
| 33 | + queries: { |
| 34 | + retry: 0, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }); |
| 38 | + return ( |
| 39 | + <QueryClientProvider client={queryClient}> |
| 40 | + {children} |
| 41 | + </QueryClientProvider> |
| 42 | + ); |
| 43 | +}; |
| 44 | + |
| 45 | +const fakeUrl = 'http://fake.api'; |
| 46 | + |
| 47 | +describe('Test useTasks hook', () => { |
| 48 | + let spy; |
| 49 | + beforeEach(() => { |
| 50 | + spy = jest.spyOn(metaUtils, 'getMetaValue').mockReturnValue(`${fakeUrl}`); |
| 51 | + }); |
| 52 | + |
| 53 | + afterEach(() => { |
| 54 | + spy.mockRestore(); |
| 55 | + nock.cleanAll(); |
| 56 | + }); |
| 57 | + |
| 58 | + test('initialData works normally', async () => { |
| 59 | + const scope = nock(fakeUrl) |
| 60 | + .get('/') |
| 61 | + .reply(200, { totalEntries: 1, tasks: [{ taskId: 'task_id' }] }); |
| 62 | + const { result, waitFor } = renderHook(() => useTasks(), { wrapper: Wrapper }); |
| 63 | + |
| 64 | + expect(result.current.data.totalEntries).toBe(0); |
| 65 | + |
| 66 | + await waitFor(() => result.current.isFetching); |
| 67 | + |
| 68 | + expect(result.current.data.totalEntries).toBe(0); |
| 69 | + |
| 70 | + await waitFor(() => !result.current.isFetching); |
| 71 | + |
| 72 | + expect(result.current.data.totalEntries).toBe(1); |
| 73 | + scope.done(); |
| 74 | + }); |
| 75 | + |
| 76 | + test('initialData persists even if there is an error', async () => { |
| 77 | + const scope = nock(fakeUrl) |
| 78 | + .get('/') |
| 79 | + .replyWithError('something awful happened'); |
| 80 | + const { result, waitFor } = renderHook(() => useTasks(), { wrapper: Wrapper }); |
| 81 | + |
| 82 | + expect(result.current.data.totalEntries).toBe(0); |
| 83 | + |
| 84 | + await waitFor(() => result.current.isError); |
| 85 | + |
| 86 | + expect(result.current.data.totalEntries).toBe(0); |
| 87 | + scope.done(); |
| 88 | + }); |
| 89 | +}); |
0 commit comments