Mirror of https://github.com/roostorg/coop
github.com/roostorg/coop
1import {
2 ContainerTypes,
3 isContainerType,
4 ScalarTypes,
5 type ContainerType,
6 type Field,
7 type ScalarType,
8} from '@roostorg/types';
9import fc from 'fast-check';
10
11import { FieldArbitrary } from '../../test/arbitraries/ContentType.js';
12import { fieldTypeHandlers } from './fieldTypeHandlers.js';
13
14describe('Content type schemas', () => {
15 describe('fieldTypeHandlers', () => {
16 test('should never accept null as a valid field value', () => {
17 for (const [fieldType, handlers] of Object.entries(fieldTypeHandlers)) {
18 if (!isContainerType(fieldType as keyof typeof fieldTypeHandlers)) {
19 expect(
20 (handlers as (typeof fieldTypeHandlers)[ScalarType]).coerce(
21 null,
22 [],
23 ),
24 ).toBeInstanceOf(Error);
25 } else {
26 const dummyContainerFieldArb = FieldArbitrary.filter(
27 (it) => it.type === fieldType,
28 ) as fc.Arbitrary<Field<ContainerType>>;
29
30 fc.assert(
31 // eslint-disable-next-line no-loop-func
32 fc.property(dummyContainerFieldArb, (containerField) => {
33 expect(
34 (handlers as (typeof fieldTypeHandlers)[ContainerType]).coerce(
35 null as never,
36 [],
37 containerField.container as never,
38 ),
39 ).toBeInstanceOf(Error);
40 }),
41 );
42 }
43 }
44
45 // Check in values of container types too.
46 expect(
47 fieldTypeHandlers[ContainerTypes.MAP].coerce({ hello: null }, [], {
48 containerType: ContainerTypes.MAP,
49 keyScalarType: ScalarTypes.STRING,
50 valueScalarType: ScalarTypes.STRING,
51 }),
52 ).toBeInstanceOf(Error);
53 expect(
54 fieldTypeHandlers[ContainerTypes.ARRAY].coerce([null], [], {
55 containerType: ContainerTypes.ARRAY,
56 keyScalarType: null,
57 valueScalarType: ScalarTypes.STRING,
58 }),
59 ).toBeInstanceOf(Error);
60 });
61 });
62});