|
| 1 | +import project from '../../../testUtils/mockApi/project'; |
| 2 | +import { mockBusinessTagsData } from '../../../testUtils/mockApi/project/data'; |
| 3 | +import { renderHooksWithRedux } from '@actiontech/shared/lib/testUtil/customRender'; |
| 4 | +import useBusinessTag from '..'; |
| 5 | +import { act, cleanup } from '@testing-library/react'; |
| 6 | +import { |
| 7 | + createSpyErrorResponse, |
| 8 | + createSpyFailResponse |
| 9 | +} from '@actiontech/shared/lib/testUtil/mockApi'; |
| 10 | + |
| 11 | +describe('base/hooks/useBusinessTag', () => { |
| 12 | + let listBusinessTagsSpy: jest.SpyInstance; |
| 13 | + beforeEach(() => { |
| 14 | + jest.useFakeTimers(); |
| 15 | + listBusinessTagsSpy = project.listBusinessTags(); |
| 16 | + }); |
| 17 | + |
| 18 | + afterEach(() => { |
| 19 | + jest.useRealTimers(); |
| 20 | + cleanup(); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should get business tag data from request', async () => { |
| 24 | + const { result } = renderHooksWithRedux(useBusinessTag, {}); |
| 25 | + |
| 26 | + expect(result.current.loading).toBeFalsy(); |
| 27 | + expect(result.current.businessTagList).toEqual([]); |
| 28 | + expect(result.current.businessTagOptions).toEqual([]); |
| 29 | + |
| 30 | + act(() => { |
| 31 | + result.current.updateBusinessTagList(); |
| 32 | + }); |
| 33 | + |
| 34 | + expect(result.current.loading).toBeTruthy(); |
| 35 | + expect(listBusinessTagsSpy).toHaveBeenCalledTimes(1); |
| 36 | + expect(listBusinessTagsSpy).toHaveBeenCalledWith({ |
| 37 | + page_size: 1000 |
| 38 | + }); |
| 39 | + |
| 40 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 41 | + |
| 42 | + expect(result.current.loading).toBeFalsy(); |
| 43 | + expect(result.current.businessTagList).toEqual(mockBusinessTagsData); |
| 44 | + expect(result.current.businessTagOptions).toEqual( |
| 45 | + mockBusinessTagsData.map((businessTag) => ({ |
| 46 | + label: businessTag.name, |
| 47 | + value: businessTag.uid |
| 48 | + })) |
| 49 | + ); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should set businessTagList to empty array when response code is not equal success code', async () => { |
| 53 | + listBusinessTagsSpy.mockClear(); |
| 54 | + listBusinessTagsSpy.mockImplementation(() => |
| 55 | + createSpyFailResponse({ data: { business_tags: [] } }) |
| 56 | + ); |
| 57 | + |
| 58 | + const { result } = renderHooksWithRedux(useBusinessTag, {}); |
| 59 | + act(() => result.current.updateBusinessTagList()); |
| 60 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 61 | + expect(result.current.businessTagList).toEqual([]); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should set businessTagList to empty array when response throw error', async () => { |
| 65 | + listBusinessTagsSpy.mockClear(); |
| 66 | + listBusinessTagsSpy.mockImplementation(() => |
| 67 | + createSpyErrorResponse({ data: [] }) |
| 68 | + ); |
| 69 | + const { result } = renderHooksWithRedux(useBusinessTag, {}); |
| 70 | + act(() => result.current.updateBusinessTagList()); |
| 71 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 72 | + expect(result.current.businessTagList).toEqual([]); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should set businessTagList to empty array when request failed', async () => { |
| 76 | + listBusinessTagsSpy.mockClear(); |
| 77 | + listBusinessTagsSpy.mockImplementation(() => |
| 78 | + createSpyFailResponse({ data: [] }) |
| 79 | + ); |
| 80 | + const { result } = renderHooksWithRedux(useBusinessTag, {}); |
| 81 | + act(() => result.current.updateBusinessTagList()); |
| 82 | + await act(async () => jest.advanceTimersByTime(3000)); |
| 83 | + expect(result.current.businessTagList).toEqual([]); |
| 84 | + }); |
| 85 | +}); |
0 commit comments