Skip to content

Commit

Permalink
Add unit and type tests for lazyAsync schema
Browse files Browse the repository at this point in the history
  • Loading branch information
fabian-hiller committed May 20, 2024
1 parent 6241e0b commit 3573e82
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 27 deletions.
32 changes: 32 additions & 0 deletions library/src/schemas/lazy/lazyAsync.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { describe, expectTypeOf, test } from 'vitest';
import type { InferInput, InferIssue, InferOutput } from '../../types/index.ts';
import {
string,
type StringIssue,
type StringSchema,
} from '../string/index.ts';
import { lazyAsync, type LazySchemaAsync } from './lazyAsync.ts';

describe('lazyAsync', () => {
test('should return schema object', () => {
expectTypeOf(lazyAsync(async () => string())).toEqualTypeOf<
LazySchemaAsync<StringSchema<undefined>>
>();
});

describe('should infer correct types', () => {
type Schema = LazySchemaAsync<StringSchema<undefined>>;

test('of input', () => {
expectTypeOf<InferInput<Schema>>().toEqualTypeOf<string>();
});

test('of output', () => {
expectTypeOf<InferOutput<Schema>>().toEqualTypeOf<string>();
});

test('of issue', () => {
expectTypeOf<InferIssue<Schema>>().toEqualTypeOf<StringIssue>();
});
});
});
97 changes: 97 additions & 0 deletions library/src/schemas/lazy/lazyAsync.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { describe, expect, test, vi } from 'vitest';
import {
expectNoSchemaIssueAsync,
expectSchemaIssueAsync,
} from '../../vitest/index.ts';
import {
string,
type StringIssue,
type StringSchema,
} from '../string/index.ts';
import { lazyAsync, type LazySchemaAsync } from './lazyAsync.ts';

describe('lazyAsync', () => {
test('should return schema object', () => {
const getter = async () => string();
expect(lazyAsync(getter)).toStrictEqual({
kind: 'schema',
type: 'lazy',
reference: lazyAsync,
expects: 'unknown',
getter,
async: true,
_run: expect.any(Function),
} satisfies LazySchemaAsync<StringSchema<undefined>>);
});

describe('should return dataset without issues', () => {
const schema = lazyAsync(() => string());

test('for strings', async () => {
await expectNoSchemaIssueAsync(schema, ['', 'foo', '123']);
});
});

describe('should return dataset with issues', () => {
const schema = lazyAsync(() => string('message'));
const baseIssue: Omit<StringIssue, 'input' | 'received'> = {
kind: 'schema',
type: 'string',
expected: 'string',
message: 'message',
};

// Primitive types

test('for bigints', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [-1n, 0n, 123n]);
});

test('for booleans', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [true, false]);
});

test('for null', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [null]);
});

test('for numbers', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [-1, 0, 123, 45.67]);
});

test('for undefined', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [undefined]);
});

test('for symbols', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [
Symbol(),
Symbol('foo'),
]);
});

// Complex types

test('for arrays', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [[], ['value']]);
});

test('for functions', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [
() => {},
function () {},
]);
});

test('for objects', async () => {
await expectSchemaIssueAsync(schema, baseIssue, [{}, { key: 'value' }]);
});
});

test('should call getter with input', () => {
const getter = vi.fn(() => string());
const dataset = { typed: false, value: 'foo' };
lazyAsync(getter)._run(dataset, {});
expect(getter).toHaveBeenCalledWith(dataset.value);
});
});
8 changes: 5 additions & 3 deletions library/src/schemas/lazy/lazyAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,19 @@ export interface LazySchemaAsync<
}

