Skip to content

Commit

Permalink
test: fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jul 9, 2024
1 parent a7dea3a commit e38627b
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { setHeadstartWPConfig } from '../../../utils';
import { AuthorArchiveFetchStrategy } from '../AuthorArchiveFetchStrategy';

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

describe('SearchFetchStrategy', () => {
let fetchStrategy: AuthorArchiveFetchStrategy;

beforeEach(() => {
fetchStrategy = new AuthorArchiveFetchStrategy();
fetchStrategy = new AuthorArchiveFetchStrategy(config.sourceUrl);
setHeadstartWPConfig(config);
});

it('parses the url properly', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ import { NotFoundError, setHeadstartWPConfig } from '../../../utils';
import { PostOrPostsFetchStrategy, PostOrPostsParams } from '../PostOrPostsFetchStrategy';
import { PostEntity } from '../../types';

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

describe('PostOrPostsFetchStrategy', () => {
let fetchStrategy: PostOrPostsFetchStrategy;

beforeEach(() => {
fetchStrategy = new PostOrPostsFetchStrategy('');
setHeadstartWPConfig(config);
fetchStrategy = new PostOrPostsFetchStrategy(config.sourceUrl);
});

it('returns `@postOrPosts` as the default endpoint', () => {
Expand Down Expand Up @@ -98,11 +104,6 @@ describe('PostOrPostsFetchStrategy', () => {
});

it('fetches the proper resource', async () => {
setHeadstartWPConfig({
sourceUrl: '',
useWordPressPlugin: true,
});

let params = fetchStrategy.getParamsFromURL('/');
let response = await fetchStrategy.fetcher('', merge(params, { priority: 'archive' }));
expect(response.result.isArchive).toBeTruthy();
Expand Down Expand Up @@ -152,10 +153,6 @@ describe('PostOrPostsFetchStrategy', () => {
'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/',
Expand All @@ -172,7 +169,6 @@ describe('PostOrPostsFetchStrategy', () => {

// 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(
Expand All @@ -186,14 +182,9 @@ describe('PostOrPostsFetchStrategy', () => {
'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 #54 - "https://js1.10up.com/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam/" 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 returned 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',
'/2020/05/07/distinctio-rerum-ratione-maxime-repudiandae-laboriosam-quam',
p,
),
p,
Expand Down Expand Up @@ -345,17 +336,12 @@ describe('PostOrPostsFetchStrategy', () => {
});

it('normalizes data for caching', async () => {
setHeadstartWPConfig({
sourceUrl: '',
useWordPressPlugin: true,
});

let params = merge(fetchStrategy.getParamsFromURL('/'), { priority: 'single' });
let response = await fetchStrategy.fetcher('', params);

let normalizedResponse = fetchStrategy.normalizeForCache(response, params);
expect(normalizedResponse.additionalCacheObjects?.[0].key).toStrictEqual({
args: { _embed: true, sourceUrl: '' },
args: { _embed: true, sourceUrl: config.sourceUrl },
url: '/wp-json/wp/v2/posts',
});

Expand All @@ -375,7 +361,7 @@ describe('PostOrPostsFetchStrategy', () => {
normalizedResponse = fetchStrategy.normalizeForCache(response, params);

expect(normalizedResponse.additionalCacheObjects?.[0].key).toStrictEqual({
args: { _embed: true, ...params.single, sourceUrl: '' },
args: { _embed: true, ...params.single, sourceUrl: config.sourceUrl },
url: '/wp-json/wp/v2/posts',
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { setHeadstartWPConfig } from '../../../utils';
import { SearchFetchStrategy } from '../SearchFetchStrategy';

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

describe('SearchFetchStrategy', () => {
let fetchStrategy: SearchFetchStrategy;

beforeEach(() => {
fetchStrategy = new SearchFetchStrategy();
fetchStrategy = new SearchFetchStrategy(config.sourceUrl);
setHeadstartWPConfig(config);
});

it('parses the url properly', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { SearchNativeFetchStrategy } from '../SearchNativeFetchStrategy';
import { setHeadstartWPConfig } from '../../../utils';

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

describe('SearchNativeFetchStrategy', () => {
let fetchStrategy: SearchNativeFetchStrategy;

beforeEach(() => {
fetchStrategy = new SearchNativeFetchStrategy();

setHeadstartWPConfig({});
setHeadstartWPConfig(config);
});

it('parse url properly', async () => {
Expand Down
22 changes: 13 additions & 9 deletions packages/core/src/react/hooks/__tests__/useFetchAppSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,20 @@ import { mockUseFetchErrorResponse } from '../mocks';
import { SettingsProvider } from '../../provider';
import { setHeadstartWPConfig } from '../../../utils';

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

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

const wrapper = ({ children }) => {
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider
settings={{ sourceUrl: 'https://js1.10up.com', useWordPressPlugin: true }}
>
{children}
</SettingsProvider>
<SettingsProvider settings={config}>{children}</SettingsProvider>
</SWRConfig>
);
};
Expand All @@ -35,8 +36,9 @@ describe('useFetchAppSettings types', () => {
includeCustomSettings: boolean;
}

const { result } = renderHook(() =>
useFetchAppSettings<MyAppEntity, Params>({ includeCustomSettings: true }),
const { result } = renderHook(
() => useFetchAppSettings<MyAppEntity, Params>({ includeCustomSettings: true }),
{ wrapper },
);
expectTypeOf(result.current.data).toMatchTypeOf<
| {
Expand All @@ -50,7 +52,9 @@ describe('useFetchAppSettings types', () => {
const spyUseFetch = jest
.spyOn(useFetchModule, 'useFetch')
.mockReturnValueOnce(mockUseFetchErrorResponse);
const { result } = renderHook(() => useFetchAppSettings({ includeCustomSettings: true }));
const { result } = renderHook(() => useFetchAppSettings({ includeCustomSettings: true }), {
wrapper,
});

const expectedKeys = ['error', 'loading', 'data', 'isMainQuery', 'mutate'];
const returnedKeys = Object.keys(result.current);
Expand Down
25 changes: 24 additions & 1 deletion packages/core/src/react/hooks/__tests__/useFetchTerms.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,31 @@
import { renderHook, waitFor } from '@testing-library/react';
import { expectTypeOf } from 'expect-type';
import { SWRConfig } from 'swr';
import * as React from 'react';
import { TaxonomyArchiveParams, TermEntity } from '../../../data';
import { useFetchTerms } from '../useFetchTerms';
import * as useFetchModule from '../useFetch';
import { mockUseFetchErrorResponse } from '../mocks';
import { setHeadstartWPConfig } from '../../../utils';
import { SettingsProvider } from '../../provider';

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

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

const wrapper = ({ children }) => {
return (
<SWRConfig value={{ provider: () => new Map() }}>
<SettingsProvider settings={config}>{children}</SettingsProvider>
</SWRConfig>
);
};
it('allows overriding types', () => {
interface Genre extends TermEntity {
editor: string;
Expand All @@ -15,7 +35,10 @@ describe('useFetchTerms types', () => {
editor: string;
}

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

expectTypeOf(result.current.data?.terms).toMatchTypeOf<
| Array<{
Expand Down

0 comments on commit e38627b

Please sign in to comment.