2019-10-28 13:53:09 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import TestRenderer, { act } from 'react-test-renderer';
|
|
|
|
import { createRegistry, RegistryProvider } from '@wordpress/data';
|
2019-11-08 16:30:11 +00:00
|
|
|
import { QUERY_STATE_STORE_KEY as storeKey } from '@woocommerce/block-data';
|
2019-10-28 13:53:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
2019-11-07 13:58:32 +00:00
|
|
|
useQueryStateByContext,
|
2019-10-28 13:53:09 +00:00
|
|
|
useQueryStateByKey,
|
|
|
|
useSynchronizedQueryState,
|
|
|
|
} from '../use-query-state';
|
|
|
|
|
|
|
|
jest.mock( '@woocommerce/block-data', () => ( {
|
|
|
|
__esModule: true,
|
|
|
|
QUERY_STATE_STORE_KEY: 'test/store',
|
|
|
|
} ) );
|
|
|
|
|
|
|
|
describe( 'Testing Query State Hooks', () => {
|
|
|
|
let registry, mocks;
|
|
|
|
beforeAll( () => {
|
|
|
|
registry = createRegistry();
|
|
|
|
mocks = {};
|
|
|
|
} );
|
|
|
|
/**
|
|
|
|
* Test helper to return a tuple containing the expected query value and the
|
|
|
|
* expected query state action creator from the given rendered test instance.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} testRenderer An instance of the created test component.
|
2019-10-28 13:53:09 +00:00
|
|
|
*
|
2019-11-15 14:41:23 +00:00
|
|
|
* @return {Array} A tuple containing the expected query value as the first
|
2019-10-28 13:53:09 +00:00
|
|
|
* element and the expected query action creator as the
|
|
|
|
* second argument.
|
|
|
|
*/
|
|
|
|
const getProps = ( testRenderer ) => {
|
2022-04-08 13:47:19 +00:00
|
|
|
//eslint-disable-next-line testing-library/await-async-query
|
2019-10-28 13:53:09 +00:00
|
|
|
const props = testRenderer.root.findByType( 'div' ).props;
|
|
|
|
return [ props.queryState, props.setQueryState ];
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the given component wrapped in the registry provider for
|
|
|
|
* instantiating using the TestRenderer using the current prepared registry
|
|
|
|
* for the TestRenderer to instantiate with.
|
|
|
|
*
|
2019-12-10 17:17:46 +00:00
|
|
|
* @param {*} Component The test component to wrap.
|
|
|
|
* @param {Object} props Props to feed the wrapped component.
|
2019-10-28 13:53:09 +00:00
|
|
|
*
|
2019-12-10 17:17:46 +00:00
|
|
|
* @return {*} Wrapped component.
|
2019-10-28 13:53:09 +00:00
|
|
|
*/
|
|
|
|
const getWrappedComponent = ( Component, props ) => (
|
|
|
|
<RegistryProvider value={ registry }>
|
|
|
|
<Component { ...props } />
|
|
|
|
</RegistryProvider>
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a TestComponent for the provided hook to test with, and the
|
|
|
|
* expected PropKeys for obtaining the values to be fed to the hook as
|
|
|
|
* arguments.
|
|
|
|
*
|
2019-11-15 14:41:23 +00:00
|
|
|
* @param {Function} hookTested The hook being tested to use in the
|
2019-10-28 13:53:09 +00:00
|
|
|
* test comopnent.
|
2019-11-15 14:41:23 +00:00
|
|
|
* @param {Array} propKeysForArgs An array of keys for the props that
|
2019-10-28 13:53:09 +00:00
|
|
|
* will be used on the test component that
|
|
|
|
* will have values fed to the tested
|
|
|
|
* hook.
|
|
|
|
*
|
2019-12-10 17:17:46 +00:00
|
|
|
* @return {*} A component ready for testing with!
|
2019-10-28 13:53:09 +00:00
|
|
|
*/
|
|
|
|
const getTestComponent = ( hookTested, propKeysForArgs ) => ( props ) => {
|
|
|
|
const args = propKeysForArgs.map( ( key ) => props[ key ] );
|
|
|
|
const [ queryValue, setQueryValue ] = hookTested( ...args );
|
|
|
|
return (
|
|
|
|
<div queryState={ queryValue } setQueryState={ setQueryValue } />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A helper for setting up the `mocks` object and the `registry` mock before
|
|
|
|
* each test.
|
|
|
|
*
|
|
|
|
* @param {string} actionMockName This should be the name of the action
|
|
|
|
* that the hook returns. This will be
|
|
|
|
* mocked using `mocks.action` when
|
|
|
|
* registered in the mock registry.
|
|
|
|
* @param {string} selectorMockName This should be the mame of the selector
|
|
|
|
* that the hook uses. This will be mocked
|
|
|
|
* using `mocks.selector` when registered
|
|
|
|
* in the mock registry.
|
|
|
|
*/
|
|
|
|
const setupMocks = ( actionMockName, selectorMockName ) => {
|
|
|
|
mocks.action = jest.fn().mockReturnValue( { type: 'testAction' } );
|
|
|
|
mocks.selector = jest.fn().mockReturnValue( { foo: 'bar' } );
|
|
|
|
registry.registerStore( storeKey, {
|
|
|
|
reducer: () => ( {} ),
|
|
|
|
actions: {
|
|
|
|
[ actionMockName ]: mocks.action,
|
|
|
|
},
|
|
|
|
selectors: {
|
|
|
|
[ selectorMockName ]: mocks.selector,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
};
|
2019-11-07 13:58:32 +00:00
|
|
|
describe( 'useQueryStateByContext', () => {
|
|
|
|
const TestComponent = getTestComponent( useQueryStateByContext, [
|
2019-10-28 13:53:09 +00:00
|
|
|
'context',
|
|
|
|
] );
|
|
|
|
let renderer;
|
|
|
|
beforeEach( () => {
|
|
|
|
renderer = null;
|
|
|
|
setupMocks( 'setValueForQueryContext', 'getValueForQueryContext' );
|
|
|
|
} );
|
|
|
|
afterEach( () => {
|
|
|
|
act( () => {
|
|
|
|
renderer.unmount();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
it(
|
|
|
|
'calls useSelect with the provided context and returns expected' +
|
|
|
|
' values',
|
|
|
|
() => {
|
|
|
|
const { action, selector } = mocks;
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponent( TestComponent, {
|
|
|
|
context: 'test-context',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
const [ queryState, setQueryState ] = getProps( renderer );
|
|
|
|
// the {} is because all selectors are called internally in the
|
|
|
|
// registry with the first argument being the state which is empty.
|
|
|
|
expect( selector ).toHaveBeenLastCalledWith(
|
|
|
|
{},
|
|
|
|
'test-context',
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
expect( queryState ).toEqual( { foo: 'bar' } );
|
|
|
|
expect( action ).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
//execute dispatcher and make sure it's called.
|
|
|
|
act( () => {
|
|
|
|
setQueryState( { foo: 'bar' } );
|
|
|
|
} );
|
2019-11-21 15:06:52 +00:00
|
|
|
expect( action ).toHaveBeenCalledWith( 'test-context', {
|
|
|
|
foo: 'bar',
|
|
|
|
} );
|
2019-10-28 13:53:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
describe( 'useQueryStateByKey', () => {
|
|
|
|
const TestComponent = getTestComponent( useQueryStateByKey, [
|
|
|
|
'queryKey',
|
2019-11-14 17:16:27 +00:00
|
|
|
undefined,
|
|
|
|
'context',
|
2019-10-28 13:53:09 +00:00
|
|
|
] );
|
|
|
|
let renderer;
|
|
|
|
beforeEach( () => {
|
|
|
|
renderer = null;
|
|
|
|
setupMocks( 'setQueryValue', 'getValueForQueryKey' );
|
|
|
|
} );
|
|
|
|
afterEach( () => {
|
|
|
|
act( () => {
|
|
|
|
renderer.unmount();
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
it(
|
|
|
|
'calls useSelect with the provided context and returns expected' +
|
|
|
|
' values',
|
|
|
|
() => {
|
|
|
|
const { selector, action } = mocks;
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponent( TestComponent, {
|
|
|
|
context: 'test-context',
|
|
|
|
queryKey: 'someValue',
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
const [ queryState, setQueryState ] = getProps( renderer );
|
|
|
|
// the {} is because all selectors are called internally in the
|
|
|
|
// registry with the first argument being the state which is empty.
|
|
|
|
expect( selector ).toHaveBeenLastCalledWith(
|
|
|
|
{},
|
|
|
|
'test-context',
|
|
|
|
'someValue',
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
expect( queryState ).toEqual( { foo: 'bar' } );
|
|
|
|
expect( action ).not.toHaveBeenCalled();
|
|
|
|
|
|
|
|
//execute dispatcher and make sure it's called.
|
|
|
|
act( () => {
|
|
|
|
setQueryState( { foo: 'bar' } );
|
|
|
|
} );
|
2019-11-01 12:33:42 +00:00
|
|
|
expect( action ).toHaveBeenCalledWith(
|
|
|
|
'test-context',
|
|
|
|
'someValue',
|
|
|
|
{ foo: 'bar' }
|
|
|
|
);
|
2019-10-28 13:53:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
} );
|
2020-05-26 17:31:33 +00:00
|
|
|
// Note: these tests only add partial coverage because the state is not
|
2019-10-28 13:53:09 +00:00
|
|
|
// actually updated by the action dispatch via our mocks.
|
|
|
|
describe( 'useSynchronizedQueryState', () => {
|
|
|
|
const TestComponent = getTestComponent( useSynchronizedQueryState, [
|
|
|
|
'synchronizedQuery',
|
2019-11-14 17:16:27 +00:00
|
|
|
'context',
|
2019-10-28 13:53:09 +00:00
|
|
|
] );
|
|
|
|
const initialQuery = { a: 'b' };
|
|
|
|
let renderer;
|
|
|
|
beforeEach( () => {
|
|
|
|
setupMocks( 'setValueForQueryContext', 'getValueForQueryContext' );
|
|
|
|
} );
|
|
|
|
it( 'returns provided query state on initial render', () => {
|
|
|
|
const { action, selector } = mocks;
|
|
|
|
act( () => {
|
|
|
|
renderer = TestRenderer.create(
|
|
|
|
getWrappedComponent( TestComponent, {
|
|
|
|
context: 'test-context',
|
|
|
|
synchronizedQuery: initialQuery,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
const [ queryState ] = getProps( renderer );
|
|
|
|
expect( queryState ).toBe( initialQuery );
|
|
|
|
expect( selector ).toHaveBeenLastCalledWith(
|
|
|
|
{},
|
|
|
|
'test-context',
|
|
|
|
undefined
|
|
|
|
);
|
|
|
|
expect( action ).toHaveBeenCalledWith( 'test-context', {
|
|
|
|
foo: 'bar',
|
|
|
|
a: 'b',
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
it( 'returns merged queryState on subsequent render', () => {
|
|
|
|
act( () => {
|
|
|
|
renderer.update(
|
|
|
|
getWrappedComponent( TestComponent, {
|
|
|
|
context: 'test-context',
|
|
|
|
synchronizedQuery: initialQuery,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
} );
|
|
|
|
// note our test doesn't interact with an actual reducer so the
|
|
|
|
// store state is not updated. Here we're just verifying that
|
|
|
|
// what is is returned by the state selector mock is returned.
|
|
|
|
// However we DO expect this to be a new object.
|
|
|
|
const [ queryState ] = getProps( renderer );
|
|
|
|
expect( queryState ).not.toBe( initialQuery );
|
|
|
|
expect( queryState ).toEqual( { foo: 'bar' } );
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
} );
|