Skip to content

Commit

Permalink
Write some tests for the enum array
Browse files Browse the repository at this point in the history
  • Loading branch information
thoom76 committed Jul 17, 2024
1 parent 79d1adf commit 8fd3461
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion test/array-items.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {Client, Table} from './utils/init'
import {Client, Entity, Table} from './utils/init'
import {ArrayItemsSchema} from './schemas'
import { ColorEnum } from './schemas/arrayItemsSchema'

// jest.setTimeout(7200 * 1000)

Expand All @@ -14,6 +15,7 @@ const table = new Table({
const expected = {
id: '1111-2222',
arrayWithTypedItems: [{bar: 'Bar', when: new Date()}],
arrayWithEnumItems: [ColorEnum.blue, ColorEnum.red, ColorEnum.white],
arrayWithoutTypedItems: ['a', '2', 3, new Date()],
}

Expand All @@ -34,6 +36,7 @@ test('Create', async () => {
expect(item.arrayWithTypedItems).toBeDefined()
expect(item.arrayWithTypedItems.length).toBe(1)
expect(item.arrayWithTypedItems[0].bar).toBe('Bar')
expect(item.arrayWithEnumItems).toBe(expected.arrayWithEnumItems)
expect(item.arrayWithoutTypedItems.length).toBe(4)
expect(item.arrayWithoutTypedItems[0]).toBe('a')

Expand Down Expand Up @@ -61,3 +64,11 @@ test('Destroy Table', async () => {
await table.deleteTable('DeleteTableForever')
expect(await table.exists()).toBe(false)
})

test('Array with enum items typing', () => {
type ArrayWithEnumItemsType = Entity<typeof ArrayItemsSchema.models.TestModel>['arrayWithEnumItems'];
const validA: ColorEnum[]|undefined = {} as ArrayWithEnumItemsType;
const validB: ArrayWithEnumItemsType = {} as ColorEnum[]|undefined;
// @ts-expect-error
const invalid: ArrayWithEnumItemsType = {} as string[]|undefined;
});
13 changes: 13 additions & 0 deletions test/schemas/arrayItemsSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
Schema to test items property for array type
*/

export enum ColorEnum {
red = 'red',
white = 'white',
blue = 'blue'
}

export default {
version: '0.0.1',
format: 'onetable:1.1.0',
Expand All @@ -26,6 +32,13 @@ export default {
},
},
},
arrayWithEnumItems: {
type: Array,
items: {
type: String,
enum: Object.values(ColorEnum),
},
},
arrayWithoutTypedItems: {type: Array, required: true},
},
} as const,
Expand Down

0 comments on commit 8fd3461

Please sign in to comment.