SWR 测试变异

huangapple go评论103阅读模式
英文:

SWR Test Mutation

问题

以下是您要翻译的内容:

What is a recommended way of testing mutation from SWR?

  1. const { data, mutate } = useSWR("/api/user", fetch);
  2. if (data.loggedIn) {
  3. return (
  4. <Button
  5. onClick={() => {
  6. logout();
  7. mutate(); // after logging in/out, we mutate the SWR
  8. }}
  9. >
  10. Logout
  11. </Button>
  12. );
  13. } else {
  14. // login
  15. return (<h1>Please login</h1>)
  16. }
  17. }```
  18. Related to discussion: https://github.com/vercel/swr/discussions/2434
  19. If we have given flow `logout -> mutate swr -> show login form`
  20. I think we shouldn't test implementation details and instead test what user actually see after mutation/change happened?
  21. ```expect(mutate).toHaveBeenCalled('/logout'); // bad
  22. expect(screen.getByText('Please login')).toBeInTheDocument(); // correct```
  23. What is the general feeling about this?
  24. <details>
  25. <summary>英文:</summary>
  26. What is a recommended way of testing mutation from SWR?

function Index() {
const { data, mutate } = useSWR("/api/user", fetch);

if (data.loggedIn) {
return (
<Button
onClick={() => {
logout();
mutate(); // after logging in/out, we mutate the SWR
}}
>
Logout
</Button>
);
} else {
// login
return (<h1>Please login</h1>)
}
}

  1. Related to discussion: https://github.com/vercel/swr/discussions/2434
  2. If we have given flow `logout -&gt; mutate swr -&gt; show login form`
  3. I think we shouldn&#39;t test implementation details and instead test what user actually see after mutation/change happened?

expect(mutate).toHaveBeenCalled('/logout'); // bad
expect(screen.getByText('Please login')).toBeInTheDocument(); // correct

  1. What is the general feeling about this?
  2. </details>
  3. # 答案1
  4. **得分**: 0
  5. 这应该可以工作,但是你可能要考虑模拟`fetch`而不是`mock fetch`
  6. ```javascript
  7. import { useSWRConfig } from 'swr';
  8. // FIXME: tests should be mocking the fetch instead
  9. // https://dev.to/bmvantunes/please-don-t-mock-swr-fetch-or-axios-in-your-unit-integration-tests-521k
  10. jest.mock('swr', () => ({
  11. ...jest.requireActual('swr'),
  12. useSWRConfig: jest.fn(() => ({
  13. mutate: () => {},
  14. })),
  15. }));
  16. it('should mutate data', async () => {
  17. const mockMutate = jest.fn();
  18. useSWRConfig.mockImplementationOnce(() => ({ mutate: mockMutate }));
  19. expect(mockMutate).toHaveBeenCalledWith('swrKey');
  20. ...
  21. });
英文:

This should work, however you want to consider to mock the fetch instead.

  1. import { useSWRConfig } from &#39;swr&#39;;
  2. // FIXME: tests should be mocking the fetch instead
  3. // https://dev.to/bmvantunes/please-don-t-mock-swr-fetch-or-axios-in-your-unit-integration-tests-521k
  4. jest.mock(&#39;swr&#39;, () =&gt; ({
  5. ...jest.requireActual(&#39;swr&#39;),
  6. useSWRConfig: jest.fn(() =&gt; ({
  7. mutate: () =&gt; {},
  8. })),
  9. }));
  10. it(&#39;should mutate data&#39;, async () =&gt; {
  11. const mockMutate = jest.fn();
  12. useSWRConfig.mockImplementationOnce(() =&gt; ({ mutate: mockMutate }));
  13. expect(mockMutate).toHaveBeenCalledWith(&#39;swrKey&#39;);
  14. ...
  15. });

huangapple
  • 本文由 发表于 2023年2月23日 23:43:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定