-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for categories data store.
- Loading branch information
1 parent
d1d1ab6
commit 373079a
Showing
2 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
packages/data-stores/src/newsletter-categories/test/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; | ||
import { renderHook, waitFor } from '@testing-library/react'; | ||
import * as React from 'react'; | ||
import request from 'wpcom-proxy-request'; | ||
import { useNewsletterCategories, getNewsletterCategoriesKey } from '../index'; | ||
|
||
// Mock the wpcom-proxy-request | ||
jest.mock( 'wpcom-proxy-request' ); | ||
const mockedRequest = request as jest.MockedFunction< typeof request >; | ||
|
||
type WrapperProps = { | ||
children: React.ReactNode; | ||
}; | ||
|
||
describe( 'Newsletter Categories Data Store', () => { | ||
const queryClient = new QueryClient(); | ||
const wrapper = ( { children }: WrapperProps ) => ( | ||
<QueryClientProvider client={ queryClient }>{ children }</QueryClientProvider> | ||
); | ||
|
||
beforeEach( () => { | ||
queryClient.clear(); | ||
jest.clearAllMocks(); | ||
} ); | ||
|
||
describe( 'getNewsletterCategoriesKey', () => { | ||
it( 'should return the correct query key', () => { | ||
expect( getNewsletterCategoriesKey( '123' ) ).toEqual( [ 'newsletter-categories', '123' ] ); | ||
} ); | ||
} ); | ||
|
||
describe( 'useNewsletterCategories', () => { | ||
const mockResponse = { | ||
enabled: true, | ||
newsletter_categories: [ | ||
{ | ||
id: 1, | ||
name: 'Test Category', | ||
slug: 'test-category', | ||
description: 'Test Description', | ||
parent: 0, | ||
}, | ||
], | ||
}; | ||
|
||
it( 'should fetch and convert newsletter categories successfully', async () => { | ||
mockedRequest.mockResolvedValueOnce( mockResponse ); | ||
|
||
const { result } = renderHook( () => useNewsletterCategories( { siteId: 123 } ), { | ||
wrapper, | ||
} ); | ||
|
||
// Initial state | ||
expect( result.current.isLoading ).toBe( true ); | ||
|
||
// Wait for the query to complete | ||
await waitFor( () => expect( result.current.isSuccess ).toBe( true ) ); | ||
|
||
// Check the converted response | ||
expect( result.current.data ).toEqual( { | ||
enabled: true, | ||
newsletterCategories: mockResponse.newsletter_categories, | ||
} ); | ||
|
||
// Verify API call | ||
expect( mockedRequest ).toHaveBeenCalledWith( { | ||
path: '/sites/123/newsletter-categories', | ||
apiVersion: '2', | ||
apiNamespace: 'wpcom/v2', | ||
} ); | ||
} ); | ||
|
||
it( 'should handle API errors gracefully', async () => { | ||
const error = new Error( 'API Error' ); | ||
mockedRequest.mockRejectedValueOnce( error ); | ||
|
||
const { result } = renderHook( () => useNewsletterCategories( { siteId: 123 } ), { | ||
wrapper, | ||
} ); | ||
|
||
await waitFor( () => expect( result.current.isSuccess ).toBe( true ) ); | ||
|
||
expect( result.current.data ).toEqual( { | ||
enabled: false, | ||
newsletterCategories: [], | ||
error: 'API Error', | ||
} ); | ||
} ); | ||
|
||
it( 'should not fetch when siteId is falsy', () => { | ||
const { result } = renderHook( () => useNewsletterCategories( { siteId: 0 } ), { wrapper } ); | ||
|
||
expect( result.current.isLoading ).toBe( false ); | ||
expect( mockedRequest ).not.toHaveBeenCalled(); | ||
} ); | ||
} ); | ||
} ); |