initial commit

This commit is contained in:
2021-12-10 12:03:04 +00:00
commit c46c7ddbf0
3643 changed files with 582794 additions and 0 deletions

View File

@ -0,0 +1,73 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { receiveRoutes } from '../reducers';
import { ACTION_TYPES as types } from '../action-types';
describe( 'receiveRoutes', () => {
it( 'returns original state when action type is not a match', () => {
expect( receiveRoutes( undefined, { type: 'invalid' } ) ).toEqual( {} );
} );
it( 'returns original state when the given endpoints already exists', () => {
const routes = [
'wc/blocks/products/attributes',
'wc/blocks/products/attributes/(?P<attribute_id>[d]+)/terms/(?P<id>[d]+)',
];
const originalState = deepFreeze( {
'wc/blocks': {
'products/attributes': {
'wc/blocks/products/attributes': [],
},
'products/attributes/terms': {
'wc/blocks/products/attributes/{attribute_id}/terms/{id}': [
'attribute_id',
'id',
],
},
},
} );
const newState = receiveRoutes( originalState, {
type: types.RECEIVE_MODEL_ROUTES,
namespace: 'wc/blocks',
routes,
} );
expect( newState ).toBe( originalState );
} );
it( 'returns expected state when new route added', () => {
const action = {
type: types.RECEIVE_MODEL_ROUTES,
namespace: 'wc/blocks',
routes: [ 'wc/blocks/products/attributes' ],
};
const originalState = deepFreeze( {
'wc/blocks': {
'products/attributes/terms': {
'wc/blocks/products/attributes/{attribute_id}/terms/{id}': [
'attribute_id',
'id',
],
},
},
} );
const newState = receiveRoutes( originalState, action );
expect( newState ).not.toBe( originalState );
expect( newState ).toEqual( {
'wc/blocks': {
'products/attributes': {
'wc/blocks/products/attributes': [],
},
'products/attributes/terms': {
'wc/blocks/products/attributes/{attribute_id}/terms/{id}': [
'attribute_id',
'id',
],
},
},
} );
} );
} );

View File

@ -0,0 +1,55 @@
/**
* External dependencies
*/
import { select, apiFetch } from '@wordpress/data-controls';
/**
* Internal dependencies
*/
import { getRoute, getRoutes } from '../resolvers';
import { receiveRoutes } from '../actions';
import { STORE_KEY } from '../constants';
jest.mock( '@wordpress/data-controls' );
describe( 'getRoute', () => {
it( 'yields select control response', () => {
const fulfillment = getRoute( 'wc/blocks' );
fulfillment.next();
expect( select ).toHaveBeenCalledWith(
STORE_KEY,
'getRoutes',
'wc/blocks'
);
const { done } = fulfillment.next();
expect( done ).toBe( true );
} );
} );
describe( 'getRoutes', () => {
describe( 'yields with expected responses', () => {
let fulfillment;
const rewind = () => ( fulfillment = getRoutes( 'wc/blocks' ) );
test( 'with apiFetch control invoked', () => {
rewind();
fulfillment.next();
expect( apiFetch ).toHaveBeenCalledWith( { path: 'wc/blocks' } );
} );
test( 'with receiveRoutes action with valid response', () => {
const testResponse = {
routes: {
'/wc/blocks/products/attributes': [],
},
};
const { value } = fulfillment.next( testResponse );
expect( value ).toEqual(
receiveRoutes( Object.keys( testResponse.routes ), 'wc/blocks' )
);
} );
test( 'with receiveRoutesAction with invalid response', () => {
rewind();
fulfillment.next();
const { value } = fulfillment.next( {} );
expect( value ).toEqual( receiveRoutes( [], 'wc/blocks' ) );
} );
} );
} );

View File

