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,99 @@
/**
* External dependencies
*/
import deepFreeze from 'deep-freeze';
/**
* Internal dependencies
*/
import receiveCollection from '../reducers';
import { ACTION_TYPES as types } from '../action-types';
describe( 'receiveCollection', () => {
const originalState = deepFreeze( {
'wc/blocks': {
products: {
'[]': {
'?someQuery=2': {
items: [ 'foo' ],
headers: { 'x-wp-total': 22 },
},
},
},
},
} );
it(
'returns original state when there is already an entry in the state ' +
'for the given arguments',
() => {
const testAction = {
type: types.RECEIVE_COLLECTION,
namespace: 'wc/blocks',
resourceName: 'products',
queryString: '?someQuery=2',
response: {
items: [ 'bar' ],
headers: { foo: 'bar' },
},
};
expect( receiveCollection( originalState, testAction ) ).toBe(
originalState
);
}
);
it(
'returns new state when items exist in collection but the type is ' +
'for a reset',
() => {
const testAction = {
type: types.RESET_COLLECTION,
namespace: 'wc/blocks',
resourceName: 'products',
queryString: '?someQuery=2',
response: {
items: [ 'cheeseburger' ],
headers: { foo: 'bar' },
},
};
const newState = receiveCollection( originalState, testAction );
expect( newState ).not.toBe( originalState );
expect(
newState[ 'wc/blocks' ].products[ '[]' ][ '?someQuery=2' ]
).toEqual( {
items: [ 'cheeseburger' ],
headers: { foo: 'bar' },
} );
}
);
it( 'returns new state when items do not exist in collection yet', () => {
const testAction = {
type: types.RECEIVE_COLLECTION,
namespace: 'wc/blocks',
resourceName: 'products',
queryString: '?someQuery=3',
response: { items: [ 'cheeseburger' ], headers: { foo: 'bar' } },
};
const newState = receiveCollection( originalState, testAction );
expect( newState ).not.toBe( originalState );
expect(
newState[ 'wc/blocks' ].products[ '[]' ][ '?someQuery=3' ]
).toEqual( { items: [ 'cheeseburger' ], headers: { foo: 'bar' } } );
} );
it( 'sets expected state when ids are passed in', () => {
const testAction = {
type: types.RECEIVE_COLLECTION,
namespace: 'wc/blocks',
resourceName: 'products/attributes',
queryString: '?something',
response: { items: [ 10, 20 ], headers: { foo: 'bar' } },
ids: [ 30, 42 ],
};
const newState = receiveCollection( originalState, testAction );
expect( newState ).not.toBe( originalState );
expect(
newState[ 'wc/blocks' ][ 'products/attributes' ][ '[30,42]' ][
'?something'
]
).toEqual( { items: [ 10, 20 ], headers: { foo: 'bar' } } );
} );
} );

View File

@ -0,0 +1,161 @@
/**
* External dependencies
*/
import { select } from '@wordpress/data-controls';
/**
* Internal dependencies
*/
import { getCollection, getCollectionHeader } from '../resolvers';
import { receiveCollection } from '../actions';
import { STORE_KEY as SCHEMA_STORE_KEY } from '../../schema/constants';
import { STORE_KEY } from '../constants';
import { apiFetchWithHeaders } from '../../shared-controls';
jest.mock( '@wordpress/data-controls' );
describe( 'getCollection', () => {
describe( 'yields with expected responses', () => {
let fulfillment;
const testArgs = [
'wc/blocks',
'products',
{ foo: 'bar' },
[ 20, 30 ],
];
const rewind = () => ( fulfillment = getCollection( ...testArgs ) );
test( 'with getRoute call invoked to retrieve route', () => {
rewind();
fulfillment.next();
expect( select ).toHaveBeenCalledWith(
SCHEMA_STORE_KEY,
'getRoute',
testArgs[ 0 ],
testArgs[ 1 ],
testArgs[ 3 ]
);
} );
test(
'when no route is retrieved, yields receiveCollection and ' +
'returns',
() => {
const { value } = fulfillment.next();
const expected = receiveCollection(
'wc/blocks',
'products',
'?foo=bar',
[ 20, 30 ],
{
items: [],
headers: {
get: () => undefined,
has: () => undefined,
},
}
);
expect( value.type ).toBe( expected.type );
expect( value.namespace ).toBe( expected.namespace );
expect( value.resourceName ).toBe( expected.resourceName );
expect( value.queryString ).toBe( expected.queryString );
expect( value.ids ).toEqual( expected.ids );
expect( Object.keys( value.response ) ).toEqual(
Object.keys( expected.response )
);
const { done } = fulfillment.next();
expect( done ).toBe( true );
}
);
test(
'when route is retrieved, yields apiFetchWithHeaders control action with ' +
'expected route',
() => {
rewind();
fulfillment.next();
const { value } = fulfillment.next( 'https://example.org' );
expect( value ).toEqual(
apiFetchWithHeaders( {
path: 'https://example.org?foo=bar',
} )
);
}
);
test(
'when apiFetchWithHeaders does not return a valid response, ' +
'yields expected action',
() => {
const { value } = fulfillment.next( {} );
expect( value ).toEqual(
receiveCollection(
'wc/blocks',
'products',
'?foo=bar',
[ 20, 30 ],
{ items: [], headers: undefined }
)
);
}
);
test(
'when apiFetch returns a valid response, yields expected ' +
'action',
() => {
rewind();
fulfillment.next();
fulfillment.next( 'https://example.org' );
const { value } = fulfillment.next( {
response: [ '42', 'cheeseburgers' ],
headers: { foo: 'bar' },
} );
expect( value ).toEqual(
receiveCollection(
'wc/blocks',
'products',
'?foo=bar',
[ 20, 30 ],
{
items: [ '42', 'cheeseburgers' ],
headers: { foo: 'bar' },
}
)
);
const { done } = fulfillment.next();
expect( done ).toBe( true );
}
);
} );
} );
describe( 'getCollectionHeader', () => {
let fulfillment;
const rewind = ( ...testArgs ) =>
( fulfillment = getCollectionHeader( ...testArgs ) );
it( 'yields expected select control when called with less args', () => {
rewind( 'x-wp-total', '/wc/blocks', 'products' );
const { value } = fulfillment.next();
expect( value ).toEqual(
select( STORE_KEY, 'getCollection', '/wc/blocks', 'products' )
);
} );
it( 'yields expected select control when called with all args', () => {
const args = [
'x-wp-total',
'/wc/blocks',
'products/attributes',
{ sort: 'ASC' },
[ 10 ],
];
rewind( ...args );
const { value } = fulfillment.next();
expect( value ).toEqual(
select(
STORE_KEY,
'/wc/blocks',
'products/attributes',
{ sort: 'ASC' },
[ 10 ]
)
);
const { done } = fulfillment.next();
expect( done ).toBe( true );
} );
} );