/**
* Creates an async lazy schema.
* Creates a lazy schema.
*
* @param getter The schema getter.
*
* @returns An async lazy schema.
* @returns A lazy schema.
*/
export function lazyAsync<
const TWrapped extends
| BaseSchema<unknown, unknown, BaseIssue<unknown>>
| BaseSchemaAsync<unknown, unknown, BaseIssue<unknown>>,
>(getter: (input: unknown) => TWrapped): LazySchemaAsync<TWrapped> {
>(
getter: (input: unknown) => MaybePromise<TWrapped>
): LazySchemaAsync<TWrapped> {
return {
kind: 'schema',
type: 'lazy',
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/nonNullable/nonNullableAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export interface NonNullableSchemaAsync<
}

/**
* Creates an async non nullable schema.
* Creates a non nullable schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async non nullable schema.
* @returns A non nullable schema.
*/
export function nonNullableAsync<
const TWrapped extends
Expand All @@ -61,12 +61,12 @@ export function nonNullableAsync<
>(wrapped: TWrapped): NonNullableSchemaAsync<TWrapped, undefined>;

/**
* Creates an async non nullable schema.
* Creates a non nullable schema.
*
* @param wrapped The wrapped schema.
* @param message The error message.
*
* @returns An async non nullable schema.
* @returns A non nullable schema.
*/
export function nonNullableAsync<
const TWrapped extends
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/nonNullish/nonNullishAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export interface NonNullishSchemaAsync<
}

/**
* Creates an async non nullish schema.
* Creates a non nullish schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async non nullish schema.
* @returns A non nullish schema.
*/
export function nonNullishAsync<
const TWrapped extends
Expand All @@ -61,12 +61,12 @@ export function nonNullishAsync<
>(wrapped: TWrapped): NonNullishSchemaAsync<TWrapped, undefined>;

/**
* Creates an async non nullish schema.
* Creates a non nullish schema.
*
* @param wrapped The wrapped schema.
* @param message The error message.
*
* @returns An async non nullish schema.
* @returns A non nullish schema.
*/
export function nonNullishAsync<
const TWrapped extends
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/nonOptional/nonOptionalAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ export interface NonOptionalSchemaAsync<
}

/**
* Creates an async non optional schema.
* Creates a non optional schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async non optional schema.
* @returns A non optional schema.
*/
export function nonOptionalAsync<
const TWrapped extends
Expand All @@ -61,12 +61,12 @@ export function nonOptionalAsync<
>(wrapped: TWrapped): NonOptionalSchemaAsync<TWrapped, undefined>;

/**
* Creates an async non optional schema.
* Creates a non optional schema.
*
* @param wrapped The wrapped schema.
* @param message The error message.
*
* @returns An async non optional schema.
* @returns A non optional schema.
*/
export function nonOptionalAsync<
const TWrapped extends
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/nullable/nullableAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export interface NullableSchemaAsync<
}

/**
* Creates an async nullable schema.
* Creates a nullable schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async nullable schema.
* @returns A nullable schema.
*/
export function nullableAsync<
const TWrapped extends
Expand All @@ -65,12 +65,12 @@ export function nullableAsync<
>(wrapped: TWrapped): NullableSchemaAsync<TWrapped, never>;

/**
* Creates an async nullable schema.
* Creates a nullable schema.
*
* @param wrapped The wrapped schema.
* @param default_ The default value.
*
* @returns An async nullable schema.
* @returns A nullable schema.
*/
export function nullableAsync<
const TWrapped extends
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/nullish/nullishAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export interface NullishSchemaAsync<
}

/**
* Creates an async nullish schema.
* Creates a nullish schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async nullish schema.
* @returns A nullish schema.
*/
export function nullishAsync<
const TWrapped extends
Expand All @@ -65,12 +65,12 @@ export function nullishAsync<
>(wrapped: TWrapped): NullishSchemaAsync<TWrapped, never>;

/**
* Creates an async nullish schema.
* Creates a nullish schema.
*
* @param wrapped The wrapped schema.
* @param default_ The default value.
*
* @returns An async nullish schema.
* @returns A nullish schema.
*/
export function nullishAsync<
const TWrapped extends
Expand Down
8 changes: 4 additions & 4 deletions library/src/schemas/optional/optionalAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ export interface OptionalSchemaAsync<
}

/**
* Creates an async optional schema.
* Creates an optional schema.
*
* @param wrapped The wrapped schema.
*
* @returns An async optional schema.
* @returns An optional schema.
*/
export function optionalAsync<
const TWrapped extends
Expand All @@ -65,12 +65,12 @@ export function optionalAsync<
>(wrapped: TWrapped): OptionalSchemaAsync<TWrapped, never>;

/**
* Creates an async optional schema.
* Creates an optional schema.
*
* @param wrapped The wrapped schema.
* @param default_ The default value.
*
* @returns An async optional schema.
* @returns An optional schema.
*/
export function optionalAsync<
const TWrapped extends
Expand Down

0 comments on commit 3573e82

Please sign in to comment.