forked from rjsf-team/react-jsonschema-form
/
createAjvInstance.test.ts
209 lines (205 loc) · 6.22 KB
/
createAjvInstance.test.ts
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import Ajv from "ajv";
import Ajv2019 from "ajv/dist/2019";
import addFormats from "ajv-formats";
import createAjvInstance, {
AJV_CONFIG,
COLOR_FORMAT_REGEX,
DATA_URL_FORMAT_REGEX,
} from "../src/createAjvInstance";
import { CustomValidatorOptionsType } from "../src";
jest.mock("ajv");
jest.mock("ajv/dist/2019");
jest.mock("ajv-formats");
export const CUSTOM_OPTIONS: CustomValidatorOptionsType = {
additionalMetaSchemas: [require("ajv/lib/refs/json-schema-draft-06.json")],
customFormats: {
"phone-us": /\(?\d{3}\)?[\s-]?\d{3}[\s-]?\d{4}$/,
"area-code": /\d{3}/,
},
ajvOptionsOverrides: {
$data: true,
verbose: true,
},
ajvFormatOptions: {
mode: "fast",
},
};
describe("createAjvInstance()", () => {
describe("no additional meta schemas, custom formats, ajv options overrides or ajv format options", () => {
let ajv: Ajv;
beforeAll(() => {
ajv = createAjvInstance();
});
afterAll(() => {
(Ajv as unknown as jest.Mock).mockClear();
(addFormats as unknown as jest.Mock).mockClear();
});
it("expect a new Ajv to be constructed with the AJV_CONFIG", () => {
expect(Ajv).toHaveBeenCalledWith(AJV_CONFIG);
});
it("expect addFormats to be called with the new ajv instance and undefined", () => {
expect(addFormats).toHaveBeenCalledWith(ajv);
});
it("addFormat() was called twice", () => {
expect(ajv.addFormat).toHaveBeenCalledTimes(2);
});
it("the first addFormat() was for data-url", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
1,
"data-url",
DATA_URL_FORMAT_REGEX
);
});
it("the second addFormat() was for color", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
2,
"color",
COLOR_FORMAT_REGEX
);
});
it("addMetaSchema was not called", () => {
expect(ajv.addMetaSchema).not.toHaveBeenCalled();
});
});
describe("all defaults except uses the Ajv2019 class", () => {
let ajv: Ajv;
beforeAll(() => {
ajv = createAjvInstance(
undefined,
undefined,
undefined,
undefined,
Ajv2019
);
});
afterAll(() => {
(Ajv as unknown as jest.Mock).mockClear();
(addFormats as unknown as jest.Mock).mockClear();
});
it("expect a new Ajv2019 to be constructed with the AJV_CONFIG", () => {
expect(Ajv2019).toHaveBeenCalledWith(AJV_CONFIG);
});
it("expect the default Ajv constructor was not called", () => {
expect(Ajv).not.toHaveBeenCalled();
});
it("expect addFormats to be called with the new ajv instance and undefined", () => {
expect(addFormats).toHaveBeenCalledWith(ajv);
});
it("addFormat() was called twice", () => {
expect(ajv.addFormat).toHaveBeenCalledTimes(2);
});
it("the first addFormat() was for data-url", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
1,
"data-url",
DATA_URL_FORMAT_REGEX
);
});
it("the second addFormat() was for color", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
2,
"color",
COLOR_FORMAT_REGEX
);
});
it("addMetaSchema was not called", () => {
expect(ajv.addMetaSchema).not.toHaveBeenCalled();
});
});
describe("has additional meta schemas, custom formats, ajv options override and ajv format options", () => {
let ajv: Ajv;
beforeAll(() => {
ajv = createAjvInstance(
CUSTOM_OPTIONS.additionalMetaSchemas,
CUSTOM_OPTIONS.customFormats,
CUSTOM_OPTIONS.ajvOptionsOverrides,
CUSTOM_OPTIONS.ajvFormatOptions
);
});
afterAll(() => {
(Ajv as unknown as jest.Mock).mockClear();
(addFormats as unknown as jest.Mock).mockClear();
});
it("expect a new Ajv to be constructed with the AJV_CONFIG", () => {
expect(Ajv).toHaveBeenCalledWith({
...AJV_CONFIG,
...CUSTOM_OPTIONS.ajvOptionsOverrides,
});
});
it("expect addFormats to be called with the new ajv instance and options", () => {
expect(addFormats).toHaveBeenCalledWith(
ajv,
CUSTOM_OPTIONS.ajvFormatOptions
);
});
it("addFormat() was called twice", () => {
expect(ajv.addFormat).toHaveBeenCalledTimes(4);
});
it("the first addFormat() was for data-url", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
1,
"data-url",
DATA_URL_FORMAT_REGEX
);
});
it("the second addFormat() was for color", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
2,
"color",
COLOR_FORMAT_REGEX
);
});
it("the remaining addFormat() calls were for custom formats", () => {
Object.keys(CUSTOM_OPTIONS.customFormats!).forEach(
(key: string, i: number) => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
3 + i,
key,
CUSTOM_OPTIONS.customFormats![key]
);
}
);
});
it("addMetaSchema was not called", () => {
expect(ajv.addMetaSchema).toHaveBeenCalledWith(
CUSTOM_OPTIONS.additionalMetaSchemas
);
});
});
describe("disables ajv format", () => {
let ajv: Ajv;
beforeAll(() => {
ajv = createAjvInstance(undefined, undefined, undefined, false);
});
afterAll(() => {
(Ajv as unknown as jest.Mock).mockClear();
(addFormats as unknown as jest.Mock).mockClear();
});
it("expect a new Ajv to be constructed with the AJV_CONFIG", () => {
expect(Ajv).toHaveBeenCalledWith(AJV_CONFIG);
});
it("expect addFormats NOT to be called", () => {
expect(addFormats).not.toHaveBeenCalled();
});
it("addFormat() was called twice", () => {
expect(ajv.addFormat).toHaveBeenCalledTimes(2);
});
it("the first addFormat() was for data-url", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
1,
"data-url",
DATA_URL_FORMAT_REGEX
);
});
it("the second addFormat() was for color", () => {
expect(ajv.addFormat).toHaveBeenNthCalledWith(
2,
"color",
COLOR_FORMAT_REGEX
);
});
it("addMetaSchema was not called", () => {
expect(ajv.addMetaSchema).not.toHaveBeenCalled();
});
});
});