Server IP : 104.21.14.103 / Your IP : 18.119.131.235 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/base/hooks/test/ |
Upload File : |
/** * External dependencies */ import TestRenderer, { act } from 'react-test-renderer'; /** * Internal dependencies */ import { usePrevious } from '../use-previous'; describe( 'usePrevious', () => { const TestComponent = ( { testValue, validation } ) => { const previousValue = usePrevious( testValue, validation ); return <div testValue={ testValue } previousValue={ previousValue } />; }; let renderer; beforeEach( () => ( renderer = null ) ); it( 'should be undefined at first pass', () => { act( () => { renderer = TestRenderer.create( <TestComponent testValue={ 1 } /> ); } ); const testValue = renderer.root.findByType( 'div' ).props.testValue; const testPreviousValue = renderer.root.findByType( 'div' ).props .previousValue; expect( testValue ).toBe( 1 ); expect( testPreviousValue ).toBe( undefined ); } ); it( 'test new and previous value', () => { let testValue; let testPreviousValue; act( () => { renderer = TestRenderer.create( <TestComponent testValue={ 1 } /> ); } ); act( () => { renderer.update( <TestComponent testValue={ 2 } /> ); } ); testValue = renderer.root.findByType( 'div' ).props.testValue; testPreviousValue = renderer.root.findByType( 'div' ).props .previousValue; expect( testValue ).toBe( 2 ); expect( testPreviousValue ).toBe( 1 ); act( () => { renderer.update( <TestComponent testValue={ 3 } /> ); } ); testValue = renderer.root.findByType( 'div' ).props.testValue; testPreviousValue = renderer.root.findByType( 'div' ).props .previousValue; expect( testValue ).toBe( 3 ); expect( testPreviousValue ).toBe( 2 ); } ); it( 'should not update value if validation fails', () => { let testValue; let testPreviousValue; act( () => { renderer = TestRenderer.create( <TestComponent testValue={ 1 } validation={ Number.isFinite } /> ); } ); act( () => { renderer.update( <TestComponent testValue="abc" validation={ Number.isFinite } /> ); } ); testValue = renderer.root.findByType( 'div' ).props.testValue; testPreviousValue = renderer.root.findByType( 'div' ).props .previousValue; expect( testValue ).toBe( 'abc' ); expect( testPreviousValue ).toBe( 1 ); act( () => { renderer.update( <TestComponent testValue={ 3 } validation={ Number.isFinite } /> ); } ); testValue = renderer.root.findByType( 'div' ).props.testValue; testPreviousValue = renderer.root.findByType( 'div' ).props .previousValue; expect( testValue ).toBe( 3 ); expect( testPreviousValue ).toBe( 1 ); } ); } );