import { OwnerModel, OwnerSchema } from './owner.model'; describe('OwnerModel', () => { describe('constructor', () => { it('should create and initialize empty model', () => { const model = new OwnerModel(); expect(model.clientId).toBe(''); expect(model.userId).toBe(''); }); it('should intialize model from schema', () => { const schema: OwnerSchema = { client_id: 'test-cilent', user_id: 'test-user', }; const model = new OwnerModel(schema); expect(model.clientId).toBe(schema.client_id); expect(model.userId).toBe(schema.user_id); }); }); describe('#fromSchema', () => { it('should initialize from a schema input', () => { const model = new OwnerModel(); expect(model.clientId).toBe(''); expect(model.userId).toBe(''); const schema: OwnerSchema = { client_id: 'test-client', user_id: 'test-user', }; model.fromSchema(schema); expect(model.clientId).toBe(schema.client_id); expect(model.userId).toBe(schema.user_id); }); }); describe('#toSchema', () => { it('should build a schema object from the model', () => { const model = new OwnerModel(); model.clientId = 'test-client'; model.userId = 'test-user'; const schema = model.toSchema(); expect(schema.client_id).toBe(model.clientId); expect(schema.user_id).toBe(model.userId); }); }); describe('#sub', () => { it('should get proper sub value', () => { const model = new OwnerModel({ client_id: 'test-client', user_id: 'test-user', }); expect(model.sub).toBe('test-client|test-user'); }); it('should set proper sub value', () => { const model = new OwnerModel(); const sub = 'test-client|test-user'; model.sub = sub; expect(model.clientId).toBe('test-client'); expect(model.userId).toBe('test-user'); }); it('should throw an error if sub is invalid', () => { const model = new OwnerModel(); try { model.sub = 'badsub'; throw new Error('Should have thrown an error'); } catch (error) { // tslint:disable-next-line: no-unsafe-any expect(error.message).toBe('Invalid owner subject'); } try { model.sub = 'badsub|toomany|pipes'; throw new Error('Should have thrown an error'); } catch (error) { // tslint:disable-next-line: no-unsafe-any expect(error.message).toBe('Invalid owner subject'); } }); }); });