Skip to content

Commit

Permalink
test: add more data-fetching tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed Jun 25, 2024
1 parent c3cf144 commit d96e10c
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 18 deletions.
6 changes: 3 additions & 3 deletions packages/next/src/rsc/data/handleFetchError.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { LOGTYPE, fetchRedirect, getHeadstartWPConfig, log } from '@headstartwp/core';
import { HeadlessConfig, LOGTYPE, fetchRedirect, log } from '@headstartwp/core';
import { notFound, permanentRedirect, redirect } from 'next/navigation';

export async function handleFetchError(error: Error, path = '') {
const { redirectStrategy, sourceUrl, debug } = getHeadstartWPConfig();
export async function handleFetchError(error: Error, config: HeadlessConfig, path = '') {
const { redirectStrategy, sourceUrl, debug } = config;

if (debug?.devMode) {
log(LOGTYPE.INFO, '[handleError] error', error.name, error.message);
Expand Down
84 changes: 84 additions & 0 deletions packages/next/src/rsc/data/queries/__tests__/prepareQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { setHeadstartWPConfig } from '@headstartwp/core';
import { prepareQuery } from '../prepareQuery';

describe('prepareQuery', () => {
beforeAll(() => {
setHeadstartWPConfig({
sites: [
{
sourceUrl: 'https://backend1.com',
hostUrl: 'https://site1.com',
},
{
sourceUrl: 'https://backend2.com',
hostUrl: 'https://site2.com',
},
],
});
});

it('converts path to string', () => {
expect(
prepareQuery({ routeParams: { path: ['2020', '05', '07', 'post-name'] } }),
).toMatchObject({
path: '/2020/05/07/post-name',
});

expect(prepareQuery({ routeParams: { path: '/2020/05/07/post-name' } })).toMatchObject({
path: '/2020/05/07/post-name',
});
});

it('default headers to no-store', () => {
expect(prepareQuery({})).toMatchObject({
options: {
headers: {
cache: 'no-store',
},
},
});
});

it('gets site config based on site param', () => {
expect(
prepareQuery({
routeParams: { site: 'site1.com' },
}),
).toMatchObject({
config: {
sourceUrl: 'https://backend1.com',
hostUrl: 'https://site1.com',
},
});

expect(
prepareQuery({
routeParams: { site: 'site2.com' },
}),
).toMatchObject({
config: {
sourceUrl: 'https://backend2.com',
hostUrl: 'https://site2.com',
},
});

expect(
prepareQuery({
routeParams: { site: 'site2.com', path: ['post-name'] },
}),
).toMatchObject({
path: '/post-name',
config: {
sourceUrl: 'https://backend2.com',
hostUrl: 'https://site2.com',
},
});

// this one should throw
expect(() =>
prepareQuery({
routeParams: { site: 'site3.com', path: ['post-name'] },
}),
).toThrow('Sub site not found, make sure to add site3.com to headstartwp.config.js');
});
});
16 changes: 16 additions & 0 deletions packages/next/src/rsc/data/queries/__tests__/queryPost.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { queryPost } from '../queryPost';

describe('queryPosts', () => {
it('fetches posts', async () => {
const { data } = await queryPost({
routeParams: {
path: ['2020', '05', '07', 'modi-qui-dignissimos-sed-assumenda-sint-iusto'],
},
params: {
matchCurrentPath: false,
},
});

expect(data.post.slug).toBe('modi-qui-dignissimos-sed-assumenda-sint-iusto');
});
});
13 changes: 13 additions & 0 deletions packages/next/src/rsc/data/queries/__tests__/queryPosts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { queryPosts } from '../queryPosts';

describe('queryPosts', () => {
it('fetches posts', async () => {
const { data } = await queryPosts({
params: {
per_page: 2,
},
});

expect(data.posts).toHaveLength(2);
});
});
28 changes: 23 additions & 5 deletions packages/next/src/rsc/data/queries/prepareQuery.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { HeadlessConfig, getSiteByHost } from '@headstartwp/core';
import {
FrameworkError,
HeadlessConfig,
getHeadstartWPConfig,
getSiteByHost,
} from '@headstartwp/core';
import deepmerge from 'deepmerge';
import { convertToPath } from '../../../data/convertToPath';

import type { NextQueryProps } from './types';

const { all: merge } = deepmerge;

export function prepareQuery<P>(
query: NextQueryProps<P>,
_config: HeadlessConfig | undefined = undefined,
Expand All @@ -13,17 +21,27 @@ export function prepareQuery<P>(
const siteConfig = routeParams?.site ? getSiteByHost(routeParams?.site) : null;

if (routeParams?.site && !siteConfig) {
// improve this
throw new Error('Sub site not found');
throw new FrameworkError(
`Sub site not found, make sure to add ${routeParams?.site} to headstartwp.config.js`,
);
}

const config = siteConfig ?? _config;
const options = merge<NextQueryProps<P>['options']>([
{
headers: {
cache: 'no-store',
},
},
rest.options ?? {},
]);

const config = siteConfig ?? _config;
const pathname = Array.isArray(path) ? convertToPath(path) : path;

return {
...rest,
options,
path: pathname,
config,
config: config ?? getHeadstartWPConfig(),
};
}
4 changes: 1 addition & 3 deletions packages/next/src/rsc/data/queries/queryAppSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,13 @@ export async function queryAppSettings<
>(q: NextQueryProps<P>, _config: HeadlessConfig | undefined = undefined) {
const { config, ...query } = prepareQuery<P>(q, _config);

// TODO: do we need to handle errors?

try {
const result = await fetchAppSettings<T, P>(query, config);

return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/queryAuthorArchive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function queryAuthorArchive<
return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/queryPost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function queryPost<
return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/queryPostOrPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function queryPostOrPosts<
return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/queryPosts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function queryPosts<
return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/rsc/data/queries/querySearch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function querySearch<
return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/next/src/rsc/data/queries/queryTerms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ export async function queryTerms<
>(q: NextQueryProps<P>, _config: HeadlessConfig | undefined = undefined) {
const { config, ...query } = prepareQuery<P>(q, _config);

// TODO does this one need to fetch handle errors?
try {
const result = await fetchTerms<T, P>(query, config);

return result;
} catch (error) {
if (error instanceof Error) {
handleFetchError(error, query.path);
handleFetchError(error, config, query.path);
}
throw error;
}
Expand Down

0 comments on commit d96e10c

Please sign in to comment.