2021-02-17 13:01:20 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { renderHook } from '@testing-library/react-hooks';
|
2021-02-09 16:54:38 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import {
|
|
|
|
__experimentalRegisterCheckoutFilters,
|
|
|
|
__experimentalApplyCheckoutFilter,
|
|
|
|
} from '../';
|
|
|
|
|
|
|
|
describe( 'Checkout registry', () => {
|
|
|
|
const filterName = 'loremIpsum';
|
|
|
|
|
|
|
|
test( 'should return default value if there are no filters', () => {
|
|
|
|
const value = 'Hello World';
|
2021-02-17 13:01:20 +00:00
|
|
|
const { result: newValue } = renderHook( () =>
|
|
|
|
__experimentalApplyCheckoutFilter( {
|
|
|
|
filterName,
|
|
|
|
defaultValue: value,
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
expect( newValue.current ).toBe( value );
|
2021-02-09 16:54:38 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should return filtered value when a filter is registered', () => {
|
|
|
|
const value = 'Hello World';
|
|
|
|
__experimentalRegisterCheckoutFilters( filterName, {
|
2021-02-17 13:01:20 +00:00
|
|
|
[ filterName ]: ( val, extensions, args ) =>
|
2021-02-09 16:54:38 +00:00
|
|
|
val.toUpperCase() + args.punctuationSign,
|
|
|
|
} );
|
2021-02-17 13:01:20 +00:00
|
|
|
const { result: newValue } = renderHook( () =>
|
|
|
|
__experimentalApplyCheckoutFilter( {
|
|
|
|
filterName,
|
|
|
|
defaultValue: value,
|
|
|
|
arg: {
|
|
|
|
punctuationSign: '!',
|
|
|
|
},
|
|
|
|
} )
|
|
|
|
);
|
2021-02-09 16:54:38 +00:00
|
|
|
|
2021-02-17 13:01:20 +00:00
|
|
|
expect( newValue.current ).toBe( 'HELLO WORLD!' );
|
2021-02-09 16:54:38 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should not return filtered value if validation failed', () => {
|
|
|
|
const value = 'Hello World';
|
|
|
|
__experimentalRegisterCheckoutFilters( filterName, {
|
|
|
|
[ filterName ]: ( val ) => val.toUpperCase(),
|
|
|
|
} );
|
2021-02-17 13:01:20 +00:00
|
|
|
const { result: newValue } = renderHook( () =>
|
|
|
|
__experimentalApplyCheckoutFilter( {
|
|
|
|
filterName,
|
|
|
|
defaultValue: value,
|
|
|
|
validation: ( val ) => ! val.includes( 'HELLO' ),
|
|
|
|
} )
|
|
|
|
);
|
|
|
|
|
|
|
|
expect( newValue.current ).toBe( value );
|
|
|
|
} );
|
|
|
|
|
|
|
|
test( 'should catch filter errors if user is not an admin', () => {
|
|
|
|
const spy = {};
|
|
|
|
spy.console = jest
|
|
|
|
.spyOn( console, 'error' )
|
|
|
|
.mockImplementation( () => {} );
|
|
|
|
|
|
|
|
const error = new Error( 'test error' );
|
|
|
|
const value = 'Hello World';
|
|
|
|
__experimentalRegisterCheckoutFilters( filterName, {
|
|
|
|
[ filterName ]: () => {
|
|
|
|
throw error;
|
|
|
|
},
|
2021-02-09 16:54:38 +00:00
|
|
|
} );
|
2021-02-17 13:01:20 +00:00
|
|
|
const { result: newValue } = renderHook( () =>
|
|
|
|
__experimentalApplyCheckoutFilter( {
|
|
|
|
filterName,
|
|
|
|
defaultValue: value,
|
|
|
|
} )
|
|
|
|
);
|
2021-02-09 16:54:38 +00:00
|
|
|
|
2021-02-17 13:01:20 +00:00
|
|
|
expect( spy.console ).toHaveBeenCalledWith( error );
|
|
|
|
expect( newValue.current ).toBe( value );
|
|
|
|
spy.console.mockRestore();
|
2021-02-09 16:54:38 +00:00
|
|
|
} );
|
|
|
|
} );
|