File size: 5,517 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
/**
* @jest-environment jsdom
*/
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { renderHook, waitFor } from '@testing-library/react';
import React from 'react';
import { act } from 'react-dom/test-utils';
import { SiteSubscriptionsSortBy } from '../../constants';
import {
SiteSubscriptionsQueryPropsProvider,
useSiteSubscriptionsQueryProps,
} from '../../contexts';
import { callApi, getSubkey } from '../../helpers';
import useSiteSubscriptionsQuery from '../../queries/use-site-subscriptions-query';
jest.mock( '../../helpers', () => ( {
callApi: jest.fn(),
getSubkey: jest.fn(),
} ) );
const queryClient = new QueryClient();
const wrapper = ( { children } ) => (
<QueryClientProvider client={ queryClient }>
<SiteSubscriptionsQueryPropsProvider>{ children }</SiteSubscriptionsQueryPropsProvider>
</QueryClientProvider>
);
describe( 'useSiteSubscriptionsQuery hook', () => {
beforeEach( () => {
queryClient.clear();
jest.resetAllMocks();
( getSubkey as jest.Mock ).mockReturnValue( () => 'test-key' );
} );
it( 'fetches subscriptions data with multiple pages', async () => {
( callApi as jest.Mock ).mockResolvedValueOnce( {
subscriptions: [
{ id: '1', name: 'Site 1', URL: 'https://site1.example.com' },
{ id: '2', name: 'Site 2', URL: 'https://site2.example.com' },
],
page: 1,
total_subscriptions: 6,
} );
( callApi as jest.Mock ).mockResolvedValueOnce( {
subscriptions: [
{ id: '3', name: 'Site 3', URL: 'https://site3.example.com' },
{ id: '4', name: 'Site 4', URL: 'https://site4.example.com' },
],
page: 2,
total_subscriptions: 6,
} );
( callApi as jest.Mock ).mockResolvedValueOnce( {
subscriptions: [
{ id: '5', name: 'Site 5', URL: 'https://site5.example.com' },
{ id: '6', name: 'Site 6', URL: 'https://site6.example.com' },
],
page: 3,
total_subscriptions: 6,
} );
const { result } = renderHook( () => useSiteSubscriptionsQuery( { number: 2 } ), {
wrapper,
} );
await waitFor( () => {
expect( callApi ).toHaveBeenCalledTimes( 3 );
expect( result.current.data.subscriptions.length ).toBe( 6 );
expect( result.current.data.totalCount ).toBe( 6 );
} );
} );
it( 'fetches subscriptions data with search term', async () => {
( callApi as jest.Mock ).mockResolvedValue( {
subscriptions: [
{ id: '1', name: 'Site 1', URL: 'https://site1.example.com' },
{ id: '2', name: 'Site 2', URL: 'https://site2.example.com' },
],
page: 1,
total_subscriptions: 2,
} );
const { result } = renderHook(
() => {
const { setSearchTerm, searchTerm } = useSiteSubscriptionsQueryProps();
const { data, isLoading } = useSiteSubscriptionsQuery();
return { setSearchTerm, searchTerm, data, isLoading };
},
{
wrapper,
}
);
await waitFor( () => expect( result.current.isLoading ).toBe( false ) );
act( () => result.current.setSearchTerm( 'te 1' ) );
await waitFor( () => expect( result.current.searchTerm ).toBe( 'te 1' ) );
expect( callApi ).toHaveBeenCalledTimes( 1 );
expect( result.current.data.subscriptions.length ).toBe( 1 );
expect( result.current.data.subscriptions[ 0 ].name ).toBe( 'Site 1' );
expect( result.current.data.totalCount ).toBe( 2 );
} );
it.each( [
{
sortTerm: SiteSubscriptionsSortBy.SiteName,
expectedResult: [
{ ID: '2', name: "Arnold's site" },
{ ID: '3', name: "Maciej's site" },
{ ID: '1', name: "Zorro's site" },
],
},
{
sortTerm: SiteSubscriptionsSortBy.DateSubscribed,
expectedResult: [
{ ID: '3', date_subscribed: '2023-04-18T17:00:00+00:00' },
{ ID: '2', date_subscribed: '2023-04-18T12:00:00+00:00' },
{ ID: '1', date_subscribed: '2022-01-18T00:00:00+00:00' },
],
},
{
sortTerm: SiteSubscriptionsSortBy.LastUpdated,
expectedResult: [
{ ID: '1', last_updated: '2023-04-18T19:00:00+00:00' },
{ ID: '3', last_updated: '2023-04-18T17:00:00+00:00' },
{ ID: '2', last_updated: '2023-04-18T12:00:00+00:00' },
],
},
] )( 'Applies sorting to the subscriptions returned', async ( { sortTerm, expectedResult } ) => {
( callApi as jest.Mock ).mockResolvedValue( {
subscriptions: [
{
ID: '1',
name: "Zorro's site",
URL: 'https://site2.example.com',
date_subscribed: '2022-01-18T00:00:00+00:00',
last_updated: '2023-04-18T19:00:00+00:00',
},
{
ID: '3',
name: "Maciej's site",
URL: 'https://site2.example.com',
date_subscribed: '2023-04-18T17:00:00+00:00',
last_updated: '2023-04-18T17:00:00+00:00',
},
{
ID: '2',
name: "Arnold's site",
URL: 'https://site1.example.com',
date_subscribed: '2023-04-18T12:00:00+00:00',
last_updated: '2023-04-18T12:00:00+00:00',
},
],
page: 1,
total_subscriptions: 3,
} );
const { result } = renderHook(
() => {
const { setSortTerm, sortTerm } = useSiteSubscriptionsQueryProps();
const { data, isLoading } = useSiteSubscriptionsQuery();
return { data, isLoading, setSortTerm, sortTerm };
},
{
wrapper,
}
);
await waitFor( () => expect( result.current.isLoading ).toBe( false ) );
act( () => result.current.setSortTerm( sortTerm ) );
await waitFor( () => expect( result.current.sortTerm ).toBe( sortTerm ) );
expect( callApi ).toHaveBeenCalledTimes( 1 );
result.current.data.subscriptions.forEach( ( subscription, index ) => {
expect( subscription.ID ).toEqual( expectedResult[ index ].ID );
} );
} );
} );
|