Skip to content

Commit

Permalink
Add unit test for independent resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Dec 22, 2022
1 parent 55441a2 commit d09df1c
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions packages/data/src/components/use-select/test/suspense.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,76 @@ describe( 'useSuspenseSelect', () => {
expect( label ).toHaveTextContent( 'resolution failed' );
expect( console ).toHaveErrored();
} );

it( 'independent resolutions do not cause unrelated rerenders', async () => {
const store = createReduxStore( 'test', {
reducer: ( state = {}, action ) => {
switch ( action.type ) {
case 'RECEIVE':
return { ...state, [ action.endpoint ]: action.data };
default:
return state;
}
},
selectors: {
getData: ( state, endpoint ) => state[ endpoint ],
},
resolvers: {
getData:
( endpoint ) =>
async ( { dispatch } ) => {
const delay = endpoint === 'slow' ? 30 : 10;
await new Promise( ( r ) =>
setTimeout( () => r(), delay )
);
dispatch( {
type: 'RECEIVE',
endpoint,
data: endpoint,
} );
},
},
} );

const registry = createRegistry();
registry.register( store );

const FastUI = jest.fn( () => {
const data = useSuspenseSelect(
( select ) => select( store ).getData( 'fast' ),
[]
);
return <div aria-label="fast loaded">{ data }</div>;
} );

const SlowUI = jest.fn( () => {
const data = useSuspenseSelect(
( select ) => select( store ).getData( 'slow' ),
[]
);
return <div aria-label="slow loaded">{ data }</div>;
} );

const App = () => (
<RegistryProvider value={ registry }>
<Suspense fallback="fast loading">
<FastUI />
</Suspense>
<Suspense fallback="slow loading">
<SlowUI />
</Suspense>
</RegistryProvider>
);

render( <App /> );

const fastLabel = await screen.findByLabelText( 'fast loaded' );
expect( fastLabel ).toHaveTextContent( 'fast' );

const slowLabel = await screen.findByLabelText( 'slow loaded' );
expect( slowLabel ).toHaveTextContent( 'slow' );

expect( FastUI ).toHaveBeenCalledTimes( 2 );
expect( SlowUI ).toHaveBeenCalledTimes( 2 );
} );
} );

1 comment on commit d09df1c

@github-actions
Copy link

@github-actions github-actions bot commented on d09df1c Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3758042665
📝 Reported issues:

Please sign in to comment.