Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasio committed May 14, 2024
1 parent 6f68ecc commit b4ee30c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
7 changes: 4 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"copy-webpack-plugin": "^10.2.4",
"expect-type": "^0.15.0",
"jest": "^29.0.3",
"next-router-mock": "^0.9.1",
"next-router-mock": "^0.9.13",
"node-mocks-http": "^1.14.1",
"ts-jest": "^29.0.1",
"typescript": "^5.4.2",
Expand Down
30 changes: 17 additions & 13 deletions packages/next/src/handlers/__tests__/previewHandler.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { createMocks } from 'node-mocks-http';
import { DRAFT_POST_ID, VALID_AUTH_TOKEN } from '@headstartwp/core/test';
import { removeSourceUrl, setHeadstartWPConfig } from '@headstartwp/core';
import { NextApiRequest, NextApiResponse } from 'next';
import { previewHandler } from '../previewHandler';

describe('previewHandler', () => {
it('does not accepts POST requests', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'POST',
query: {},
});
Expand All @@ -17,7 +18,7 @@ describe('previewHandler', () => {
});

it('rejects requests missing params', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: 1 },
});
Expand All @@ -29,7 +30,7 @@ describe('previewHandler', () => {
});

it('fails if a valid auth token is not provided', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: 'test', post_type: 'post' },
});
Expand All @@ -41,7 +42,7 @@ describe('previewHandler', () => {
});

it('works if a valid auth token is provided', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -57,7 +58,7 @@ describe('previewHandler', () => {
});

it('sets preview cookie path', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down Expand Up @@ -94,7 +95,7 @@ describe('previewHandler', () => {
],
});

const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -123,7 +124,10 @@ describe('previewHandler', () => {
'/book/modi-qui-dignissimos-sed-assumenda-sint-iusto-preview=true',
);

const { req: reqWithLocale, res: resWithLocale } = createMocks({
const { req: reqWithLocale, res: resWithLocale } = createMocks<
NextApiRequest,
NextApiResponse
>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -155,7 +159,7 @@ describe('previewHandler', () => {
});

it('sets preview cookie path with locale', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: {
post_id: DRAFT_POST_ID,
Expand Down Expand Up @@ -187,7 +191,7 @@ describe('previewHandler', () => {
});

it('set preview cookie path to all paths if onRedirect is passed without getRedirectPath', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -213,7 +217,7 @@ describe('previewHandler', () => {
});

it('set preview cookie path redirectPath if getRedirectPath is passed', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand All @@ -239,7 +243,7 @@ describe('previewHandler', () => {
});

it('correctly takes into account `options`', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down Expand Up @@ -271,7 +275,7 @@ describe('previewHandler', () => {
});

it('fails if post type is not defined', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'recipe' },
});
Expand All @@ -290,7 +294,7 @@ describe('previewHandler', () => {
preview: { usePostLinkForRedirect: true },
});

const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: DRAFT_POST_ID, token: VALID_AUTH_TOKEN, post_type: 'post' },
});
Expand Down
5 changes: 3 additions & 2 deletions packages/next/src/handlers/__tests__/revalidateHandler.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createMocks } from 'node-mocks-http';
import { NextApiRequest, NextApiResponse } from 'next';
import { revalidateHandler } from '../revalidateHandler';

describe('revalidateHandler', () => {
it('does not accepts POST requests', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'POST',
query: {},
});
Expand All @@ -15,7 +16,7 @@ describe('revalidateHandler', () => {
});

it('rejects requests missing params', async () => {
const { req, res } = createMocks({
const { req, res } = createMocks<NextApiRequest, NextApiResponse>({
method: 'GET',
query: { post_id: 1 },
});
Expand Down

0 comments on commit b4ee30c

Please sign in to comment.