import { describe, it, assert, expect } from 'vitest' import SDK, { SDKCollection, SDKModel } from '../src/index.js' describe('Model', () => { describe('User', () => { class User extends SDKModel { defineProperties() { super.defineProperties() this.sdk.utils.defineAccessor(this, 'role', {}, this.getHiddenProps()) } name: string _role: string get role() { return this._role } set role(value) { this._role = value } } it('should define accessors', () => { const user = new User() expect(JSON.stringify(user)).toEqual(JSON.stringify({})) const user2 = new User({ name: 'test' }) expect(user2.name).toEqual('test') expect(Object.keys(user2)).toEqual(['role', 'name']) expect(JSON.stringify(user2)).toEqual(JSON.stringify({ name: 'test' })) const user3 = new User({ role: 'test' }) expect(user3.role).toEqual('test') expect(Object.getOwnPropertyDescriptor(user3, 'role').enumerable).toEqual(true) expect(Object.keys(user3)).toEqual(['role']) expect(JSON.stringify(user3)).toEqual(JSON.stringify({ role: 'test' })) expect(JSON.stringify(user3.clone())).toEqual(JSON.stringify({ role: 'test' })) }) it('should respect accessprs on collection', () => { const collection = SDKCollection.construct(User, () => { return {} }) const user3 = new User({ role: 'test' }) expect(collection.addItem({ name: 'John' })).toEqual({ name: 'John' }) expect(() => collection.addItem({ role: 'test' })).toThrow() expect(collection.upsertItem({ role: 'test' })).toEqual({ role: 'test' }) expect(() => collection.addItem(user3)).toThrow() expect(collection.upsertItem(user3)).toEqual({ role: 'test' }) expect(() => collection.addItem({ role: 'test', name: 'John' })).toThrow() expect(collection.upsertItem({ role: 'test', name: 'John' })).toEqual({ role: 'test', name: 'John' }) }) it('should assign implicit params', () => { var implicit = { role: 'zogg' } const collection = SDKCollection.construct(User, () => { return implicit }) expect(collection.addItem({ name: 'John' }).role).toEqual('zogg') expect(collection.upsertItem({ name: 'John' }).clone().role).toEqual('zogg') expect(collection.construct({ name: 'John' }).role).toEqual('zogg') expect(collection.construct({ name: 'John' }).clone().role).toEqual('zogg') expect(collection.new({ name: 'John' }).role).toEqual('zogg') expect(collection.new({ name: 'John' }).clone().role).toEqual('zogg') }) }) class Patient extends SDKModel { getDefaults(sdk: SDK) { return { id: 'abc' } } } it('should not overwrite given values', () => { expect(new Patient().id).toEqual(undefined) expect(new Patient({ id: null }).id).toEqual(null) expect(new Patient({ id: 'a' }).id).toEqual('a') expect(new Patient().construct()).toEqual({}) expect(new Patient().construct({ id: 'a' }).export()).toEqual({ id: 'a' }) expect(new Patient({ id: 'b' }).construct({ id: 'a' }).export()).toEqual({ id: 'a' }) expect(new Patient().setDefaults().export()).toEqual({ id: 'abc' }) expect(new Patient().construct({ id: 'a' }).setDefaults().export()).toEqual({ id: 'a' }) expect(new Patient().setDefaults().construct({ id: 'a' }).export()).toEqual({ id: 'a' }) expect(new Patient().setDefaults().clone().export()).toEqual({ id: 'abc' }) expect(new Patient().setDefaults().construct({ id: 'a' }).clone().export()).toEqual({ id: 'a' }) expect(new Patient().construct({}).export()).toEqual({}) }) class Dog extends SDKModel { modifiedAt: Date getDefaults(sdk: SDK) { return { modifiedAt: new Date(1), id: 1 } } } it('should not overwrite date', () => { expect(new Dog({}).modifiedAt).toEqual(new Date(1)) expect(new Dog({ modifiedAt: new Date(2) }).modifiedAt).toEqual(new Date(2)) expect( new Dog({}) .construct({ modifiedAt: new Date(3), id: 2 }) .export() ).toEqual({ modifiedAt: new Date(3), id: 2 }) }) })