import { createModel } from '../create-model' import Ajv from 'ajv' import addFormats from 'ajv-formats' const ajv = new Ajv() addFormats(ajv) describe('create-model', () => { it('should create real world model', () => { const Model = createModel({ id: { type: 'string', default: '123', isArray: false, }, }) const validate = ajv.compile(Model.getSchema()) expect(validate({ id: undefined })).toBe(true) expect(Model.getSchema()).toMatchSnapshot() }) it('should basic model with values and evaluated it', () => { const TestModel = createModel({ user_id: { type: 'string' }, username: { type: 'string' }, date_create: { type: 'datetime' }, otherValue: { type: 'string' }, }) const result = new TestModel({ user_id: 'hello-user', otherValue: 'ok___', username: 'test', }) expect(result).toEqual({ user_id: 'hello-user', username: 'test', otherValue: 'ok___', }) }) it('should create model with lot values and init with few values and count loop over', () => { const TestModel = createModel({ value_1: { type: 'string' }, value_2: { type: 'string' }, value_3: { type: 'string' }, value_4: { type: 'string' }, value_5: { type: 'string' }, value_6: { type: 'string' }, value_7: { type: 'string' }, value_8: { type: 'string' }, value_9: { type: 'string' }, value_10: { type: 'string' }, value_11: { type: 'string' }, value_12: { type: 'string' }, value_13: { type: 'string' }, value_14: { type: 'string' }, value_15: { type: 'string' }, value_16: { type: 'string' }, value_17: { type: 'string' }, value_18: { type: 'string' }, value_19: { type: 'string' }, value_20: { type: 'string' }, value_21: { type: 'string' }, value_22: { type: 'string' }, value_23: { type: 'string' }, value_24: { type: 'string' }, value_25: { type: 'string' }, value_26: { type: 'string' }, value_27: { type: 'string' }, value_28: { type: 'string' }, value_29: { type: 'string' }, value_30: { type: 'string' }, }) const result = new TestModel({ value_1: 'hello-user', }) let loopCount = 0 // eslint-disable-next-line @typescript-eslint/no-unused-vars for (const _ in result) { loopCount++ } expect(loopCount).toEqual(1) }) it('should create basic model and evaluate with null', () => { const TestModel = createModel({ user_id: { type: 'string' }, username: { type: 'string' }, }) const result = new TestModel({ username: 'test', user_id: null as any }) expect(result).toEqual({ username: 'test' }) }) })