diff --git a/packages/next/src/rsc/data/queries/__tests__/queryPost.ts b/packages/next/src/rsc/data/queries/__tests__/queryPost.ts index 5dd709d9f..5adabfa08 100644 --- a/packages/next/src/rsc/data/queries/__tests__/queryPost.ts +++ b/packages/next/src/rsc/data/queries/__tests__/queryPost.ts @@ -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'; @@ -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 | undefined, + ): Partial { + return { + slug: this.hardcodedSlug, + }; + } +} + describe('queryPosts', () => { beforeAll(() => { setHeadstartWPConfig(config); @@ -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); + }); });