Skip to content

Commit

Permalink
Gutenberg: include @wordpress/data package
Browse files Browse the repository at this point in the history
  • Loading branch information
vindl committed Jul 24, 2018
1 parent 989893d commit 849bfb6
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 28 deletions.
103 changes: 103 additions & 0 deletions client/gutenberg/editor/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* @format
* @jest-environment jsdom
*/

/**
* Internal dependencies
*/
import {
registerStore,
registerReducer,
registerSelectors,
dispatch,
select,
} from '@wordpress/data';

describe( 'Gutenberg @wordress/data package', () => {
describe( 'registerStore', () => {
it( 'should be shorthand for reducer, actions, selectors registration', () => {
const DEFAULT_STATE = {
prices: { shirt: 5 },
discountPercent: 0,
};

const store = registerStore( 'my-shop', {
reducer( state = DEFAULT_STATE, action ) {
switch ( action.type ) {
case 'SET_PRICE':
return {
...state,
prices: {
...state.prices,
[ action.item ]: action.price,
},
};

case 'START_SALE':
return {
...state,
discountPercent: action.discountPercent,
};
}

return state;
},

actions: {
setPrice( item, price ) {
return {
type: 'SET_PRICE',
item,
price,
};
},
startSale( discountPercent ) {
return {
type: 'START_SALE',
discountPercent,
};
},
},

selectors: {
getPrice( state, item ) {
const { prices, discountPercent } = state;
const price = prices[ item ];

return price * ( 1 - 0.01 * discountPercent );
},
},
} );

expect( store.getState() ).toEqual( DEFAULT_STATE );
expect( select( 'my-shop' ) ).toHaveProperty( 'getPrice' );
expect( select( 'my-shop' ).getPrice( 'shirt' ) ).toEqual( 5 );
dispatch( 'my-shop' ).setPrice( 'shirt', 10 );
expect( select( 'my-shop' ).getPrice( 'shirt' ) ).toEqual( 10 );
dispatch( 'my-shop' ).startSale( 20 );
expect( select( 'my-shop' ).getPrice( 'shirt' ) ).toEqual( 8 );
} );
} );

describe( 'registerReducer', () => {
it( 'Should append reducers to the state', () => {
const reducer = () => 'tea';

const store = registerReducer( 'reducer', reducer );
expect( store.getState() ).toEqual( 'tea' );
} );
} );

describe( 'select', () => {
it( 'registers multiple selectors to the public API', () => {
const store = registerReducer( 'reducer', () => {} );
const selector = jest.fn( () => 'test-result' );

registerSelectors( 'reducer', { selector } );

expect( select( 'reducer' ).selector() ).toEqual( 'test-result' );
expect( selector ).toBeCalledWith( store.getState() );
} );
} );
} );
122 changes: 94 additions & 28 deletions npm-shrinkwrap.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@babel/preset-env": "7.0.0-beta.53",
"@babel/preset-react": "7.0.0-beta.53",
"@babel/runtime": "7.0.0-beta.53",
"@wordpress/data": "1.0.1",
"autoprefixer": "9.0.1",
"autosize": "4.0.2",
"babel-loader": "8.0.0-beta.4",
Expand Down

0 comments on commit 849bfb6

Please sign in to comment.