Skip to content

Commit

Permalink
test: provide a separate cache for each test (#654)
Browse files Browse the repository at this point in the history
Partially addresses #643 

### Checklist:
<!--- Go over all the following points, and put an `x` in all the boxes
that apply. -->
<!--- If you are unsure about any of these, please ask for
clarification. We are here to help! -->
- [x] I agree to follow this project's [**Code of
Conduct**](https://github.com/10up/.github/blob/trunk/CODE_OF_CONDUCT.md).
- [ ] I have updated the documentation accordingly.
- [x] I have added tests to cover my change.
- [x] All new and existing tests pass.
  • Loading branch information
nicholasio authored Nov 27, 2023
2 parents 4c586cf + e541f66 commit 1b300ee
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ export class SinglePostFetchStrategy<

if (!post) {
throw new NotFoundError(
`Post was found but did not match current path: "${this.path}""`,
`Post #${result[0].id} - "${result[0].link}" was found but did not match current path: "${this.path}""`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe('PostOrPostsFetchStrategy', () => {
);

await expect(() => fetchStrategy.fetcher('', params)).rejects.toThrow(
'Neither single or archive returned data: The request to /wp-json/wp/v2/posts?_embed=true&categories=distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam returned no data, Post was found but did not match current path: "/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam"',
'Neither single or archive returned data: The request to /wp-json/wp/v2/posts?_embed=true&categories=distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam returned no data, Post #54 - "https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/" was found but did not match current path: "/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam"',
);

// now make it work by faking full path
Expand Down
19 changes: 16 additions & 3 deletions packages/core/src/react/hooks/__tests__/useFetchPost.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { renderHook, waitFor } from '@testing-library/react';
import * as React from 'react';
import { expectTypeOf } from 'expect-type';
import { SWRConfig } from 'swr';
import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '../../../../test/server';
import { PostEntity, PostParams } from '../../../data';
import { SettingsProvider } from '../../provider';
Expand All @@ -10,7 +11,11 @@ import { mockUseFetchErrorResponse } from '../mocks';

describe('useFetchPost', () => {
const wrapper = ({ children }) => {
return <SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>;
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>
</SWRConfig>
);
};

it('throws errors if accessing data before fetch', async () => {
Expand Down Expand Up @@ -185,7 +190,15 @@ describe('useFetchPost', () => {

it('reads param from the url and sets isMainQuery flag', async () => {
const { result } = renderHook(
() => useFetchPost({}, {}, '/modi-qui-dignissimos-sed-assumenda-sint-iusto'),
() =>
useFetchPost(
{
fullPath:
'https://js1.10up.com/2020/05/07/modi-qui-dignissimos-sed-assumenda-sint-iusto/',
},
{},
'/modi-qui-dignissimos-sed-assumenda-sint-iusto/',
),
{
wrapper,
},
Expand All @@ -200,7 +213,7 @@ describe('useFetchPost', () => {
});

const { result: secondResult } = renderHook(
() => useFetchPost({ slug: 'modi-qui-dignissimos-sed-assumenda-sint-iusto' }),
() => useFetchPost({ slug: 'modi-qui-dignissimos-sed-assumenda-sint-iusto' }, {}),
{
wrapper,
},
Expand Down
17 changes: 13 additions & 4 deletions packages/core/src/react/hooks/__tests__/useFetchPostOrPosts.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as React from 'react';
import { renderHook, waitFor } from '@testing-library/react';
import { SWRConfig } from 'swr';
import { SettingsProvider } from '../../provider';
import { setHeadstartWPConfig } from '../../../utils';
import { useFetchPostOrPosts } from '../useFetchPostOrPosts';
Expand All @@ -11,6 +12,14 @@ import { mockUseFetchErrorResponse } from '../mocks';

describe('useFetchPostOrPosts', () => {
const wrapper = ({ children }) => {
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>
</SWRConfig>
);
};

const wrapperWithCache = ({ children }) => {
return <SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>;
};

Expand Down Expand Up @@ -120,7 +129,7 @@ describe('useFetchPostOrPosts', () => {
'/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
),
{
wrapper,
wrapper: wrapperWithCache,
},
);

Expand All @@ -138,7 +147,7 @@ describe('useFetchPostOrPosts', () => {
'/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
),
{
wrapper,
wrapper: wrapperWithCache,
},
);

Expand All @@ -161,7 +170,7 @@ describe('useFetchPostOrPosts', () => {
};

const { result } = renderHook(() => useFetchPostOrPosts(p, undefined, '/uncategorized'), {
wrapper,
wrapper: wrapperWithCache,
});

await waitFor(() => {
Expand All @@ -171,7 +180,7 @@ describe('useFetchPostOrPosts', () => {
const { result: result2 } = renderHook(
() => useFetchPosts(p.archive, undefined, '/uncategorized'),
{
wrapper,
wrapper: wrapperWithCache,
},
);

Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/react/hooks/__tests__/useFetchPosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { renderHook, waitFor } from '@testing-library/react';
import { expectTypeOf } from 'expect-type';
import * as React from 'react';

import { SWRConfig } from 'swr';
import { PostEntity, PostsArchiveParams } from '../../../data';
import { SettingsProvider } from '../../provider';
import { useFetchPosts } from '../useFetchPosts';
Expand All @@ -11,7 +12,11 @@ import { mockUseFetchErrorResponse } from '../mocks';

describe('useFetchPosts', () => {
const wrapper = ({ children }) => {
return <SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>;
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>
</SWRConfig>
);
};

beforeEach(() => {
Expand Down Expand Up @@ -338,7 +343,7 @@ describe('useFetchPosts', () => {
() =>
useFetchPosts(
{
randomArgument: 10, // bypass swr cache
// randomArgument: 10, // bypass swr cache
category: 'uncategorized',
per_page: 1,
},
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/react/hooks/__tests__/useFetchSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { renderHook, waitFor } from '@testing-library/react';
import { expectTypeOf } from 'expect-type';
import * as React from 'react';

import { SWRConfig } from 'swr';
import { PostEntity, PostsArchiveParams } from '../../../data';
import { SettingsProvider } from '../../provider';
import { useFetchSearch } from '../useFetchSearch';
Expand All @@ -11,7 +12,11 @@ import { mockUseFetchErrorResponse } from '../mocks';

describe('useFetchSearch', () => {
const wrapper = ({ children }) => {
return <SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>;
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider settings={{ sourceUrl: '' }}>{children}</SettingsProvider>
</SWRConfig>
);
};

setHeadlessConfig({
Expand Down

1 comment on commit 1b300ee

@vercel
Copy link

@vercel vercel bot commented on 1b300ee Nov 27, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.