Skip to content

Commit

Permalink
test: more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 25, 2023
1 parent b3f7ad7 commit 6bcb417
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface PostOrPostsParams extends EndpointParams {
* - `archive` will only trigger the archive strategy if there's a url match for the archive strategy
* - `both` requires a route match for both single and archive
*/
routeMatchStrategy: 'single' | 'archive' | 'both';
routeMatchStrategy: 'none' | 'single' | 'archive' | 'both';
}

export type PostOrPostsFetchStrategyResult<T> = {
Expand Down
77 changes: 75 additions & 2 deletions packages/core/src/data/strategies/__tests__/PostOrPostsStrategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('PostOrPostsFetchStrategy', () => {
});
});

it('fetches the proper resource with archive priority', async () => {
it('fetches the proper resource with archive priority and routeMatchStrategy set to archive', async () => {
setHeadstartWPConfig({
sourceUrl: '',
useWordPressPlugin: true,
Expand All @@ -115,6 +115,9 @@ describe('PostOrPostsFetchStrategy', () => {
);
expect(response.result.isArchive).toBeFalsy();

// simulate something like /src/pages/blog/[...path].js
// whhich would supports paths like `/blog/category-name`
// `/blog/post-name` and even `/blog/category-name/post-name`
const p: PostOrPostsParams = {
archive: { taxonomy: 'category' },
single: {},
Expand All @@ -128,9 +131,79 @@ describe('PostOrPostsFetchStrategy', () => {
expect(response.result.data.length).toBeGreaterThan(0);

(response.result.data as PostEntity[]).forEach((post) => {
// 5 is the id of the uncategorized category
// 1 is the id of the uncategorized category
expect(post.categories?.flat()).toContain(1);
});

// let's skip matching current path for this test
p.single.matchCurrentPath = false;

params = merge(
fetchStrategy.getParamsFromURL(
'/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
p,
),
p,
);

response = await fetchStrategy.fetcher('', params);
expect(response.result.isSingle).toBeTruthy();
expect((response.result.data as PostEntity).slug).toBe(
'distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
);

// this is to force post path nmapping to work
p.single.fullPath =
'https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/';

params = merge(
fetchStrategy.getParamsFromURL(
'/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/',
p,
),
p,
);

response = await fetchStrategy.fetcher('', params);
expect(response.result.isSingle).toBeTruthy();
expect((response.result.data as PostEntity).slug).toBe(
'distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
);

// with this set to true it should error out if the path does not match
p.single.matchCurrentPath = true;
delete p.single.fullPath;

params = merge(
fetchStrategy.getParamsFromURL(
'/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
p,
),
p,
);

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"',
);

// now make it work by faking full path
// this simulates the post url returnied by wp matching the front-end url
p.single.fullPath =
'https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/';

params = merge(
fetchStrategy.getParamsFromURL(
'/uncategorized/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
p,
),
p,
);

response = await fetchStrategy.fetcher('', params);
expect(response.result.isSingle).toBeTruthy();
expect((response.result.data as PostEntity).slug).toBe(
'distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
);
});

it('fetches the proper resource with single priority', async () => {
Expand Down

0 comments on commit 6bcb417

Please sign in to comment.