Skip to content

Commit c7796c2

Browse files
authored
Merge pull request #817 from json-schema-tools/fix/increase-test-coverage-for-bfs-and-cycles
Add BFS cycle tests
2 parents 985a969 + 8369879 commit c7796c2

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/index.test.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,6 +1143,57 @@ describe("traverse", () => {
11431143
testCalls(mockMutation, testSchema.properties.foo.items[0], false, 3);
11441144
testCalls(mockMutation, testSchema.properties.foo.items[1], false, 4);
11451145
});
1146+
1147+
it("handles basic cycles when bfs is true", () => {
1148+
const schema = { type: "object", properties: { foo: {} } } as any;
1149+
schema.properties.foo = schema;
1150+
const mockMutation = jest.fn((s) => s);
1151+
1152+
traverse(schema as JSONSchema, mockMutation, { bfs: true });
1153+
1154+
expect(mockMutation).toHaveBeenCalledTimes(1);
1155+
});
1156+
1157+
it("handles chained cycles when bfs is true", () => {
1158+
const schema = {
1159+
title: "1",
1160+
type: "object",
1161+
properties: {
1162+
foo: {
1163+
title: "2",
1164+
items: [
1165+
{
1166+
title: "3",
1167+
type: "array",
1168+
items: { title: "4" },
1169+
},
1170+
],
1171+
},
1172+
},
1173+
} as any;
1174+
schema.properties.foo.items[0].items = schema;
1175+
const mockMutation = jest.fn((s) => s);
1176+
1177+
traverse(schema as JSONSchema, mockMutation, { bfs: true });
1178+
1179+
expect(mockMutation).toHaveBeenCalledTimes(3);
1180+
});
1181+
1182+
it("bfs still calls mutation for root cycles when skipFirstMutation is true", () => {
1183+
const schema: any = { title: "a", items: {} };
1184+
schema.items = schema;
1185+
const mockMutation = jest.fn((s) => s);
1186+
1187+
traverse(schema as JSONSchema, mockMutation, { bfs: true, skipFirstMutation: true, mutable: true });
1188+
1189+
expect(mockMutation).toHaveBeenCalledTimes(1);
1190+
expect(mockMutation).toHaveBeenCalledWith(
1191+
schema,
1192+
true,
1193+
expect.any(String),
1194+
schema
1195+
);
1196+
});
11461197
});
11471198
describe("Mutability settings", () => {
11481199
it("defaults to being immutable", () => {

0 commit comments

Comments
 (0)