Skip to content

Commit

Permalink
test: refactor and reorganize tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dextertanyj committed May 2, 2023
1 parent 70f6f96 commit 3d2322a
Show file tree
Hide file tree
Showing 8 changed files with 1,645 additions and 1,388 deletions.
954 changes: 954 additions & 0 deletions tests/arrrays.spec.ts

Large diffs are not rendered by default.

354 changes: 354 additions & 0 deletions tests/declared-arrays.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,354 @@
import { describe, expect, test } from "@jest/globals";

import { ExpectEqual } from "./utilities";

import { reshaperBuilder, Schema } from "@";

describe("declared arrays", () => {
describe("element array", () => {
test("should create array of values", () => {
type Data = {
array: { id: number }[];
};
const data: Data = {
array: [{ id: 1 }, { id: 2 }, { id: 3 }],
};
const schema = {
new: ["array[*]", "id"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[];
};
const expected: Expected = {
new: [1, 2, 3],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should flatten multidimensional primitive array", () => {
type Data = {
array: number[][][];
};
const data: Data = {
array: [[[1], [2]], [[3, 4]]],
};
const schema = {
new: ["array[*]", "[*][*]"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[][];
};
const expected: Expected = {
new: [
[1, 2],
[3, 4],
],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should access multidimensional object array", () => {
type Data = {
array: { id: number }[][];
};
const data: Data = {
array: [
[{ id: 1 }, { id: 2 }],
[{ id: 3 }, { id: 4 }],
],
};
const schema = {
new: ["array[*]", "[*].id"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[][];
};
const expected: Expected = {
new: [
[1, 2],
[3, 4],
],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should return undefined if outer array is undefined", () => {
type Data = {
array?: { id: number }[];
};
const data: Data = {
array: undefined,
};
const schema = {
new: ["array[*]", "id"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[] | undefined;
};
const expected: Expected = {
new: undefined,
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should return undefined if outer array is null", () => {
type Data = {
array: { id: number }[] | null;
};
const data: Data = {
array: null,
};
const schema = {
new: ["array[*]", "id"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[] | undefined;
};
const expected: Expected = {
new: undefined,
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should ignore undefined inner arrays", () => {
type Data = {
array: (number[] | undefined)[];
};
const data: Data = {
array: [[1], undefined, [2]],
};
const schema = {
new: ["array[*]", "[*]"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[][];
};
const expected: Expected = {
new: [[1], [2]],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should ignore null inner arrays", () => {
type Data = {
array: (number[] | null)[];
};
const data: Data = {
array: [[1], null, [2]],
};
const schema = {
new: ["array[*]", "[*]"],
} as const satisfies Schema<Data>;

type Expected = {
new: number[][];
};
const expected: Expected = {
new: [[1], [2]],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});
});

describe("object array", () => {
test("should return array of objects", () => {
type Data = {
array: { id: number }[];
};
const data: Data = {
array: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }],
};
const schema = {
new: [
"array[*]",
{
element: "id",
},
],
} as const satisfies Schema<Data>;

type Expected = {
new: { element: number }[];
};
const expected: Expected = {
new: [{ element: 1 }, { element: 2 }, { element: 3 }, { element: 4 }],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should return array of objects containing declared arrays", () => {
type Data = {
array: { nested: { id: number }[] }[];
};
const data: Data = {
array: [{ nested: [{ id: 1 }, { id: 2 }] }, { nested: [{ id: 3 }, { id: 4 }] }],
};
const schema = {
new: [
"array[*]",
{
nested: ["nested[*]", "id"],
},
],
} as const satisfies Schema<Data>;

type Expected = {
new: { nested: number[] }[];
};
const expected: Expected = {
new: [{ nested: [1, 2] }, { nested: [3, 4] }],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should return array of objects containing nested object", () => {
type Data = {
array: { id: number }[];
};
const data: Data = {
array: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }],
};
const schema = {
new: [
"array[*]",
{
nested: { element: "id" },
},
],
} as const satisfies Schema<Data>;

type Expected = {
new: { nested: { element: number } }[];
};
const expected: Expected = {
new: [
{ nested: { element: 1 } },
{ nested: { element: 2 } },
{ nested: { element: 3 } },
{ nested: { element: 4 } },
],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});
});

describe("multidimensional array", () => {
test("should return two dimensional array", () => {
type Data = {
array: { id: number }[][];
};
const data: Data = {
array: [[{ id: 1 }], [{ id: 2 }], [{ id: 3 }, { id: 4 }]],
};
const schema = {
new: ["array[*]", ["[*]", "id"]],
} as const satisfies Schema<Data>;

type Expected = {
new: number[][];
};
const expected: Expected = {
new: [[1], [2], [3, 4]],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});

test("should return two dimensional array of objects", () => {
type Data = {
array: { id: number }[][];
};
const data: Data = {
array: [[{ id: 1 }], [{ id: 2 }], [{ id: 3 }, { id: 4 }]],
};
const schema = {
new: ["array[*]", ["[*]", { element: "id" }]],
} as const satisfies Schema<Data>;

type Expected = {
new: { element: number }[][];
};
const expected: Expected = {
new: [[{ element: 1 }], [{ element: 2 }], [{ element: 3 }, { element: 4 }]],
};

const reshaper = reshaperBuilder<Data, typeof schema>(schema);
const result: Expected = reshaper(data);
expect(result).toEqual(expected);

const typecheck: ExpectEqual<ReturnType<typeof reshaper>, Expected> = true;
[typecheck];
});
});
});
28 changes: 27 additions & 1 deletion tests/invalid.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe("invalid inputs", () => {
expect(() => reshaper({ id: "string" })).not.toThrow();
});

test("should not fail for non-object elements in mapped array creation", () => {
test("should not fail for non-object elements in declared array creation", () => {
type Data = {
id: { name: string }[];
};
Expand All @@ -52,4 +52,30 @@ describe("invalid inputs", () => {
// @ts-expect-error id is not a field of input
expect(() => reshaper({ id: ["string"] })).not.toThrow();
});

test("should not fail for null schema in declared array creation", () => {
type Data = {
id: { name: string }[];
};
const schema = {
new: ["id[*]", null],
// @ts-expect-error id is not a field of input
} as const satisfies Schema<Data>;
// @ts-expect-error id is not a field of input
const reshaper = reshaperBuilder<Data, typeof schema>(schema);
expect(() => reshaper({ id: [{ name: "string" }] })).not.toThrow();
});

test("should not fail for non-array field in declared array creation", () => {
type Data = {
id: { name: string }[];
};
const schema = {
new: [0, { new: "name" }],
// @ts-expect-error id is not a field of input
} as const satisfies Schema<Data>;
// @ts-expect-error id is not a field of input
const reshaper = reshaperBuilder<Data, typeof schema>(schema);
expect(() => reshaper({ id: [{ name: "string" }] })).not.toThrow();
});
});
Loading

0 comments on commit 3d2322a

Please sign in to comment.