Skip to content

Commit

Permalink
test: fix and add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Nov 15, 2023
1 parent ae78a0f commit 57ce497
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/dom/__tests__/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Element } from 'html-react-parser';
import { isAnchorTag, isImageTag, isTwitterEmbed, isYoutubeEmbed } from '..';

jest.mock('../../utils/getHeadlessConfig', () => {
jest.mock('../../utils/config', () => {
return {
getWPUrl: () => 'https://backendurl.com',
};
Expand Down
122 changes: 116 additions & 6 deletions packages/core/src/utils/__tests__/config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import type { HeadlessConfig } from '../../types';
import { getHeadlessConfig, getSiteByHost, setHeadlessConfig } from '../config';
import {
getCustomPostTypes,
getCustomTaxonomies,
getHeadstartWPConfig,
getSiteByHost,
setHeadstartWPConfig,
} from '../config';

describe('getHeadlessConfig', () => {
describe('getHeadstartWPConfig', () => {
const headlessConfig: HeadlessConfig = {
sourceUrl: 'https://sourceurl.com',
hostUrl: 'https://publicurl.com',
Expand All @@ -14,15 +20,119 @@ describe('getHeadlessConfig', () => {
};

beforeAll(() => {
setHeadlessConfig(headlessConfig);
setHeadstartWPConfig(headlessConfig);
});

it('returns the headless config', () => {
expect(getHeadlessConfig()).toMatchObject(headlessConfig);
expect(getHeadstartWPConfig()).toMatchObject(headlessConfig);
});

it('sets host for sites if host is not set and hostUrl is', () => {
expect(getHeadlessConfig()?.sites?.[0]?.host).toBe('site1.com');
expect(getHeadstartWPConfig()?.sites?.[0]?.host).toBe('site1.com');
});

it('returns the default customTaxonomies', () => {
expect(getCustomTaxonomies()).toStrictEqual([
{ endpoint: '/wp-json/wp/v2/categories', restParam: 'categories', slug: 'category' },
{
endpoint: '/wp-json/wp/v2/tags',
restParam: 'tags',
rewrite: 'tag',
slug: 'post_tag',
},
]);
});

it('returns the default customPostTypes', () => {
expect(getCustomPostTypes()).toStrictEqual([
{ endpoint: '/wp-json/wp/v2/pages', single: '/', slug: 'page' },
{ archive: '/blog', endpoint: '/wp-json/wp/v2/posts', single: '/', slug: 'post' },
]);
});

it('accepts an array for customTaxonomies', () => {
setHeadstartWPConfig({
...headlessConfig,
customTaxonomies: [
{
slug: 'genre',
endpoint: '/wp-json/wp/v2/genre',
},
],
});

expect(getCustomTaxonomies()).toStrictEqual([
{
slug: 'genre',
endpoint: '/wp-json/wp/v2/genre',
},
{ endpoint: '/wp-json/wp/v2/categories', restParam: 'categories', slug: 'category' },
{
endpoint: '/wp-json/wp/v2/tags',
restParam: 'tags',
rewrite: 'tag',
slug: 'post_tag',
},
]);
});

it('accepts an array for customPostTypes', () => {
setHeadstartWPConfig({
...headlessConfig,
customPostTypes: [
{
slug: 'book',
endpoint: '/wp-json/wp/v2/book',
single: '/book',
archive: '/books',
},
],
});

expect(getCustomPostTypes()).toStrictEqual([
{
slug: 'book',
endpoint: '/wp-json/wp/v2/book',
single: '/book',
archive: '/books',
},
{ endpoint: '/wp-json/wp/v2/pages', single: '/', slug: 'page' },
{ archive: '/blog', endpoint: '/wp-json/wp/v2/posts', single: '/', slug: 'post' },
]);
});

it('accepts a function for customTaxonomies', () => {
setHeadstartWPConfig({
...headlessConfig,
customTaxonomies: (defaultTaxonomies) => {
return [
...defaultTaxonomies.map((taxonomy) => ({
...taxonomy,
matchArchivePath: true,
})),
];
},
});

expect(getHeadstartWPConfig().customTaxonomies.at(0)?.matchArchivePath).toBe(true);
expect(getHeadstartWPConfig().customTaxonomies.at(1)?.matchArchivePath).toBe(true);
});

it('accepts a function for customPostTypes', () => {
setHeadstartWPConfig({
...headlessConfig,
customPostTypes: (defaultPostTypes) => {
return [
...defaultPostTypes.map((postType) => ({
...postType,
matchSinglePath: false,
})),
];
},
});

expect(getHeadstartWPConfig().customPostTypes.at(0)?.matchSinglePath).toBe(false);
expect(getHeadstartWPConfig().customPostTypes.at(1)?.matchSinglePath).toBe(false);
});
});

Expand All @@ -46,7 +156,7 @@ describe('getSiteByHost', () => {
};

beforeAll(() => {
setHeadlessConfig(headlessConfig);
setHeadstartWPConfig(headlessConfig);
});

it('finds sites by host even if host is not set but hostUrl is', () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/__tests__/isInternalLink.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isInternalLink } from '..';
import { HeadlessConfig } from '../../types';

jest.mock('../getHeadlessConfig', () => {
jest.mock('../config', () => {
return {
getWPUrl: () => 'https://backendurl.com',
};
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function getHeadstartWPConfig() {
debug,
} = __10up__HEADLESS_CONFIG;

const defaultTaxonomies = [
const defaultTaxonomies: CustomTaxonomies = [
{
slug: 'category',
endpoint: endpoints.category,
Expand All @@ -51,7 +51,7 @@ export function getHeadstartWPConfig() {
? customTaxonomies(defaultTaxonomies)
: [...(customTaxonomies || []), ...defaultTaxonomies];

const defaultPostTypes = [
const defaultPostTypes: CustomPostTypes = [
{
slug: 'page',
endpoint: '/wp-json/wp/v2/pages',
Expand Down Expand Up @@ -255,14 +255,14 @@ export function getCustomPostType(slug: string, sourceUrl?: string) {
* Returns the WP URL based on the headless config
*/
export function getWPUrl() {
const { sourceUrl } = getHeadlessConfig();
const { sourceUrl } = getHeadstartWPConfig();
return sourceUrl || '';
}

/**
* Returns the WP URL based on the headless config
*/
export function getHostUrl() {
const { hostUrl } = getHeadlessConfig();
const { hostUrl } = getHeadstartWPConfig();
return hostUrl || '';
}

0 comments on commit 57ce497

Please sign in to comment.