Skip to content

Commit

Permalink
Add tests for categories data store.
Browse files Browse the repository at this point in the history
  • Loading branch information
allilevine committed Dec 16, 2024
1 parent d1d1ab6 commit 373079a
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/data-stores/src/newsletter-categories/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export type NewsletterCategory = {
export type NewsletterCategories = {
enabled: boolean;
newsletterCategories: NewsletterCategory[];
error?: string;
};

type NewsletterCategoryResponse = {
Expand Down
100 changes: 100 additions & 0 deletions packages/data-stores/src/newsletter-categories/test/index.tsx
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();
} );
} );
} );

0 comments on commit 373079a

Please sign in to comment.