View File

@ -0,0 +1,117 @@
/**
* Internal dependencies
*/
import { getCollection, getCollectionHeader } from '../selectors';
const getHeaderMock = ( total ) => {
const headers = { total };
return {
get: ( key ) => headers[ key ] || null,
has: ( key ) => !! headers[ key ],
};
};
const state = {
'wc/blocks': {
products: {
'[]': {
'?someQuery=2': {
items: [ 'foo' ],
headers: getHeaderMock( 22 ),
},
},
},
'products/attributes': {
'[10]': {
'?someQuery=2': {
items: [ 'bar' ],
headers: getHeaderMock( 42 ),
},
},
},
'products/attributes/terms': {
'[10,20]': {
'?someQuery=10': {
items: [ 42 ],
headers: getHeaderMock( 12 ),
},
},
},
},
};
describe( 'getCollection', () => {
it( 'returns empty array when namespace does not exist in state', () => {
expect( getCollection( state, 'invalid', 'products' ) ).toEqual( [] );
} );
it( 'returns empty array when resourceName does not exist in state', () => {
expect( getCollection( state, 'wc/blocks', 'invalid' ) ).toEqual( [] );
} );
it( 'returns empty array when query does not exist in state', () => {
expect( getCollection( state, 'wc/blocks', 'products' ) ).toEqual( [] );
} );
it( 'returns empty array when ids do not exist in state', () => {
expect(
getCollection(
state,
'wc/blocks',
'products/attributes',
'?someQuery=2',
[ 20 ]
)
).toEqual( [] );
} );
describe( 'returns expected values for items existing in state', () => {
test.each`
resourceName | ids | query | expected
${ 'products' } | ${ [] } | ${ { someQuery: 2 } } | ${ [ 'foo' ] }
${ 'products/attributes' } | ${ [ 10 ] } | ${ { someQuery: 2 } } | ${ [ 'bar' ] }
${ 'products/attributes/terms' } | ${ [ 10, 20 ] } | ${ { someQuery: 10 } } | ${ [ 42 ] }
`(
'for "$resourceName", "$ids", and "$query"',
( { resourceName, ids, query, expected } ) => {
expect(
getCollection(
state,
'wc/blocks',
resourceName,
query,
ids
)
).toEqual( expected );
}
);
} );
} );
describe( 'getCollectionHeader', () => {
it(
'returns undefined when there are headers but the specific header ' +
'does not exist',
() => {
expect(
getCollectionHeader(
state,
'invalid',
'wc/blocks',
'products',
{
someQuery: 2,
}
)
).toBeUndefined();
}
);
it( 'returns null when there are no headers for the given arguments', () => {
expect( getCollectionHeader( state, 'wc/blocks', 'invalid' ) ).toBe(
null
);
} );
it( 'returns expected header when it exists', () => {
expect(
getCollectionHeader( state, 'total', 'wc/blocks', 'products', {
someQuery: 2,
} )
).toBe( 22 );
} );
} );