Server IP : 104.21.14.103 / Your IP : 3.144.2.48 Web Server : LiteSpeed System : Linux business53.web-hosting.com 4.18.0-553.lve.el8.x86_64 #1 SMP Mon May 27 15:27:34 UTC 2024 x86_64 User : giankuin ( 1871) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : OFF | Pkexec : OFF Directory : /proc/thread-self/root/home/giankuin/sieuthiweb.com.vn/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/hocs/test/ |
Upload File : |
/** * External dependencies */ import TestRenderer from 'react-test-renderer'; /** * Internal dependencies */ import withCategories from '../with-categories'; import * as mockUtils from '../../components/utils'; import * as mockBaseUtils from '../../base/utils/errors'; jest.mock( '../../components/utils', () => ( { getCategories: jest.fn(), } ) ); jest.mock( '../../base/utils/errors', () => ( { formatError: jest.fn(), } ) ); const mockCategories = [ { id: 1, name: 'Clothing' }, { id: 2, name: 'Food' }, ]; const TestComponent = withCategories( ( props ) => { return ( <div error={ props.error } isLoading={ props.isLoading } categories={ props.categories } /> ); } ); const render = () => { return TestRenderer.create( <TestComponent /> ); }; describe( 'withCategories Component', () => { let renderer; afterEach( () => { mockUtils.getCategories.mockReset(); } ); describe( 'lifecycle events', () => { beforeEach( () => { mockUtils.getCategories.mockImplementation( () => Promise.resolve() ); renderer = render(); } ); it( 'getCategories is called on mount', () => { const { getCategories } = mockUtils; expect( getCategories ).toHaveBeenCalledTimes( 1 ); } ); } ); describe( 'when the API returns categories data', () => { beforeEach( () => { mockUtils.getCategories.mockImplementation( () => Promise.resolve( mockCategories ) ); renderer = render(); } ); it( 'sets the categories props', () => { const props = renderer.root.findByType( 'div' ).props; expect( props.error ).toBeNull(); expect( props.isLoading ).toBe( false ); expect( props.categories ).toEqual( mockCategories ); } ); } ); describe( 'when the API returns an error', () => { const error = { message: 'There was an error.' }; const getCategoriesPromise = Promise.reject( error ); const formattedError = { message: 'There was an error.', type: 'api' }; beforeEach( () => { mockUtils.getCategories.mockImplementation( () => getCategoriesPromise ); mockBaseUtils.formatError.mockImplementation( () => formattedError ); renderer = render(); } ); test( 'sets the error prop', async () => { await expect( () => getCategoriesPromise() ).toThrow(); const { formatError } = mockBaseUtils; const props = renderer.root.findByType( 'div' ).props; expect( formatError ).toHaveBeenCalledWith( error ); expect( formatError ).toHaveBeenCalledTimes( 1 ); expect( props.error ).toEqual( formattedError ); expect( props.isLoading ).toBe( false ); expect( props.categories ).toBeNull(); } ); } ); } );