Skip to content

Commit

Permalink
Improve unit tests of map, record and tupleWithRest
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed Oct 30, 2024
1 parent e2eab9b commit 4baa0f6
Show file tree
Hide file tree
Showing 6 changed files with 437 additions and 110 deletions.
124 changes: 100 additions & 24 deletions library/src/schemas/map/map.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,14 @@ describe('map', () => {
abortPipeEarly: undefined,
};

const input = new Map<unknown, unknown>([
[0, 'foo'],
[1, 123], // Invalid value
[2, 'baz'],
[null, 'bar'], // Invalid key
[4, null], // Invalid value
]);

const stringIssue: StringIssue = {
...baseInfo,
kind: 'schema',
Expand All @@ -173,25 +181,14 @@ describe('map', () => {
{
type: 'map',
origin: 'value',
input: new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]),
input,
key: 1,
value: 123,
},
],
};

test('for wrong values', () => {
const input = new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]);
test('for invalid values', () => {
expect(schema['~validate']({ value: input }, {})).toStrictEqual({
typed: false,
value: input,
Expand All @@ -214,31 +211,73 @@ describe('map', () => {
},
],
},
{
...baseInfo,
kind: 'schema',
type: 'string',
input: null,
expected: 'string',
received: 'null',
path: [
{
type: 'map',
origin: 'value',
input,
key: 4,
value: null,
},
],
},
],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('with abort early', () => {
test('with abort early for invalid key', () => {
const input = new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'], // Invalid key
[2, 'baz'],
[3, 123], // Invalid value
]);
expect(
schema['~validate'](
schema['~validate']({ value: input }, { abortEarly: true })
).toStrictEqual({
typed: false,
value: new Map([[0, 'foo']]),
issues: [
{
value: new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]),
...baseInfo,
kind: 'schema',
type: 'number',
input: '1',
expected: 'number',
received: '"1"',
path: [
{
type: 'map',
origin: 'key',
input,
key: '1',
value: 'bar',
},
],
abortEarly: true,
},
{ abortEarly: true }
)
],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('with abort early for invalid value', () => {
expect(
schema['~validate']({ value: input }, { abortEarly: true })
).toStrictEqual({
typed: false,
value: new Map([[0, 'foo']]),
issues: [{ ...stringIssue, abortEarly: true }],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('for wrong nested values', () => {
test('for invalid nested values', () => {
const nestedSchema = map(schema, schema);
const input = new Map<unknown, unknown>([
[
Expand All @@ -249,6 +288,13 @@ describe('map', () => {
new Map(),
],
[new Map(), 'bar'],
[
new Map(),
new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
],
]);
expect(nestedSchema['~validate']({ value: input }, {})).toStrictEqual({
typed: false,
Expand Down Expand Up @@ -301,6 +347,36 @@ describe('map', () => {
},
],
},
{
...baseInfo,
kind: 'schema',
type: 'number',
input: '1',
expected: 'number',
received: '"1"',
path: [
{
type: 'map',
origin: 'value',
input,
key: new Map(),
value: new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
},
{
type: 'map',
origin: 'key',
input: new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
key: '1',
value: 'bar',
},
],
},
],
} satisfies FailureDataset<InferIssue<typeof nestedSchema>>);
});
Expand Down
124 changes: 100 additions & 24 deletions library/src/schemas/map/mapAsync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ describe('mapAsync', () => {
abortPipeEarly: undefined,
};

const input = new Map<unknown, unknown>([
[0, 'foo'],
[1, 123], // Invalid value
[2, 'baz'],
[null, 'bar'], // Invalid key
[4, null], // Invalid value
]);

const stringIssue: StringIssue = {
...baseInfo,
kind: 'schema',
Expand All @@ -182,25 +190,14 @@ describe('mapAsync', () => {
{
type: 'map',
origin: 'value',
input: new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]),
input,
key: 1,
value: 123,
},
],
};

test('for wrong values', async () => {
const input = new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]);
test('for invalid values', async () => {
expect(await schema['~validate']({ value: input }, {})).toStrictEqual({
typed: false,
value: input,
Expand All @@ -223,31 +220,73 @@ describe('mapAsync', () => {
},
],
},
{
...baseInfo,
kind: 'schema',
type: 'string',
input: null,
expected: 'string',
received: 'null',
path: [
{
type: 'map',
origin: 'value',
input,
key: 4,
value: null,
},
],
},
],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('with abort early', async () => {
test('with abort early for invalid key', async () => {
const input = new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'], // Invalid key
[2, 'baz'],
[3, 123], // Invalid value
]);
expect(
await schema['~validate'](
await schema['~validate']({ value: input }, { abortEarly: true })
).toStrictEqual({
typed: false,
value: new Map([[0, 'foo']]),
issues: [
{
value: new Map<unknown, unknown>([
[0, 'foo'],
[1, 123],
[2, 'baz'],
[null, 'bar'],
]),
...baseInfo,
kind: 'schema',
type: 'number',
input: '1',
expected: 'number',
received: '"1"',
path: [
{
type: 'map',
origin: 'key',
input,
key: '1',
value: 'bar',
},
],
abortEarly: true,
},
{ abortEarly: true }
)
],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('with abort early for invalid value', async () => {
expect(
await schema['~validate']({ value: input }, { abortEarly: true })
).toStrictEqual({
typed: false,
value: new Map([[0, 'foo']]),
issues: [{ ...stringIssue, abortEarly: true }],
} satisfies FailureDataset<InferIssue<typeof schema>>);
});

test('for wrong nested values', async () => {
test('for invalid nested values', async () => {
const nestedSchema = mapAsync(schema, schema);
const input = new Map<unknown, unknown>([
[
Expand All @@ -258,6 +297,13 @@ describe('mapAsync', () => {
new Map(),
],
[new Map(), 'bar'],
[
new Map(),
new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
],
]);
expect(
await nestedSchema['~validate']({ value: input }, {})
Expand Down Expand Up @@ -312,6 +358,36 @@ describe('mapAsync', () => {
},
],
},
{
...baseInfo,
kind: 'schema',
type: 'number',
input: '1',
expected: 'number',
received: '"1"',
path: [
{
type: 'map',
origin: 'value',
input,
key: new Map(),
value: new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
},
{
type: 'map',
origin: 'key',
input: new Map<unknown, unknown>([
[0, 'foo'],
['1', 'bar'],
]),
key: '1',
value: 'bar',
},
],
},
],
} satisfies FailureDataset<InferIssue<typeof nestedSchema>>);
});
Expand Down
Loading

0 comments on commit 4baa0f6

Please sign in to comment.