From bd70af95100aeb39ac6f5fb8002ec5ab2c52ff69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=ADcholas=20Oliveira?= Date: Tue, 9 Jul 2024 11:37:17 -0300 Subject: [PATCH] test: fixing more tests --- .../{useAppSettings.ts => useAppSettings.tsx} | 20 +++++++---- ...eAuthorArchive.ts => useAuthorArchive.tsx} | 20 ++++++++--- .../src/data/hooks/__tests__/usePosts.tsx | 33 ++++++++----------- .../data/hooks/__tests__/usePrepareFetch.tsx | 16 ++++----- .../__tests__/{useSearch.ts => useSearch.tsx} | 20 ++++++++--- .../__tests__/{useTerms.ts => useTerms.tsx} | 24 +++++++++++--- .../data/server/__tests__/fetchHookData.ts | 22 +++++++++++-- .../src/handlers/__tests__/previewHandler.ts | 12 +++++++ .../rsc/data/queries/__tests__/queryPost.ts | 10 ++++++ .../rsc/data/queries/__tests__/queryPosts.ts | 10 ++++-- 10 files changed, 134 insertions(+), 53 deletions(-) rename packages/next/src/data/hooks/__tests__/{useAppSettings.ts => useAppSettings.tsx} (58%) rename packages/next/src/data/hooks/__tests__/{useAuthorArchive.ts => useAuthorArchive.tsx} (65%) rename packages/next/src/data/hooks/__tests__/{useSearch.ts => useSearch.tsx} (65%) rename packages/next/src/data/hooks/__tests__/{useTerms.ts => useTerms.tsx} (57%) diff --git a/packages/next/src/data/hooks/__tests__/useAppSettings.ts b/packages/next/src/data/hooks/__tests__/useAppSettings.tsx similarity index 58% rename from packages/next/src/data/hooks/__tests__/useAppSettings.ts rename to packages/next/src/data/hooks/__tests__/useAppSettings.tsx index 3adc1d4af..3acb0812d 100644 --- a/packages/next/src/data/hooks/__tests__/useAppSettings.ts +++ b/packages/next/src/data/hooks/__tests__/useAppSettings.tsx @@ -1,15 +1,22 @@ import { setHeadstartWPConfig, type AppEntity, type EndpointParams } from '@headstartwp/core'; import { expectTypeOf } from 'expect-type'; import { renderHook } from '@testing-library/react'; +import * as React from 'react'; +import { SettingsProvider } from '@headstartwp/core/react'; import { useAppSettings } from '../useAppSettings'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('useAppSettings types', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); + const wrapper = ({ children }) => { + return {children}; + }; it('allows overriding types', () => { interface MyAppEntity extends AppEntity { @@ -20,8 +27,9 @@ describe('useAppSettings types', () => { includeCustomSettings: boolean; } - const { result } = renderHook(() => - useAppSettings({ includeCustomSettings: true }), + const { result } = renderHook( + () => useAppSettings({ includeCustomSettings: true }), + { wrapper }, ); expectTypeOf(result.current.data).toMatchTypeOf< diff --git a/packages/next/src/data/hooks/__tests__/useAuthorArchive.ts b/packages/next/src/data/hooks/__tests__/useAuthorArchive.tsx similarity index 65% rename from packages/next/src/data/hooks/__tests__/useAuthorArchive.ts rename to packages/next/src/data/hooks/__tests__/useAuthorArchive.tsx index 4e4ca01a2..bb79be6c8 100644 --- a/packages/next/src/data/hooks/__tests__/useAuthorArchive.ts +++ b/packages/next/src/data/hooks/__tests__/useAuthorArchive.tsx @@ -1,16 +1,24 @@ import { setHeadstartWPConfig, type PostEntity, type PostsArchiveParams } from '@headstartwp/core'; import { renderHook } from '@testing-library/react'; import { expectTypeOf } from 'expect-type'; +import * as React from 'react'; +import { SettingsProvider } from '@headstartwp/core/react'; import { useAuthorArchive } from '../useAuthorArchive'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('useAuthorArchive types', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); + const wrapper = ({ children }) => { + return {children}; + }; + it('allows overriding types', () => { interface Book extends PostEntity { isbn: string; @@ -20,7 +28,9 @@ describe('useAuthorArchive types', () => { isbn: string; } - const { result } = renderHook(() => useAuthorArchive({ isbn: 'sdasd' })); + const { result } = renderHook(() => useAuthorArchive({ isbn: 'sdasd' }), { + wrapper, + }); expectTypeOf(result.current.data?.posts).toMatchTypeOf< | Array<{ diff --git a/packages/next/src/data/hooks/__tests__/usePosts.tsx b/packages/next/src/data/hooks/__tests__/usePosts.tsx index ed10e3e97..37b2f465c 100644 --- a/packages/next/src/data/hooks/__tests__/usePosts.tsx +++ b/packages/next/src/data/hooks/__tests__/usePosts.tsx @@ -11,24 +11,20 @@ jest.mock('next/router', () => ({ useRouter: () => useRouterMock(), })); +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + +const wrapper = ({ children }) => { + return {children}; +}; + describe('usePosts', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); - const wrapper = ({ children }) => { - return ( - - {children} - - ); - }; - beforeAll(() => { useRouterMock.mockReturnValue({ query: { path: '' } }); }); @@ -50,10 +46,7 @@ describe('usePosts types', () => { }); beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); it('allows overriding types', () => { @@ -65,7 +58,9 @@ describe('usePosts types', () => { isbn: string; } - const { result } = renderHook(() => usePosts({ isbn: 'sdasd' })); + const { result } = renderHook(() => usePosts({ isbn: 'sdasd' }), { + wrapper, + }); expectTypeOf(result.current.data?.posts).toMatchTypeOf< | Array<{ diff --git a/packages/next/src/data/hooks/__tests__/usePrepareFetch.tsx b/packages/next/src/data/hooks/__tests__/usePrepareFetch.tsx index 99897b04b..1e33a62d2 100644 --- a/packages/next/src/data/hooks/__tests__/usePrepareFetch.tsx +++ b/packages/next/src/data/hooks/__tests__/usePrepareFetch.tsx @@ -10,12 +10,14 @@ jest.mock('next/router', () => ({ useRouter: () => useRouterMock(), })); +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('usePrepareFetch', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); it('injects locale if locale is set and polylang integration is enabled', () => { @@ -23,8 +25,7 @@ describe('usePrepareFetch', () => { return ( @@ -49,8 +50,7 @@ describe('usePrepareFetch', () => { return ( diff --git a/packages/next/src/data/hooks/__tests__/useSearch.ts b/packages/next/src/data/hooks/__tests__/useSearch.tsx similarity index 65% rename from packages/next/src/data/hooks/__tests__/useSearch.ts rename to packages/next/src/data/hooks/__tests__/useSearch.tsx index b8004be26..8f2337523 100644 --- a/packages/next/src/data/hooks/__tests__/useSearch.ts +++ b/packages/next/src/data/hooks/__tests__/useSearch.tsx @@ -1,14 +1,22 @@ import { setHeadstartWPConfig, type PostEntity, type PostsArchiveParams } from '@headstartwp/core'; import { renderHook } from '@testing-library/react'; +import * as React from 'react'; import { expectTypeOf } from 'expect-type'; +import { SettingsProvider } from '@headstartwp/core/react'; import { useSearch } from '../useSearch'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + +const wrapper = ({ children }) => { + return {children}; +}; + describe('useSearch types', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); it('allows overriding types', () => { @@ -20,7 +28,9 @@ describe('useSearch types', () => { isbn: string; } - const { result } = renderHook(() => useSearch({ isbn: 'sdasd' })); + const { result } = renderHook(() => useSearch({ isbn: 'sdasd' }), { + wrapper, + }); expectTypeOf(result.current.data?.posts).toMatchTypeOf< | Array<{ diff --git a/packages/next/src/data/hooks/__tests__/useTerms.ts b/packages/next/src/data/hooks/__tests__/useTerms.tsx similarity index 57% rename from packages/next/src/data/hooks/__tests__/useTerms.ts rename to packages/next/src/data/hooks/__tests__/useTerms.tsx index 7a46816b0..c0775b128 100644 --- a/packages/next/src/data/hooks/__tests__/useTerms.ts +++ b/packages/next/src/data/hooks/__tests__/useTerms.tsx @@ -4,17 +4,29 @@ import { type TermEntity, } from '@headstartwp/core'; import { renderHook } from '@testing-library/react'; +import * as React from 'react'; import { expectTypeOf } from 'expect-type'; +import { DataFetchingProvider, SettingsProvider } from '@headstartwp/core/react'; import { useTerms } from '../useTerms'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('useTerms types', () => { beforeAll(() => { - setHeadstartWPConfig({ - sourceUrl: 'https://js1.10up.com', - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); + const wrapper = ({ children }) => { + return ( + new Map() }} data={{}}> + {children} + + ); + }; + it('allows overriding types', () => { interface Genre extends TermEntity { editor: string; @@ -24,7 +36,9 @@ describe('useTerms types', () => { editor: string; } - const { result } = renderHook(() => useTerms({ editor: 'sdasd' })); + const { result } = renderHook(() => useTerms({ editor: 'sdasd' }), { + wrapper, + }); expectTypeOf(result.current.data?.terms).toMatchTypeOf< | Array<{ diff --git a/packages/next/src/data/server/__tests__/fetchHookData.ts b/packages/next/src/data/server/__tests__/fetchHookData.ts index 68e444bcf..9a3540bcf 100644 --- a/packages/next/src/data/server/__tests__/fetchHookData.ts +++ b/packages/next/src/data/server/__tests__/fetchHookData.ts @@ -9,7 +9,14 @@ import { usePostOrPosts } from '../../hooks/usePostOrPosts'; import { fetchHookData, prepareFetchHookData } from '../fetchHookData'; import { addHookData } from '../addHookData'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + test('fetchHookData types', async () => { + setHeadstartWPConfig(config); + expectTypeOf((await fetchHookData(usePosts.fetcher(), {})).data.result).toMatchTypeOf< PostEntity[] >(); @@ -32,6 +39,10 @@ test('fetchHookData types', async () => { }); describe('prepareFetchHookData', () => { + beforeAll(() => { + setHeadstartWPConfig(config); + }); + it('builds path correctly', () => { const { path } = prepareFetchHookData(usePosts.fetcher(), { params: { @@ -70,6 +81,7 @@ describe('prepareFetchHookData', () => { it('returns params properly', () => { setHeadstartWPConfig({ + ...config, useWordPressPlugin: true, integrations: { polylang: { @@ -98,6 +110,7 @@ describe('prepareFetchHookData', () => { expect(params).toEqual({ slug: 'page-name', _embed: true, id: 1, lang: 'pt' }); setHeadstartWPConfig({ + ...config, useWordPressPlugin: true, }); }); @@ -120,15 +133,20 @@ describe('prepareFetchHookData', () => { ); expect(cacheKey).toMatchObject({ - args: { _embed: true, id: 1, slug: 'page-name', sourceUrl: '' }, + args: { _embed: true, id: 1, slug: 'page-name', sourceUrl: config.sourceUrl }, url: '/wp-json/wp/v2/posts', }); }); }); describe('fetchHookData', () => { + beforeAll(() => { + setHeadstartWPConfig(config); + }); + it('handles additionalCacheObjects', async () => { setHeadstartWPConfig({ + ...config, useWordPressPlugin: true, }); @@ -152,7 +170,7 @@ describe('fetchHookData', () => { expect(addHookData([result], {})).toMatchObject({ props: { fallback: { - '#url:"/wp-json/wp/v2/posts",args:#taxonomy:"category",sourceUrl:"",category:"uncategorized",_embed:true,,': + '#url:"/wp-json/wp/v2/posts",args:#taxonomy:"category",sourceUrl:"https://js1.10up.com",category:"uncategorized",_embed:true,,': { pageInfo: { page: 1, diff --git a/packages/next/src/handlers/__tests__/previewHandler.ts b/packages/next/src/handlers/__tests__/previewHandler.ts index a12af6001..3d0b77c81 100644 --- a/packages/next/src/handlers/__tests__/previewHandler.ts +++ b/packages/next/src/handlers/__tests__/previewHandler.ts @@ -4,7 +4,16 @@ import { removeSourceUrl, setHeadstartWPConfig } from '@headstartwp/core'; import { NextApiRequest, NextApiResponse } from 'next'; import { previewHandler } from '../previewHandler'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('previewHandler', () => { + beforeAll(() => { + setHeadstartWPConfig(config); + }); + it('does not accepts POST requests', async () => { const { req, res } = createMocks({ method: 'POST', @@ -83,6 +92,7 @@ describe('previewHandler', () => { it('preview works for custom post types', async () => { setHeadstartWPConfig({ + ...config, customPostTypes: [ { slug: 'book', @@ -291,6 +301,7 @@ describe('previewHandler', () => { it('use post.link when preview.usePostLinkForRedirect is true', async () => { setHeadstartWPConfig({ + ...config, preview: { usePostLinkForRedirect: true }, }); @@ -314,6 +325,7 @@ describe('previewHandler', () => { }); setHeadstartWPConfig({ + ...config, preview: { usePostLinkForRedirect: false }, }); }); diff --git a/packages/next/src/rsc/data/queries/__tests__/queryPost.ts b/packages/next/src/rsc/data/queries/__tests__/queryPost.ts index 4b299becc..5dd709d9f 100644 --- a/packages/next/src/rsc/data/queries/__tests__/queryPost.ts +++ b/packages/next/src/rsc/data/queries/__tests__/queryPost.ts @@ -1,5 +1,6 @@ import nextHeaders from 'next/headers'; import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '@headstartwp/core/test'; +import { setHeadstartWPConfig } from '@headstartwp/core'; import { queryPost } from '../queryPost'; import { COOKIE_NAME } from '../../../handlers/previewRouteHandler'; @@ -11,7 +12,16 @@ jest.mock('next/headers', () => ({ })), })); +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('queryPosts', () => { + beforeAll(() => { + setHeadstartWPConfig(config); + }); + it('fetches posts', async () => { const { data } = await queryPost({ routeParams: { diff --git a/packages/next/src/rsc/data/queries/__tests__/queryPosts.ts b/packages/next/src/rsc/data/queries/__tests__/queryPosts.ts index e13a03276..6b925398a 100644 --- a/packages/next/src/rsc/data/queries/__tests__/queryPosts.ts +++ b/packages/next/src/rsc/data/queries/__tests__/queryPosts.ts @@ -1,15 +1,19 @@ import { setHeadstartWPConfig } from '@headstartwp/core'; import { queryPosts } from '../queryPosts'; +const config = { + sourceUrl: 'https://js1.10up.com', + useWordPressPlugin: true, +}; + describe('queryPosts', () => { beforeAll(() => { - setHeadstartWPConfig({ - useWordPressPlugin: true, - }); + setHeadstartWPConfig(config); }); afterAll(() => { setHeadstartWPConfig({ + ...config, useWordPressPlugin: false, }); });