Skip to content

Commit

Permalink
improve tests
Browse files Browse the repository at this point in the history
- use native jest each api
- improve naming of variables
- simplify mocking and test setup.
  • Loading branch information
nerrad committed Jul 27, 2019
1 parent 642d672 commit e2f6c28
Showing 1 changed file with 46 additions and 46 deletions.
92 changes: 46 additions & 46 deletions packages/data/src/components/use-select/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,34 +133,35 @@ describe( 'useSelect', () => {
} );
} );
describe( 'rerenders as expected with various mapSelect return types', () => {
const getComponent = ( selectSpy ) => () => {
selectSpy.mockImplementation(
( select ) => select( 'testStore' ).testSelector()
);
const data = useSelect( selectSpy, [] );
const getComponent = ( mapSelectSpy ) => () => {
const data = useSelect( mapSelectSpy, [] );
return <div data={ data } />;
};
let selectorSpy,
subscribedSpy,
subscriberSpy,
selectSpy,
TestComponent;
let subscribedSpy, TestComponent;
const mapSelectSpy = jest.fn(
( select ) => select( 'testStore' ).testSelector()
);
const selectorSpy = jest.fn();
const subscribeCallback = ( subscription ) => {
subscribedSpy = subscription;
};

beforeEach( () => {
selectorSpy = jest.fn();
subscriberSpy = ( subscription ) => {
subscribedSpy = subscription;
};
registry.registerStore( 'testStore', {
reducer: () => null,
selectors: {
testSelector: selectorSpy,
},
} );
registry.subscribe = subscriberSpy;
selectSpy = jest.fn();
TestComponent = getComponent( selectSpy );
registry.subscribe = subscribeCallback;
TestComponent = getComponent( mapSelectSpy );
} );
afterEach( () => {
selectorSpy.mockClear();
mapSelectSpy.mockClear();
} );
[

it.each( [
[
'boolean',
[ false, true ],
Expand Down Expand Up @@ -189,34 +190,33 @@ describe( 'useSelect', () => {
'undefined',
[ undefined, 42 ],
],
].forEach(
( [ type, testValues ] ) => {
const [ valueA, valueB ] = testValues;
it( `renders as expected with ${ type } return values`, () => {
selectorSpy.mockReturnValue( valueA );
let renderer;
act( () => {
renderer = TestRenderer.create(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);
} );
const testInstance = renderer.root;
// ensure expected state was rendered.
expect( testInstance.findByType( 'div' ).props.data ).toEqual( valueA );

// Update the returned value from the selector and trigger the
// subscription which should in turn trigger a re-render.
act( () => {
selectorSpy.mockReturnValue( valueB );
subscribedSpy();
} );
expect( testInstance.findByType( 'div' ).props.data ).toEqual( valueB );
expect( selectSpy ).toHaveBeenCalledTimes( 3 );
} );
}
);
] )( 'renders as expected with %s return values', (
type,
testValues,
) => {
const [ valueA, valueB ] = testValues;
selectorSpy.mockReturnValue( valueA );
let renderer;
act( () => {
renderer = TestRenderer.create(
<RegistryProvider value={ registry }>
<TestComponent />
</RegistryProvider>
);
} );
const testInstance = renderer.root;
// ensure expected state was rendered.
expect( testInstance.findByType( 'div' ).props.data ).toEqual( valueA );

// Update the returned value from the selector and trigger the
// subscription which should in turn trigger a re-render.
act( () => {
selectorSpy.mockReturnValue( valueB );
subscribedSpy();
} );
expect( testInstance.findByType( 'div' ).props.data ).toEqual( valueB );
expect( mapSelectSpy ).toHaveBeenCalledTimes( 3 );
} );
} );
} );

0 comments on commit e2f6c28

Please sign in to comment.