Skip to content

Commit

Permalink
test: fixing more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 9, 2024
1 parent e38627b commit bd70af9
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 53 deletions.
Original file line number Diff line number Diff line change
@@ -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 <SettingsProvider settings={config}>{children}</SettingsProvider>;
};

it('allows overriding types', () => {
interface MyAppEntity extends AppEntity {
Expand All @@ -20,8 +27,9 @@ describe('useAppSettings types', () => {
includeCustomSettings: boolean;
}

const { result } = renderHook(() =>
useAppSettings<MyAppEntity, Params>({ includeCustomSettings: true }),
const { result } = renderHook(
() => useAppSettings<MyAppEntity, Params>({ includeCustomSettings: true }),
{ wrapper },
);

expectTypeOf(result.current.data).toMatchTypeOf<
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <SettingsProvider settings={config}>{children}</SettingsProvider>;
};

it('allows overriding types', () => {
interface Book extends PostEntity {
isbn: string;
Expand All @@ -20,7 +28,9 @@ describe('useAuthorArchive types', () => {
isbn: string;
}

const { result } = renderHook(() => useAuthorArchive<Book, BookParams>({ isbn: 'sdasd' }));
const { result } = renderHook(() => useAuthorArchive<Book, BookParams>({ isbn: 'sdasd' }), {
wrapper,
});

expectTypeOf(result.current.data?.posts).toMatchTypeOf<
| Array<{
Expand Down
33 changes: 14 additions & 19 deletions packages/next/src/data/hooks/__tests__/usePosts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,20 @@ jest.mock('next/router', () => ({
useRouter: () => useRouterMock(),
}));

const config = {
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
};

const wrapper = ({ children }) => {
return <SettingsProvider settings={config}>{children}</SettingsProvider>;
};

describe('usePosts', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
setHeadstartWPConfig(config);
});

const wrapper = ({ children }) => {
return (
<SettingsProvider
settings={{ sourceUrl: 'https://js1.10up.com', useWordPressPlugin: true }}
>
{children}
</SettingsProvider>
);
};

beforeAll(() => {
useRouterMock.mockReturnValue({ query: { path: '' } });
});
Expand All @@ -50,10 +46,7 @@ describe('usePosts types', () => {
});

beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
setHeadstartWPConfig(config);
});

it('allows overriding types', () => {
Expand All @@ -65,7 +58,9 @@ describe('usePosts types', () => {
isbn: string;
}

const { result } = renderHook(() => usePosts<Book, BookParams>({ isbn: 'sdasd' }));
const { result } = renderHook(() => usePosts<Book, BookParams>({ isbn: 'sdasd' }), {
wrapper,
});

expectTypeOf(result.current.data?.posts).toMatchTypeOf<
| Array<{
Expand Down
16 changes: 8 additions & 8 deletions packages/next/src/data/hooks/__tests__/usePrepareFetch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,22 @@ 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', () => {
const wrapper = ({ children }) => {
return (
<SettingsProvider
settings={{
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
...config,
integrations: { polylang: { enable: true } },
}}
>
Expand All @@ -49,8 +50,7 @@ describe('usePrepareFetch', () => {
return (
<SettingsProvider
settings={{
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
...config,
integrations: { polylang: { enable: true } },
}}
>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <SettingsProvider settings={config}>{children}</SettingsProvider>;
};

describe('useSearch types', () => {
beforeAll(() => {
setHeadstartWPConfig({
sourceUrl: 'https://js1.10up.com',
useWordPressPlugin: true,
});
setHeadstartWPConfig(config);
});

it('allows overriding types', () => {
Expand All @@ -20,7 +28,9 @@ describe('useSearch types', () => {
isbn: string;
}

const { result } = renderHook(() => useSearch<Book, BookParams>({ isbn: 'sdasd' }));
const { result } = renderHook(() => useSearch<Book, BookParams>({ isbn: 'sdasd' }), {
wrapper,
});

expectTypeOf(result.current.data?.posts).toMatchTypeOf<
| Array<{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<DataFetchingProvider swrConfig={{ provider: () => new Map() }} data={{}}>
<SettingsProvider settings={config}>{children}</SettingsProvider>
</DataFetchingProvider>
);
};

it('allows overriding types', () => {
interface Genre extends TermEntity {
editor: string;
Expand All @@ -24,7 +36,9 @@ describe('useTerms types', () => {
editor: string;
}

const { result } = renderHook(() => useTerms<Genre, GenreParams>({ editor: 'sdasd' }));
const { result } = renderHook(() => useTerms<Genre, GenreParams>({ editor: 'sdasd' }), {
wrapper,
});

expectTypeOf(result.current.data?.terms).toMatchTypeOf<
| Array<{
Expand Down
22 changes: 20 additions & 2 deletions packages/next/src/data/server/__tests__/fetchHookData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
>();
Expand All @@ -32,6 +39,10 @@ test('fetchHookData types', async () => {
});

describe('prepareFetchHookData', () => {
beforeAll(() => {
setHeadstartWPConfig(config);
});

it('builds path correctly', () => {
const { path } = prepareFetchHookData(usePosts.fetcher(), {
params: {
Expand Down Expand Up @@ -70,6 +81,7 @@ describe('prepareFetchHookData', () => {

it('returns params properly', () => {
setHeadstartWPConfig({
...config,
useWordPressPlugin: true,
integrations: {
polylang: {
Expand Down Expand Up @@ -98,6 +110,7 @@ describe('prepareFetchHookData', () => {
expect(params).toEqual({ slug: 'page-name', _embed: true, id: 1, lang: 'pt' });

setHeadstartWPConfig({
...config,
useWordPressPlugin: true,
});
});
Expand All @@ -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,
});

Expand All @@ -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,
Expand Down
12 changes: 12 additions & 0 deletions packages/next/src/handlers/__tests__/previewHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<NextApiRequest, NextApiResponse>({
method: 'POST',
Expand Down Expand Up @@ -83,6 +92,7 @@ describe('previewHandler', () => {

it('preview works for custom post types', async () => {
setHeadstartWPConfig({
...config,
customPostTypes: [
{
slug: 'book',
Expand Down Expand Up @@ -291,6 +301,7 @@ describe('previewHandler', () => {

it('use post.link when preview.usePostLinkForRedirect is true', async () => {
setHeadstartWPConfig({
...config,
preview: { usePostLinkForRedirect: true },
});

Expand All @@ -314,6 +325,7 @@ describe('previewHandler', () => {
});

setHeadstartWPConfig({
...config,
preview: { usePostLinkForRedirect: false },
});
});
10 changes: 10 additions & 0 deletions packages/next/src/rsc/data/queries/__tests__/queryPost.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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: {
Expand Down
Loading

0 comments on commit bd70af9

Please sign in to comment.