import { describe, beforeEach, vi, it, expect, afterEach } from "vitest"; import handler from "./"; import { IEmailIntegrationConfig, invalidConfigs } from "./testHelpers"; describe("Preview handler", () => { const OLD_ENV = process.env; beforeEach(() => { vi.resetModules(); process.env = { ...OLD_ENV, SITE_ID: "testSiteId", SITE_NAME: "testSiteName", CONTEXT: "dev", URL: "http://localhost:8888", }; }); afterEach(() => { process.env = OLD_ENV; }); it.each(invalidConfigs)( "rejects requests with an invalid config with: %s", async (_, config, missingConfig) => { const invalidConfig = config as IEmailIntegrationConfig; process.env = { ...process.env, ...invalidConfig }; const response = await handler( new Request("http://localhost:8888/.netlify/functions/emails/confirm", { method: "GET", }) ); const body = await response.text(); // Get missingConfig object (missingConfig =) from html string // E.g. missingConfig = {"NETLIFY_EMAILS_PROVIDER":"mailgun","NETLIFY_EMAILS_API_KEY":"test" } const missingConfigObj = JSON.parse( body.match(/missingConfig = ({.*})/)?.[1] ?? "{}" ); // Get siteId variable from html string // E.g. siteId = "testSiteId" const siteId = body.match(/siteId = "(.*)"/)?.[1]; // Get templateName variable from html string // E.g. templateName = "booking-confirmed" const templateName = body.match(/templateName = "(.*)"/)?.[1]; expect(response.status).toBe(200); expect(response.headers.get("content-type")).toContain("text/html"); expect(body).toContain( "