Skip to content

Commit

Permalink
fix(next-drupal)!: upgrade tests to new async request objects
Browse files Browse the repository at this point in the history
  • Loading branch information
yobottehg committed Dec 6, 2024
1 parent e85ffb1 commit 60257e5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 29 deletions.
12 changes: 6 additions & 6 deletions packages/next-drupal/src/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ export async function enableDraftMode(
const path = searchParams.get("path")

const cookieStore = await cookies()
const draftModeStore = await draftMode()
// Enable Draft Mode by setting the cookie
draftModeStore.enable()
;(await draftMode()).enable()

// Override the default SameSite=lax.
// See https://github.com/vercel/next.js/issues/49927
Expand Down Expand Up @@ -58,9 +57,8 @@ export async function enableDraftMode(

export async function disableDraftMode() {
const cookieStore = await cookies()
const draftModeStore = await draftMode()
cookieStore.delete(DRAFT_DATA_COOKIE_NAME)
draftModeStore.disable()
;(await draftMode()).disable()

return new Response("Draft mode is disabled")
}
Expand All @@ -73,8 +71,10 @@ export interface DraftData {
export async function getDraftData() {
let data: DraftData = {}
const cookieStore = await cookies()
const draftModeStore = await draftMode()
if (draftModeStore.isEnabled && cookieStore.has(DRAFT_DATA_COOKIE_NAME)) {
if (
(await draftMode()).isEnabled &&
cookieStore.has(DRAFT_DATA_COOKIE_NAME)
) {
data = JSON.parse(cookieStore.get(DRAFT_DATA_COOKIE_NAME)?.value || "{}")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
JsonApiResourceWithPath,
NextDrupalAuth,
} from "../../src"

import { NextApiRequest, NextApiResponse } from "../__mocks__/next"
jest.setTimeout(10000)

afterEach(() => {
Expand Down Expand Up @@ -1117,13 +1117,12 @@ describe("preview()", () => {
}

test("turns on preview mode and clears preview data", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)
spyOnFetch({ responseBody: validationPayload })

// @ts-expect-error
await drupal.preview(request, response)

expect(response.clearPreviewData).toBeCalledTimes(1)
Expand All @@ -1136,9 +1135,7 @@ describe("preview()", () => {

test("does not enable preview mode if validation fails", async () => {
const logger = mockLogger()
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL, { debug: true, logger })
const status = 403
Expand All @@ -1151,6 +1148,7 @@ describe("preview()", () => {
},
})

// @ts-expect-error
await drupal.preview(request, response)

expect(logger.debug).toBeCalledWith(
Expand All @@ -1162,13 +1160,12 @@ describe("preview()", () => {
})

test("does not turn on draft mode by default", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)
spyOnFetch({ responseBody: validationPayload })

// @ts-expect-error
await drupal.preview(request, response)

expect(response.setDraftMode).toBeCalledTimes(0)
Expand All @@ -1179,9 +1176,7 @@ describe("preview()", () => {
})

test("optionally turns on draft mode", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const logger = mockLogger()
const drupal = new NextDrupalPages(BASE_URL, {
Expand All @@ -1191,6 +1186,7 @@ describe("preview()", () => {
spyOnFetch({ responseBody: validationPayload })

const options = { enable: true }
// @ts-expect-error
await drupal.preview(request, response, options)

expect(response.setDraftMode).toBeCalledWith(options)
Expand All @@ -1203,9 +1199,7 @@ describe("preview()", () => {
})

test("updates preview mode cookie’s sameSite flag", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)
spyOnFetch({ responseBody: validationPayload })
Expand All @@ -1221,6 +1215,7 @@ describe("preview()", () => {
const cookies = response.getHeader("Set-Cookie")
cookies[0] = cookies[0].replace("SameSite=Lax", "SameSite=None; Secure")

// @ts-expect-error
await drupal.preview(request, response)

expect(response.getHeader).toHaveBeenLastCalledWith("Set-Cookie")
Expand All @@ -1229,14 +1224,13 @@ describe("preview()", () => {
})

test("redirects to the given path", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const logger = mockLogger()
const drupal = new NextDrupalPages(BASE_URL, { debug: true, logger })
spyOnFetch({ responseBody: validationPayload })

// @ts-expect-error
await drupal.preview(request, response)

expect(response.setPreviewData).toBeCalledWith({
Expand All @@ -1249,9 +1243,7 @@ describe("preview()", () => {
})

test("returns a 422 response on error", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const logger = mockLogger()
const drupal = new NextDrupalPages(BASE_URL, { debug: true, logger })
Expand All @@ -1260,6 +1252,7 @@ describe("preview()", () => {
throw new Error(message)
})

// @ts-expect-error
await drupal.preview(request, response)

expect(logger.debug).toHaveBeenLastCalledWith(`Preview failed: ${message}`)
Expand All @@ -1270,34 +1263,31 @@ describe("preview()", () => {

describe("previewDisable()", () => {
test("clears preview data", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)

// @ts-expect-error
await drupal.previewDisable(request, response)
expect(response.clearPreviewData).toBeCalledTimes(1)
})

test("disables draft mode", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)

// @ts-expect-error
await drupal.previewDisable(request, response)
expect(response.setDraftMode).toBeCalledWith({ enable: false })
})

test("deletes the draft cookie", async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)

// @ts-expect-error
await drupal.previewDisable(request, response)
const cookies = response.getHeader("Set-Cookie")
expect(cookies[cookies.length - 1]).toBe(
Expand All @@ -1306,12 +1296,11 @@ describe("previewDisable()", () => {
})

test('redirects to "/"', async () => {
// @ts-expect-error
const request = new NextApiRequest()
// @ts-expect-error
const response = new NextApiResponse()
const drupal = new NextDrupalPages(BASE_URL)

// @ts-expect-error
await drupal.previewDisable(request, response)
expect(response.writeHead).toBeCalledWith(307, { Location: "/" })
expect(response.end).toBeCalled()
Expand Down

0 comments on commit 60257e5

Please sign in to comment.