Download wp-calypso/client/reader/following/test/use-site-subscriptions.test.ts from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/reader/following/test/use-site-subscriptions.test.ts
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/wp-calypso/client/reader/following/test/use-site-subscriptions.test.ts
-
curl -L -o use-site-subscriptions.test.ts https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/wp-calypso/client/reader/following/test/use-site-subscriptions.test.ts
3.68 kB
| /** | |
| * @jest-environment jsdom | |
| */ | |
| import { SubscriptionManager } from '@automattic/data-stores'; | |
| import { renderHook } from '@testing-library/react'; | |
| import { useSiteSubscriptions } from '../use-site-subscriptions'; | |
| jest.mock( '@automattic/data-stores', () => ( { | |
| SubscriptionManager: { | |
| useSubscriptionsCountQuery: jest.fn(), | |
| useSiteSubscriptionsQuery: jest.fn(), | |
| }, | |
| } ) ); | |
| describe( 'useSiteSubscriptions', () => { | |
| beforeEach( () => { | |
| jest.clearAllMocks(); | |
| } ); | |
| it( 'should return loading state when either query is loading', () => { | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: null, | |
| isLoading: true, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: null, | |
| isLoading: false, | |
| refetch: jest.fn(), | |
| } ); | |
| const { result } = renderHook( () => useSiteSubscriptions() ); | |
| expect( result.current.isLoading ).toBe( true ); | |
| } ); | |
| it( 'should return false for hasNonSelfSubscriptions when blog count is 0', () => { | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: { blogs: 0 }, | |
| isLoading: false, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: null, | |
| isLoading: false, | |
| refetch: jest.fn(), | |
| } ); | |
| const { result } = renderHook( () => useSiteSubscriptions() ); | |
| expect( result.current.hasNonSelfSubscriptions ).toBe( false ); | |
| } ); | |
| it( 'should filter out self-owned blogs when calculating hasNonSelfSubscriptions', () => { | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: { blogs: 2 }, | |
| isLoading: false, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: { | |
| subscriptions: [ { is_owner: true }, { is_owner: false } ], | |
| }, | |
| isLoading: false, | |
| refetch: jest.fn(), | |
| } ); | |
| const { result } = renderHook( () => useSiteSubscriptions() ); | |
| expect( result.current.hasNonSelfSubscriptions ).toBe( true ); | |
| } ); | |
| it( 'should return false for hasNonSelfSubscriptions when all subscriptions are self-owned', () => { | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: { blogs: 2 }, | |
| isLoading: false, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: { | |
| subscriptions: [ { is_owner: true }, { is_owner: true } ], | |
| }, | |
| isLoading: false, | |
| refetch: jest.fn(), | |
| } ); | |
| const { result } = renderHook( () => useSiteSubscriptions() ); | |
| expect( result.current.hasNonSelfSubscriptions ).toBe( false ); | |
| } ); | |
| it( 'should return true for hasNonSelfSubscriptions when blog count > 0 but no subscription data yet', () => { | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: { blogs: 1 }, | |
| isLoading: false, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: null, | |
| isLoading: false, | |
| refetch: jest.fn(), | |
| } ); | |
| const { result } = renderHook( () => useSiteSubscriptions() ); | |
| expect( result.current.hasNonSelfSubscriptions ).toBe( true ); | |
| } ); | |
| it( 'should call refetch when blog count changes from 0 to positive', () => { | |
| const refetchMock = jest.fn(); | |
| ( SubscriptionManager.useSubscriptionsCountQuery as jest.Mock ).mockReturnValue( { | |
| data: { blogs: 1 }, | |
| isLoading: false, | |
| } ); | |
| ( SubscriptionManager.useSiteSubscriptionsQuery as jest.Mock ).mockReturnValue( { | |
| data: null, | |
| isLoading: false, | |
| refetch: refetchMock, | |
| } ); | |
| renderHook( () => useSiteSubscriptions() ); | |
| expect( refetchMock ).toHaveBeenCalled(); | |
| } ); | |
| } ); | |