@ -0,0 +1,104 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import { getRoute, getRoutes } from '../selectors';
const mockHasFinishedResolution = jest.fn().mockReturnValue( false );
jest.mock( '@wordpress/data', () => ( {
__esModule: true,
createRegistrySelector: ( callback ) =>
callback( () => ( {
hasFinishedResolution: mockHasFinishedResolution,
} ) ),
} ) );
const testState = deepFreeze( {
routes: {
'wc/blocks': {
'products/attributes': {
'wc/blocks/products/attributes': [],
},
'products/attributes/terms': {
'wc/blocks/products/attributes/{attribute_id}/terms/{id}': [
'attribute_id',
'id',
],
},
},
},
} );
describe( 'getRoute', () => {
const invokeTest = ( namespace, resourceName, ids = [] ) => () => {
return getRoute( testState, namespace, resourceName, ids );
};
describe( 'with throwing errors', () => {
beforeEach( () => mockHasFinishedResolution.mockReturnValue( true ) );
it( 'throws an error if there is no route for the given namespace', () => {
expect( invokeTest( 'invalid' ) ).toThrowError( /given namespace/ );
} );
it(
'throws an error if there are routes for the given namespace, but no ' +
'route for the given resource',
() => {
expect( invokeTest( 'wc/blocks', 'invalid' ) ).toThrowError();
}
);
it(
'throws an error if there are routes for the given namespace and ' +
'resource name, but no routes for the given ids',
() => {
expect(
invokeTest( 'wc/blocks', 'products/attributes', [ 10 ] )
).toThrowError( /number of ids you included/ );
}
);
} );
describe( 'with no throwing of errors if resolution has not finished', () => {
beforeEach( () => mockHasFinishedResolution.mockReturnValue( false ) );
it.each`
description | args
${ 'is no route for the given namespace' } | ${ [ 'invalid' ] }
${ 'are no routes for the given namespace, but no route for the given resource' } | ${ [ 'wc/blocks', 'invalid' ] }
${ 'are routes for the given namespace and resource name, but no routes for the given ids' } | ${ [ 'wc/blocks', 'products/attributes', [ 10 ] ] }
`( 'does not throw an error if there $description', ( { args } ) => {
expect( invokeTest( ...args ) ).not.toThrowError();
} );
} );
describe( 'returns expected value for given valid arguments', () => {
test( 'when there is a route with no placeholders', () => {
expect( invokeTest( 'wc/blocks', 'products/attributes' )() ).toBe(
'wc/blocks/products/attributes'
);
} );
test( 'when there is a route with placeholders', () => {
expect(
invokeTest( 'wc/blocks', 'products/attributes/terms', [
10,
20,
] )()
).toBe( 'wc/blocks/products/attributes/10/terms/20' );
} );
} );
} );
describe( 'getRoutes', () => {
const invokeTest = ( namespace ) => () => {
return getRoutes( testState, namespace );
};
it( 'throws an error if there is no route for the given namespace', () => {
mockHasFinishedResolution.mockReturnValue( true );
expect( invokeTest( 'invalid' ) ).toThrowError( /given namespace/ );
} );
it( 'returns expected routes for given namespace', () => {
expect( invokeTest( 'wc/blocks' )() ).toEqual( [
'wc/blocks/products/attributes',
'wc/blocks/products/attributes/{attribute_id}/terms/{id}',
] );
} );
} );

View File

@ -0,0 +1,56 @@
/**
* Internal dependencies
*/
import {
extractResourceNameFromRoute,
getRouteIds,
simplifyRouteWithId,
} from '../utils';
describe( 'extractResourceNameFromRoute', () => {
it.each`
namespace | route | expected
${ 'wc/blocks' } | ${ 'wc/blocks/products' } | ${ 'products' }
${ 'wc/other' } | ${ 'wc/blocks/product' } | ${ 'wc/blocks/product' }
${ 'wc/blocks' } | ${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)' } | ${ 'products/attributes' }
${ 'wc/blocks' } | ${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms' } | ${ 'products/attributes/terms' }
${ 'wc/blocks' } | ${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms/(?P<id>[d]+)' } | ${ 'products/attributes/terms' }
`(
'returns "$expected" when namespace is "$namespace" and route is "$route"',
( { namespace, route, expected } ) => {
expect( extractResourceNameFromRoute( namespace, route ) ).toBe(
expected
);
}
);
} );
describe( 'getRouteIds', () => {
it.each`
route | expected
${ 'wc/blocks/products' } | ${ [] }
${ 'wc/blocks/products/(?P<id>[\\d]+)' } | ${ [ 'id' ] }
${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms/(?P<id>[\\d]+)' } | ${ [ 'attribute_id', 'id' ] }
`(
'returns "$expected" when route is "$route"',
( { route, expected } ) => {
expect( getRouteIds( route ) ).toEqual( expected );
}
);
} );
describe( 'simplifyRouteWithId', () => {
it.each`
route | matchIds | expected
${ 'wc/blocks/products' } | ${ [] } | ${ 'wc/blocks/products' }
${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)' } | ${ [ 'attribute_id' ] } | ${ 'wc/blocks/products/attributes/{attribute_id}' }
${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms' } | ${ [ 'attribute_id' ] } | ${ 'wc/blocks/products/attributes/{attribute_id}/terms' }
${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms/(?P<id>[\\d]+)' } | ${ [ 'attribute_id', 'id' ] } | ${ 'wc/blocks/products/attributes/{attribute_id}/terms/{id}' }
${ 'wc/blocks/products/attributes/(?P<attribute_id>[\\d]+)/terms/(?P<id>[\\d]+)' } | ${ [ 'id', 'attribute_id' ] } | ${ 'wc/blocks/products/attributes/{attribute_id}/terms/{id}' }
`(
'returns "$expected" when route is "$route" and matchIds is "$matchIds"',
( { route, matchIds, expected } ) => {
expect( simplifyRouteWithId( route, matchIds ) ).toBe( expected );
}
);
} );