Skip to content

Commit

Permalink
test: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Aug 7, 2024
1 parent b03defd commit 6422ffc
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion packages/next/src/rsc/data/queries/__tests__/queryPost.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nextHeaders from 'next/headers';
import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '@headstartwp/core/test';
import { setHeadstartWPConfig } from '@headstartwp/core';
import { PostParams, setHeadstartWPConfig, SinglePostFetchStrategy } from '@headstartwp/core';
import { queryPost } from '../queryPost';
import { COOKIE_NAME } from '../../../handlers/previewRouteHandler';

Expand All @@ -17,6 +17,21 @@ const config = {
useWordPressPlugin: true,
};

class SinglePostFetchStrategyWithoutUrlParamAndHardcodedSlug extends SinglePostFetchStrategy {
hardcodedSlug = 'ipsum-repudiandae-est-nam';

getParamsFromURL(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
path: string,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
nonUrlParams?: Partial<PostParams> | undefined,
): Partial<PostParams> {
return {
slug: this.hardcodedSlug,
};
}
}

describe('queryPosts', () => {
beforeAll(() => {
setHeadstartWPConfig(config);
Expand Down Expand Up @@ -88,4 +103,21 @@ describe('queryPosts', () => {

expect(data.post.id).toBe(DRAFT_POST_ID);
});

it('allows overriding fetch strategy', async () => {
const fetchStrategy = new SinglePostFetchStrategyWithoutUrlParamAndHardcodedSlug();

const { data } = await queryPost({
routeParams: {
// this should be ignored bc of the custom fetch strategy
path: ['2020', '05', '07', 'modi-qui-dignissimos-sed-assumenda-sint-iusto'],
},
params: {
matchCurrentPath: false,
},
fetchStrategy,
});

expect(data.post.slug).toBe(fetchStrategy.hardcodedSlug);
});
});

0 comments on commit 6422ffc

Please sign in to comment.