-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useEntityRecord: Do not trigger REST API requests when disabled #56108
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -113,4 +113,38 @@ describe( 'useEntityRecord', () => { | |||||||||||||||||||
expect( widget.editedRecord ).toEqual( { hello: 'foo', id: 1 } ); | ||||||||||||||||||||
expect( widget.edits ).toEqual( { hello: 'foo' } ); | ||||||||||||||||||||
} ); | ||||||||||||||||||||
|
||||||||||||||||||||
it( 'does not resolve entity record when disabled via options', async () => { | ||||||||||||||||||||
// Provide response | ||||||||||||||||||||
triggerFetch.mockImplementation( () => TEST_RECORD ); | ||||||||||||||||||||
|
||||||||||||||||||||
let data; | ||||||||||||||||||||
const TestComponent = () => { | ||||||||||||||||||||
data = useEntityRecord( 'root', 'widget', 2, { | ||||||||||||||||||||
options: { enabled: false }, | ||||||||||||||||||||
} ); | ||||||||||||||||||||
return <div />; | ||||||||||||||||||||
}; | ||||||||||||||||||||
render( | ||||||||||||||||||||
<RegistryProvider value={ registry }> | ||||||||||||||||||||
<TestComponent /> | ||||||||||||||||||||
</RegistryProvider> | ||||||||||||||||||||
); | ||||||||||||||||||||
|
||||||||||||||||||||
expect( data ).toEqual( { | ||||||||||||||||||||
edit: expect.any( Function ), | ||||||||||||||||||||
editedRecord: {}, | ||||||||||||||||||||
hasEdits: false, | ||||||||||||||||||||
edits: {}, | ||||||||||||||||||||
record: null, | ||||||||||||||||||||
save: expect.any( Function ), | ||||||||||||||||||||
} ); | ||||||||||||||||||||
|
||||||||||||||||||||
// Fetch request should have been issued | ||||||||||||||||||||
await waitFor( () => | ||||||||||||||||||||
expect( triggerFetch ).not.toHaveBeenCalledWith( { | ||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should test what we expect and not something different. Probably use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's odd. This was failing without changes in my test. I'll have a loot at it tomorrow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the tests. They should fail on trunk but pass on this branch. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really weird :). I also tried yesterday to reset the mocks etc.., and for me the issue remains, but I think it's something we miss with the testing library. When I comment out your
(removing the not). The tests works as expected, but the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think cc @tyxla There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
await act(
() => new Promise( ( resolve ) => setTimeout( resolve, 0 ) )
);
expect( triggerFetch ).not.toHaveBeenCalled(); In this context, If we want to make this test more resilient to implementation details, then we can first make a "enabled" assertion ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It makes sense. Thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for coming late for this convo, but yes - what @kevin940726 said 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
@kevin940726, can you elaborate bit more about this part? 🙇 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! Because we want to make our unit tests as fast as possible, but we also don't want to just specify a random magic number ( const TestComponent = ({ enabled }) => {
data = useEntityRecord( 'root', 'widget', 2, { enabled } );
return <div />;
};
const UI = ({ enabled }) => (
<RegistryProvider value={ registry }>
<TestComponent enabled={enabled} />
</RegistryProvider>
);
// Smoke test.
const { rerender } = render(<UI enabled={true} />);
await act(() => new Promise(resolve => setTimeout(resolve, 0))); // The minimum delay.
expect(triggerFetch).toHaveBeenCalledTimes(1);
// Real test.
rerender(<UI enabled={false} />);
await act(() => new Promise(resolve => setTimeout(resolve, 0))); // The same delay.
expect(triggerFetch).toHaveBeenCalledTimes(1); If the minimum delay (implementation detail) is still shorter than
However, if the implementation detail changes to a longer delay (one animation frame for instance), the second assertion won't catch the regression and we'll need the first assertion (smoke test) to help us.
I think I'm overly complicating this now 😅. In short, the first assertion is just a smoke test to help us make sure the wait is enough for the hook to call |
||||||||||||||||||||
path: '/wp/v2/widgets/2?context=edit', | ||||||||||||||||||||
} ) | ||||||||||||||||||||
); | ||||||||||||||||||||
} ); | ||||||||||||||||||||
} ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just be:
Passing any object still works because we're only defaulting
enabled
totrue
ifoptions
isundefined
. Otherwise,options.enabled
will always be falsy (undefined
) unless explicitly set totrue
. I think this is another bug we should fix.