-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetFormTemplate.test.ts
More file actions
99 lines (84 loc) · 2.79 KB
/
Copy pathgetFormTemplate.test.ts
File metadata and controls
99 lines (84 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import {
type PrismaClient,
type Template,
type TemplateVersion,
prisma,
} from "@gcforms/database";
import { getFormTemplate } from "@lib/formsClient/getFormTemplate.js";
import { logMessage } from "@lib/logging/logger.js";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { type DeepMockProxy, mockReset } from "vitest-mock-extended";
const prismaMock = prisma as unknown as DeepMockProxy<PrismaClient>;
describe("getFormTemplate should", () => {
beforeEach(() => {
mockReset(prismaMock);
vi.clearAllMocks();
});
it("return an undefined form template if database was not able to find it", async () => {
prismaMock.templateVersion.findUnique.mockResolvedValueOnce(null);
const formTemplate = await getFormTemplate("clzamy5qv0000115huc4bh90m", 1);
expect(formTemplate).toBeUndefined();
});
it("return a form template if database was able to find it", async () => {
prismaMock.templateVersion.findUnique.mockResolvedValue({
jsonConfig: {
elements: [
{
id: 1,
type: "textField",
},
],
},
} as unknown as TemplateVersion);
const formTemplate = await getFormTemplate("clzamy5qv0000115huc4bh90m", 1);
expect(formTemplate).toEqual({
jsonConfig: {
elements: [
{
id: 1,
type: "textField",
},
],
},
});
});
// This test can be deleted once form versioning is implemented in Production and migrated old form template database entries to the new versioned schema
it("return a form template even if form versioning is not fully deployed (testing fallback Prisma query)", async () => {
prismaMock.templateVersion.findUnique.mockResolvedValue(null);
prismaMock.template.findUnique.mockResolvedValue({
jsonConfig: {
elements: [
{
id: 1,
type: "textField",
},
],
},
} as unknown as Template);
const formTemplate = await getFormTemplate("clzamy5qv0000115huc4bh90m", 1);
expect(formTemplate).toEqual({
jsonConfig: {
elements: [
{
id: 1,
type: "textField",
},
],
},
});
});
it("throw an error if database has an internal failure", async () => {
const customError = new Error("custom error");
prismaMock.templateVersion.findUnique.mockRejectedValueOnce(customError);
const logMessageSpy = vi.spyOn(logMessage, "error");
await expect(() =>
getFormTemplate("clzamy5qv0000115huc4bh90m", 1),
).rejects.toThrow(customError);
expect(logMessageSpy).toHaveBeenCalledWith(
customError,
expect.stringContaining(
"[formsClient] Failed to retrieve form template. FormId: clzamy5qv0000115huc4bh90m",
),
);
});
});