-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathruleMapping.test.ts
More file actions
167 lines (144 loc) · 6.01 KB
/
ruleMapping.test.ts
File metadata and controls
167 lines (144 loc) · 6.01 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { getRuleMapping, ruleIdMappings } from "@/app/lib/ruleMapping";
describe("ruleMapping", () => {
describe("ruleIdMappings", () => {
it("should contain all expected rule IDs", () => {
const expectedRuleIds = [
3, 8, 10, 17, 18, 21, 22, 30, 35, 39, 40, 54, 66, 69, 71,
];
const actualRuleIds = Object.keys(ruleIdMappings).map(Number);
expectedRuleIds.forEach((ruleId) => {
expect(actualRuleIds).toContain(ruleId);
});
});
it("should have valid structure for each rule mapping", () => {
Object.entries(ruleIdMappings).forEach(([, mapping]) => {
expect(mapping).toHaveProperty("ruleDescription");
expect(mapping).toHaveProperty("moreDetails");
expect(mapping).toHaveProperty("reportingId");
expect(typeof mapping.ruleDescription).toBe("string");
expect(mapping.ruleDescription.length).toBeGreaterThan(0);
expect(typeof mapping.reportingId).toBe("string");
if (mapping.reportingId) {
expect(mapping.reportingId.length).toBeGreaterThan(0);
}
});
});
it("should have unique reporting IDs", () => {
const reportingIds = Object.values(ruleIdMappings)
.map((mapping) => mapping.reportingId)
.filter((id) => id !== undefined);
const uniqueReportingIds = new Set(reportingIds);
expect(reportingIds.length).toBe(uniqueReportingIds.size);
});
});
describe("getRuleMapping", () => {
describe("when rule ID exists in mappings", () => {
it("should return the correct mapping for rule ID 3", () => {
const result = getRuleMapping(3);
expect(result).toEqual({
ruleDescription:
"It's not possible to have both a current GP practice code and a current reason for removal (RfR), or neither.",
moreDetails:
"Either: enter a GP practice code, or enter a reason for removal. Information must only be entered for one of these fields. Both fields cannot be empty.",
reportingId: "CMR13",
});
});
it("should return the correct mapping for rule ID 17", () => {
const result = getRuleMapping(17);
expect(result).toEqual({
ruleDescription:
"Date of birth is either missing, in the wrong format, or is in the future.",
moreDetails:
"Enter the date of birth in the correct format. The date must not be in the future. ",
reportingId: "CMR14",
});
});
it("should return the correct mapping for rule ID 71", () => {
const result = getRuleMapping(71);
expect(result).toEqual({
ruleDescription: "Address is blank (postcode may be blank too).",
moreDetails: "Enter the patient's full address and postcode.",
reportingId: "CMR17",
});
});
it("should not use fallback description when rule exists", () => {
const fallbackDescription = "This should not be used";
const result = getRuleMapping(3, fallbackDescription);
expect(result.ruleDescription).not.toBe(fallbackDescription);
expect(result.ruleDescription).toBe(
"It's not possible to have both a current GP practice code and a current reason for removal (RfR), or neither."
);
});
});
describe("when rule ID does not exist in mappings", () => {
it("should return fallback description when provided", () => {
const fallbackDescription = "Custom API description";
const result = getRuleMapping(999, fallbackDescription);
expect(result).toEqual({
ruleDescription: fallbackDescription,
moreDetails: fallbackDescription,
reportingId: undefined,
});
});
it("should return undefined for all fields when no fallback provided", () => {
const result = getRuleMapping(999);
expect(result).toEqual({
ruleDescription: undefined,
moreDetails: undefined,
reportingId: undefined,
});
});
it("should handle empty string as fallback description", () => {
const result = getRuleMapping(888, "");
expect(result).toEqual({
ruleDescription: "",
moreDetails: "",
reportingId: undefined,
});
});
it("should handle undefined as fallback description", () => {
const result = getRuleMapping(777, undefined);
expect(result).toEqual({
ruleDescription: undefined,
moreDetails: undefined,
reportingId: undefined,
});
});
});
describe("return type validation", () => {
it("should return RuleMapping interface structure", () => {
const result = getRuleMapping(3);
expect(result).toHaveProperty("ruleDescription");
expect(result).toHaveProperty("moreDetails");
expect(result).toHaveProperty("reportingId");
});
it("should return RuleMapping interface structure for unmapped rules", () => {
const result = getRuleMapping(999, "Test description");
expect(result).toHaveProperty("ruleDescription");
expect(result).toHaveProperty("moreDetails");
expect(result).toHaveProperty("reportingId");
});
});
});
describe("specific rule validations", () => {
it("should have correct CMR reporting IDs format", () => {
Object.values(ruleIdMappings).forEach((mapping) => {
expect(mapping.reportingId).toMatch(/^CMR\d+$/);
});
});
it("should have non-empty rule descriptions", () => {
Object.values(ruleIdMappings).forEach((mapping) => {
expect(mapping.ruleDescription.trim()).not.toBe("");
});
});
it("should handle rules with empty moreDetails", () => {
// Rule 21 has empty moreDetails in the mapping
const result = getRuleMapping(21);
expect(result.moreDetails).toBe("");
expect(result.ruleDescription).toBe(
"The 'Superseded by NHS number' field has been populated with an NHS number by NBO."
);
expect(result.reportingId).toBe("CMR33");
});
});
});