From 0c180b0fed4a7ca430f483a35ca0cb4dd28f1f93 Mon Sep 17 00:00:00 2001 From: Zachary Belford Date: Mon, 11 Mar 2024 18:43:08 -0700 Subject: [PATCH] fix: all tests passing --- src/index.test.ts | 347 ++++++++++++++++++++++++++++++++++++++------- src/index.ts | 2 +- src/parent.test.ts | 4 +- src/paths.test.ts | 163 ++++++++++++++++++--- 4 files changed, 438 insertions(+), 78 deletions(-) diff --git a/src/index.test.ts b/src/index.test.ts index 4429c7a..c0645a7 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -156,7 +156,7 @@ describe("traverse", () => { schema, false, expect.any(String), - expect.anything(), + undefined ); }); @@ -166,26 +166,59 @@ describe("traverse", () => { const schema = { type: "object", patternProperties: { "*.": a, "x-^": b } - } as JSONSchema; + }; const mockMutation = jest.fn((s) => s); - traverse(schema, mockMutation); + traverse(schema as JSONSchema, mockMutation); expect(mockMutation).toHaveBeenCalledTimes(3); - testCalls(mockMutation, a, false, 1); - testCalls(mockMutation, b, false, 2); - testCalls(mockMutation, schema, false, 3); + + expect(mockMutation).nthCalledWith( + 1, + schema.patternProperties['*.'], + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + schema.patternProperties['x-^'], + expect.anything(), + expect.anything(), + expect.anything(), + ); }); it("allows booleans that are created via boolean class and new", () => { const a = new Boolean(true); const b = new Boolean(false); - const schema = { type: "object", properties: { a, b } } as JSONSchema; + const schema = { type: "object", properties: { a, b } }; const mockMutation = jest.fn((s) => s); - traverse(schema, mockMutation); + traverse(schema as JSONSchema, mockMutation); expect(mockMutation).toHaveBeenCalledTimes(3); - testCalls(mockMutation, a); - testCalls(mockMutation, b); - testCalls(mockMutation, schema); + expect(mockMutation).nthCalledWith( + 1, + schema.properties.a, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + schema.properties.b, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + schema, + expect.anything(), + expect.anything(), + undefined + ); expect(mockMutation).not.toHaveBeenNthCalledWith( 1, true, @@ -201,12 +234,25 @@ describe("traverse", () => { }); it("items is a boolean", () => { - const schema = { type: "array", items: true } as JSONSchema; + const schema = { type: "array", items: true }; const mockMutation = jest.fn((s) => s); - traverse(schema, mockMutation); + traverse(schema as JSONSchema, mockMutation); expect(mockMutation).toHaveBeenCalledTimes(2); - testCalls(mockMutation, true); - testCalls(mockMutation, schema); + expect(mockMutation).nthCalledWith( + 1, + schema.items, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + schema, + expect.anything(), + expect.anything(), + undefined, + ); }); it("doesnt skip boolean schemas that it has not seen", () => { @@ -238,9 +284,29 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.properties.a); - testCalls(mockMutation, testSchema.properties.b); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.properties.a, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + testSchema.properties.b, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema, + expect.anything(), + expect.anything(), + undefined, + ); expect(mockMutation).toHaveBeenCalledTimes(3); }); @@ -253,9 +319,21 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalProperties); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.additionalProperties, + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema, + expect.anything(), + expect.anything(), + undefined, + ); expect(mockMutation).toHaveBeenCalledTimes(2); }); @@ -272,10 +350,36 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalProperties); - testCalls(mockMutation, testSchema.additionalProperties.properties.c); - testCalls(mockMutation, testSchema.additionalProperties.properties.d); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.additionalProperties.properties.c, + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema.additionalProperties.properties.d, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema.additionalProperties, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 4, + testSchema, + expect.anything(), + expect.anything(), + undefined + ); expect(mockMutation).toHaveBeenCalledTimes(4); }); @@ -289,8 +393,20 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.additionalItems, + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema, + expect.anything(), + expect.anything(), + undefined, + ); expect(mockMutation).toHaveBeenCalledTimes(2); }); @@ -303,8 +419,21 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema); + + expect(mockMutation).nthCalledWith( + 1, + testSchema.additionalItems, + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema, + expect.anything(), + expect.anything(), + undefined, + ); expect(mockMutation).toHaveBeenCalledTimes(2); }); @@ -318,9 +447,29 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema.items[0]); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.items[0], + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + testSchema.additionalItems, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema, + expect.anything(), + expect.anything(), + undefined + ); expect(mockMutation).toHaveBeenCalledTimes(3); }); @@ -334,9 +483,29 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema.items); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.items, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 2, + testSchema.additionalItems, + expect.anything(), + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema, + expect.anything(), + expect.anything(), + undefined + ); expect(mockMutation).toHaveBeenCalledTimes(3); }); @@ -355,13 +524,45 @@ describe("traverse", () => { traverse(testSchema, mockMutation); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema.items[0]); - testCalls(mockMutation, testSchema.additionalItems.properties.c); - testCalls(mockMutation, testSchema.additionalItems.properties.d); - testCalls(mockMutation, testSchema); - expect(mockMutation).toHaveBeenCalledTimes(5); + expect(mockMutation).nthCalledWith( + 1, + testSchema.items[0], + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema.additionalItems.properties.c, + false, + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema.additionalItems.properties.d, + false, + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 4, + testSchema.additionalItems, + false, + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 5, + testSchema, + false, + expect.anything(), + undefined + ); + }); it("schema with nested subschemas: items is single schema", () => { @@ -377,14 +578,46 @@ describe("traverse", () => { const mockMutation = jest.fn((mockS) => mockS); traverse(testSchema, mockMutation); + expect(mockMutation).toHaveBeenCalledTimes(5); - testCalls(mockMutation, testSchema.additionalItems); - testCalls(mockMutation, testSchema.items); - testCalls(mockMutation, testSchema.additionalItems.properties.c); - testCalls(mockMutation, testSchema.additionalItems.properties.d); - testCalls(mockMutation, testSchema); + expect(mockMutation).nthCalledWith( + 1, + testSchema.items, + expect.anything(), + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema.additionalItems.properties.c, + false, + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 3, + testSchema.additionalItems.properties.d, + false, + expect.anything(), + expect.anything(), + ); + expect(mockMutation).nthCalledWith( + 4, + testSchema.additionalItems, + false, + expect.anything(), + expect.anything(), + ); + + expect(mockMutation).nthCalledWith( + 5, + testSchema, + false, + expect.anything(), + undefined + ); - expect(mockMutation).toHaveBeenCalledTimes(5); }); }); }); @@ -719,12 +952,11 @@ describe("traverse", () => { traverse(testSchema as JSONSchema, mockMutation, { mutable: true }); - testCalls(mockMutation, testSchema.properties.foo, true); - expect(mockMutation).not.toHaveBeenCalledWith( + expect(mockMutation).toHaveBeenCalledWith( testSchema, - false, + true, "$", - expect.anything() + undefined ); }); @@ -745,12 +977,19 @@ describe("traverse", () => { traverse(testSchema as JSONSchema, mockMutation, { mutable: false }); - testCalls(mockMutation, testSchema, true); - expect(mockMutation).not.toHaveBeenCalledWith( - testSchema, + expect(mockMutation).nthCalledWith( + 1, + testSchema.oneOf[0], false, + expect.any(String), + testSchema + ); + expect(mockMutation).nthCalledWith( + 2, + testSchema, + true, "$", - expect.anything(), + undefined ); }); }); diff --git a/src/index.ts b/src/index.ts index f49d3db..8b1d1db 100644 --- a/src/index.ts +++ b/src/index.ts @@ -278,7 +278,7 @@ export default function traverse( mutableSchema, isCycle, jsonPathStringify(pathStack), - last(mutableStack, 2) || schema + last(mutableStack, 2) ); mutableStack.pop(); return mutated; diff --git a/src/parent.test.ts b/src/parent.test.ts index 64e0390..eaedeac 100644 --- a/src/parent.test.ts +++ b/src/parent.test.ts @@ -331,7 +331,7 @@ describe("traverse parent", () => { testSchema, expect.any(Boolean), expect.any(String), - testSchema + undefined ); }); @@ -416,7 +416,7 @@ describe("traverse parent", () => { testSchema, false, expect.any(String), - testSchema, + undefined, ); // additionalItems is not the root should not be the its own parent diff --git a/src/paths.test.ts b/src/paths.test.ts index a730ef9..d510ee6 100644 --- a/src/paths.test.ts +++ b/src/paths.test.ts @@ -7,12 +7,13 @@ describe("traverse paths", () => { traverse(s, mutator); - paths.forEach((path) => { - expect(mutator).toHaveBeenCalledWith( + paths.forEach((path, i) => { + expect(mutator).nthCalledWith( + i + 1, expect.anything(), expect.any(Boolean), path, - isRoot ? undefined : expect.anything() + i === paths.length - 1 ? undefined : expect.anything() ); }); }; @@ -32,19 +33,45 @@ describe("traverse paths", () => { b: {}, }, }; - test(testSchema, [ - "$", - "$.properties.a", - "$.properties.b", - ]); + + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.properties.a', + expect.anything(), + ); + expect(mutator).nthCalledWith( + 2, + expect.anything(), + expect.any(Boolean), + '$.properties.b', + expect.anything() + ); }); it("allows boolean subschema in properties", () => { const testSchema = { type: "object", properties: { a: true, b: false } } as JSONSchema; - test(testSchema, [ - "$", - "$.properties.a", - "$.properties.b", - ]); + + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.properties.a', + expect.anything(), + ); + expect(mutator).nthCalledWith( + 2, + expect.anything(), + expect.any(Boolean), + '$.properties.b', + expect.anything() + ); }); }); @@ -53,7 +80,17 @@ describe("traverse paths", () => { const testSchema: any = { additionalProperties: true }; - test(testSchema, ["$", "$.additionalProperties"]); + + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.additionalProperties', + expect.anything(), + ); }); it("allows subschema", () => { @@ -66,11 +103,45 @@ describe("traverse paths", () => { }, }; + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.additionalProperties.properties.c', + expect.anything(), + ); + expect(mutator).nthCalledWith( + 2, + expect.anything(), + expect.any(Boolean), + '$.additionalProperties.properties.d', + expect.anything() + ); + + expect(mutator).nthCalledWith( + 3, + expect.anything(), + expect.any(Boolean), + '$.additionalProperties', + expect.anything() + ); + + expect(mutator).nthCalledWith( + 4, + expect.anything(), + expect.any(Boolean), + '$', + undefined + ); test(testSchema, [ - "$", - "$.additionalProperties", "$.additionalProperties.properties.c", "$.additionalProperties.properties.d", + "$.additionalProperties", + "$", ]); }); }); @@ -80,7 +151,18 @@ describe("traverse paths", () => { const testSchema: any = { additionalItems: true }; - test(testSchema, ["$", "$.additionalItems"]); + + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.additionalItems', + expect.anything(), + ); }); it("allows subschema", () => { @@ -93,11 +175,15 @@ describe("traverse paths", () => { }, }; + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + test(testSchema, [ - "$", - "$.additionalItems", "$.additionalItems.properties.c", "$.additionalItems.properties.d", + "$.additionalItems", + "$", ]); }); }); @@ -111,7 +197,25 @@ describe("traverse paths", () => { { type: "number" }, ] } as JSONSchema; - test(testSchema, ["$.items[0]"]); + + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.items[0]', + expect.anything(), + ); + expect(mutator).nthCalledWith( + 2, + expect.anything(), + expect.any(Boolean), + '$.items[1]', + expect.anything() + ); }); it("allows a schema", () => { @@ -120,7 +224,24 @@ describe("traverse paths", () => { items: { type: "number" }, } as JSONSchema; - test(testSchema, ["$.items"]); + const mutator = jest.fn((s) => s); + + traverse(testSchema, mutator); + + expect(mutator).nthCalledWith( + 1, + expect.anything(), + expect.any(Boolean), + '$.items', + expect.anything(), + ); + expect(mutator).nthCalledWith( + 2, + expect.anything(), + expect.any(Boolean), + '$', + undefined + ); }); }); });