Skip to content

Commit

Permalink
fix: add tests around MutationFunction.parent
Browse files Browse the repository at this point in the history
  • Loading branch information
BelfordZ committed Oct 5, 2022
1 parent 2e26427 commit 33a06a0
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ const isCycle = (s: JSONSchema, recursiveStack: JSONSchema[]): JSONSchema | fals
return false;
};

const last = (i: JSONSchema[]): JSONSchema | undefined => {
return i[i.length - 1];
const last = (i: JSONSchema[], skipTwo = false): JSONSchema | undefined => {
const skip = skipTwo ? -2 : -1;
return i[i.length - skip];
};

/**
Expand Down Expand Up @@ -132,7 +133,6 @@ export default function traverse(
}

recursiveStack.push(schema);

prePostMap.push([schema, mutableSchema]);

const rec = (s: JSONSchema, path: string[]): JSONSchema => {
Expand All @@ -147,7 +147,7 @@ export default function traverse(
s,
true,
jsonPathStringify(path),
last(recursiveStack) || schema
last(recursiveStack, true) || schema
);
}

Expand Down Expand Up @@ -205,7 +205,7 @@ export default function traverse(
schema.items,
true,
jsonPathStringify(pathStack),
last(recursiveStack) || schema
last(recursiveStack, true) || schema
);
} else {
const [, cycledMutableSchema] = prePostMap.find(
Expand Down Expand Up @@ -273,7 +273,7 @@ export default function traverse(
mutableSchema,
isCycle,
jsonPathStringify(pathStack),
last(recursiveStack) || schema
last(recursiveStack, true) || schema
);
}
}
148 changes: 148 additions & 0 deletions src/parent.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

import traverse from "./";
import { JSONSchema, JSONSchemaObject } from "@json-schema-tools/meta-schema";

describe("traverse parent", () => {
const test = (s: JSONSchema, parents: JSONSchema[]) => {
const mutator = jest.fn((s) => s);

traverse(s, mutator);

parents.forEach((parent) => {
expect(mutator).toHaveBeenCalledWith(
expect.anything(),
expect.any(Boolean),
expect.any(String),
parent,
);
});
};

describe("schema is a boolean", () => {
it("allows root schema as boolean", () => {
const testSchema: JSONSchema = true;
test(testSchema, [testSchema]);
});
});

describe("schema.properties", () => {
it("allows traversing property subschemas", () => {
const testSchema: JSONSchema = {
properties: {
a: {},
b: {},
},
};

test(testSchema, [testSchema]);
});

it("allows boolean subschema in properties", () => {
const testSchema: JSONSchema = {
type: "object",
properties: { a: true, b: false }
};

test(testSchema, [testSchema]);
});
});

describe("schema.additionalProperties", () => {
it("allows boolean", () => {
const testSchema: any = {
additionalProperties: true
};
test(testSchema, [testSchema]);
});

it("allows subschema", () => {
const testSchema: any = {
additionalProperties: {
properties: {
c: {},
d: {},
},
},
};

test(testSchema, [
testSchema,
testSchema.additionalProperties
]);
});
});

describe("schema.additionalItems", () => {
it("allows boolean", () => {
const testSchema: any = {
additionalItems: true
};
test(testSchema, [testSchema]);
});

it("allows subschema", () => {
const testSchema: any = {
additionalItems: {
properties: {
c: {},
d: {},
},
},
};

test(testSchema, [testSchema, testSchema.additionalItems]);
});
});

describe("schema.items", () => {
it("allows an array of schema", () => {
const testSchema = {
type: "array",
items: [
{ type: "string" },
{ type: "number" },
]
} as JSONSchema;

test(testSchema, [testSchema]);
});

it("allows a schema", () => {
const testSchema = {
type: "array",
items: { type: "number" },
} as JSONSchema;

test(testSchema, [testSchema]);
});
});

describe("schema.oneOf", () => {
it("works with deeply nested oneOfs", () => {
const testSchema: any = {
oneOf: [
{
oneOf: [{ type: "number" }, { type: "string" }]
},
{
type: "object",
properties: {
foo: {
oneOf: [
{ type: "array", items: true }, { type: "boolean" }
]
}
}
}
]
};

test(testSchema, [
testSchema,
testSchema.oneOf[0],
testSchema.oneOf[1],
testSchema.oneOf[1].properties.foo,
]);
});
});
});

0 comments on commit 33a06a0

Please sign in to comment.