Skip to content

Commit

Permalink
chore: refine the test
Browse files Browse the repository at this point in the history
  • Loading branch information
HuiSF committed Aug 2, 2024
1 parent cce2bdb commit 42dd55a
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions packages/core/__tests__/singleton/Auth/type.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,57 @@ import { JWT } from '../../../src/singleton/Auth/types';

describe('type validity', () => {
describe('JWT type', () => {
it('can contain property that has a value as array of JsonObject', () => {
const value: JWT = {
payload: { otherProperty: [{ key: '123' }] },
toString: () => 'mock',
};
const a = value.payload.otherProperty as { key: string }[];

expect(a).toEqual([{ key: '123' }]);
});

it('can contain property that has a value as array of JsonObject and JsonPrimitive', () => {
const value: JWT = {
payload: { otherProperty: [1, 2, 'hi', { key: 'value' }] },
toString: () => 'mock',
};
const a = value.payload.otherProperty as (
it('can contain property that has a value as array of JsonObjects', () => {
type OtherProperty1 = (
| { key: string }
| number
| string
| (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[]
)[];

expect(a).toEqual([1, 2, 'hi', { key: 'value' }]);
});

it('can contain property that has a value as array of JsonArray', () => {
// For testing purpose, use type alias here, as TS will complain while using
// an interface which triggers structural typing check
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
type OtherProperty2 = {
key: number;
array: (
| { key: string }
| number
| string
| ({ key: string } | number | string)[]
)[];
};
const expectedOtherProperty1 = [
{ key: '123' },
1,
'hi',
[1, 'hi', { key: '345' }, [2, 'hi', { key: '456' }]],
];
const expectedOtherProperty2 = {
key: 1,
array: [1, 'hi', { key: '123' }, [2, 'hi', { key: '456' }]],
};
const value: JWT = {
payload: {
otherProperty: [[2, 'hi', { key: 'value' }], 1],
otherProperty1: expectedOtherProperty1,
otherProperty2: expectedOtherProperty2,
},
toString: () => 'mock',
};
const a = value.payload.otherProperty as (
| ({ key: string } | number | string)[]
| number
)[];

expect(a).toEqual([[2, 'hi', { key: 'value' }], 1]);
const extractedOtherProperty1 = value.payload
.otherProperty1 as OtherProperty1;
const a: OtherProperty1 = extractedOtherProperty1;
expect(a).toEqual(expectedOtherProperty1);

const extractedOtherProperty2 = value.payload
.otherProperty2 as OtherProperty2;
const b: OtherProperty2 = extractedOtherProperty2;
expect(b).toEqual(expectedOtherProperty2);
});
});
});

0 comments on commit 42dd55a

Please sign in to comment.