Add more tests and fix pr comments

This commit is contained in:
Christian Capeans 2023-09-07 14:21:52 +02:00
parent c939ba83c5
commit eb54aadf9a
4 changed files with 168 additions and 2 deletions

View File

@ -1,4 +1,4 @@
import { Utils } from '@strapi/strapi';
import type { Utils } from '@strapi/strapi';
type Obj = {
foo: 'bar';
@ -12,7 +12,13 @@ type Obj = {
type StringValues = Utils.Array.Values<['foo', 'bar', 'baz']>;
type NumberValues = Utils.Array.Values<[1, 2, 3]>;
type BoolValues = Utils.Array.Values<[true, false, true]>;
type TrueBoolLiteralValues = Utils.Array.Values<[true, true, true]>;
type FalseBoolLiteralValues = Utils.Array.Values<[false, false, false]>;
type ObjectValues = Utils.Array.Values<[Obj, { prop1: true; prop2: false }]>;
type MixedValues = Utils.Array.Values<[Obj, 1, 'foo', true]>;
type ContainsNever = Utils.Array.Values<[never, Obj, 1, 'foo', true]>;
// TODO move this to tuple utils
// Is Empty
@ -28,7 +34,11 @@ export {
StringValues,
NumberValues,
BoolValues,
TrueBoolLiteralValues,
FalseBoolLiteralValues,
ObjectValues,
MixedValues,
ContainsNever,
IsEmptyWithEmptyTuple,
IsEmptyWithNotEmptyTuple,
IsNotEmptyWithNotEmptyTuple,

View File

@ -1,5 +1,4 @@
import type { Utils } from '@strapi/strapi';
import { Array } from '../../../../services/entity-service/types/params/filters/operators';
// IsNever
type IsNeverGivenNever = Utils.Expression.IsNever<never>;
@ -37,6 +36,10 @@ type IsStrictEqualGivenEqualLiterals = Utils.Expression.StrictEqual<1, 1>;
type IsStrictEqualGivenEqualTypes = Utils.Expression.StrictEqual<boolean, boolean>;
type IsStrictEqualGivenDifferentLiterals = Utils.Expression.StrictEqual<1, 2>;
type IsStrictEqualGivenDifferentTypes = Utils.Expression.StrictEqual<boolean, string>;
type IsStrictEqualGivenStringAndStringLiteral = Utils.Expression.StrictEqual<string, 'hello'>;
type IsStrictEqualGivenStringLiteralAndString = Utils.Expression.StrictEqual<'hello', string>;
type IsStrictEqualGivenNumberAndNumberLiteral = Utils.Expression.StrictEqual<number, 1>;
type IsStrictEqualGivenNumberLiteralAndNumber = Utils.Expression.StrictEqual<1, number>;
// Extends
type StringExtendsString = Utils.Expression.Extends<string, string>;
@ -86,6 +89,45 @@ type ArrayNotExtendsArray = Utils.Expression.DoesNotExtends<Array<string>, Array
type TupleNotExtendsArray = Utils.Expression.DoesNotExtends<[string], Array<string>>;
type StringArrayNotExtendsArray = Utils.Expression.DoesNotExtends<string[], Array<string>>;
// If
type IfTrue = Utils.Expression.If<true, true, false>;
type IfFalse = Utils.Expression.If<false, true, false>;
type IfBoolean = Utils.Expression.If<boolean, true, false>;
type IfNumber = Utils.Expression.If<number, true, false>;
type IfString = Utils.Expression.If<string, true, false>;
type IfObject = Utils.Expression.If<object, true, false>;
type IfUnknown = Utils.Expression.If<unknown, true, false>;
type IfAny = Utils.Expression.If<any, true, false>;
type IfNever = Utils.Expression.If<never, true, false>;
type IfStringLiteral = Utils.Expression.If<'test', true, false>;
type IfNumberLiteral = Utils.Expression.If<10, true, false>;
type IfObjectLiteral = Utils.Expression.If<{ test: 1 }, true, false>;
type IfTuple = Utils.Expression.If<[1, 2, 3], true, false>;
type IfArray = Utils.Expression.If<Array<string>, true, false>;
type IfStringArray = Utils.Expression.If<string[], true, false>;
type IfTupleArray = Utils.Expression.If<[string, number], true, false>;
type IfUnion = Utils.Expression.If<string | number, true, false>;
type IfIntersection = Utils.Expression.If<string & number, true, false>;
type IfFunction = Utils.Expression.If<() => void, true, false>;
type IfClass = Utils.Expression.If<new () => void, true, false>;
type IfVoid = Utils.Expression.If<void, true, false>;
type IfNull = Utils.Expression.If<null, true, false>;
type IfUndefined = Utils.Expression.If<undefined, true, false>;
type IfWithStringReturnType = Utils.Expression.If<true, 'test', 'test2'>;
type IfWithNumberReturnType = Utils.Expression.If<true, 1, 2>;
type IfWithBooleanReturnType = Utils.Expression.If<true, true, false>;
type IfWithObjectReturnType = Utils.Expression.If<true, { foo: 1 }, { bar: 'bar' }>;
type IfWithTupleReturnType = Utils.Expression.If<true, [1, 2, 3], [4, 5, 6]>;
// TODO Check this type
type IfWithArrayReturnType = Utils.Expression.If<true, Array<string>, Array<number>>;
type IfWithUnionReturnType = Utils.Expression.If<true, string | number, string & number>;
type IfWithVoidReturnType = Utils.Expression.If<true, void, number>;
type IfWithNullReturnType = Utils.Expression.If<true, null, number>;
type IfWithUndefinedReturnType = Utils.Expression.If<true, undefined, number>;
type IfWithNeverReturnType = Utils.Expression.If<true, never, number>;
type IfWithUnknownReturnType = Utils.Expression.If<true, unknown, number>;
type IfWithAnyReturnType = Utils.Expression.If<true, any, number>;
export {
// IsNever
IsNeverGivenNever,
@ -103,6 +145,10 @@ export {
IsStrictEqualGivenEqualTypes,
IsStrictEqualGivenDifferentLiterals,
IsStrictEqualGivenDifferentTypes,
IsStrictEqualGivenStringLiteralAndString,
IsStrictEqualGivenStringAndStringLiteral,
IsStrictEqualGivenNumberAndNumberLiteral,
IsStrictEqualGivenNumberLiteralAndNumber,
// IsTrue
IsTrueGivenTrue,
IsTrueGivenFalse,
@ -165,4 +211,41 @@ export {
ArrayNotExtendsArray,
TupleNotExtendsArray,
StringArrayNotExtendsArray,
// If
IfTrue,
IfFalse,
IfBoolean,
IfNumber,
IfString,
IfObject,
IfUnknown,
IfAny,
IfNever,
IfStringLiteral,
IfNumberLiteral,
IfObjectLiteral,
IfTuple,
IfArray,
IfStringArray,
IfTupleArray,
IfUnion,
IfIntersection,
IfFunction,
IfClass,
IfVoid,
IfNull,
IfUndefined,
IfWithStringReturnType,
IfWithNumberReturnType,
IfWithBooleanReturnType,
IfWithObjectReturnType,
IfWithTupleReturnType,
IfWithArrayReturnType,
IfWithUnionReturnType,
IfWithVoidReturnType,
IfWithNullReturnType,
IfWithUndefinedReturnType,
IfWithNeverReturnType,
IfWithUnknownReturnType,
IfWithAnyReturnType,
};

View File

@ -25,12 +25,37 @@ describe('Utils.Array', () => {
]);
});
test('Mixed Values', () => {
const expectedResultType = [
t.anonymousObject({
properties: {
foo: t.stringLiteral('bar'),
baz: t.booleanLiteral(false),
prop: t.anonymousObject({
properties: {
foo: t.stringLiteral('bar'),
bar: t.stringLiteral('foo'),
},
}),
},
}),
t.numberLiteral(1),
t.stringLiteral('foo'),
t.booleanLiteral(true),
];
type('MixedValues').isUnion(expectedResultType);
// The result type should not contain the type 'never'
type('ContainsNever').isUnion(expectedResultType);
});
test('Number Values', () => {
type('NumberValues').isUnion([t.numberLiteral(1), t.numberLiteral(2), t.numberLiteral(3)]);
});
test('Bool Values', () => {
type('BoolValues').isBoolean();
type('TrueBoolLiteralValues').isBooleanLiteral(true);
type('FalseBoolLiteralValues').isBooleanLiteral(false);
});
test('Object Values', () => {

View File

@ -1,6 +1,7 @@
'use strict';
const path = require('path');
const { t } = require('ts-zen');
const { createTypeSelector } = require('../test.utils');
@ -56,6 +57,10 @@ describe('Utils.Expression', () => {
type('IsStrictEqualGivenEqualTypes').isBooleanLiteral(true);
type('IsStrictEqualGivenDifferentLiterals').isBooleanLiteral(false);
type('IsStrictEqualGivenDifferentTypes').isBooleanLiteral(false);
type('IsStrictEqualGivenStringAndStringLiteral').isBooleanLiteral(false);
type('IsStrictEqualGivenStringLiteralAndString').isBooleanLiteral(false);
type('IsStrictEqualGivenNumberAndNumberLiteral').isBooleanLiteral(false);
type('IsStrictEqualGivenNumberLiteralAndNumber').isBooleanLiteral(false);
});
test('Extends', () => {
@ -113,4 +118,47 @@ describe('Utils.Expression', () => {
type('TupleNotExtendsArray').isBooleanLiteral(false);
type('StringArrayNotExtendsArray').isBooleanLiteral(false);
});
test('If', () => {
type('IfTrue').isBooleanLiteral(true);
type('IfFalse').isBooleanLiteral(false);
type('IfBoolean').isBooleanLiteral(false);
type('IfNumber').isBooleanLiteral(false);
type('IfString').isBooleanLiteral(false);
type('IfObject').isBooleanLiteral(false);
type('IfUnknown').isBooleanLiteral(false);
type('IfAny').isBooleanLiteral(true);
type('IfNever').isBooleanLiteral(true);
type('IfStringLiteral').isBooleanLiteral(false);
type('IfTuple').isBooleanLiteral(false);
type('IfArray').isBooleanLiteral(false);
type('IfStringArray').isBooleanLiteral(false);
type('IfTupleArray').isBooleanLiteral(false);
type('IfUnion').isBooleanLiteral(false);
type('IfIntersection').isBooleanLiteral(true);
type('IfFunction').isBooleanLiteral(false);
type('IfClass').isBooleanLiteral(false);
type('IfVoid').isBooleanLiteral(false);
type('IfNull').isBooleanLiteral(false);
type('IfUndefined').isBooleanLiteral(false);
type('IfWithStringReturnType').isStringLiteral('test');
type('IfWithNumberReturnType').isNumberLiteral(1);
type('IfWithBooleanReturnType').isBooleanLiteral(true);
type('IfWithObjectReturnType').isAnonymousObject({
properties: { foo: t.numberLiteral(1) },
});
type('IfWithTupleReturnType').isTuple([
t.numberLiteral(1),
t.numberLiteral(2),
t.numberLiteral(3),
]);
type('IfWithArrayReturnType').isArray(t.string());
type('IfWithUnionReturnType').isUnion([t.string(), t.number()]);
type('IfWithVoidReturnType').isVoid();
type('IfWithNullReturnType').isNull();
type('IfWithUndefinedReturnType').isUndefined();
type('IfWithNeverReturnType').isNever();
type('IfWithUnknownReturnType').isUnknown();
type('IfWithAnyReturnType').isAny();
});
});