From 3e3c19ec97551c739f761e1bf8c741490bce6ba8 Mon Sep 17 00:00:00 2001 From: Talysson Date: Fri, 22 Mar 2019 09:19:27 -0300 Subject: [PATCH] Improve test coverage --- test/unit/instanceAndUpdate.spec.js | 50 ++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/test/unit/instanceAndUpdate.spec.js b/test/unit/instanceAndUpdate.spec.js index ae64725..7ccb654 100644 --- a/test/unit/instanceAndUpdate.spec.js +++ b/test/unit/instanceAndUpdate.spec.js @@ -10,6 +10,13 @@ describe('instantiating a structure', () => { type: String, default: 'Name' }, + password: { + type: String + }, + nickname: { + type: String, + default: (instance) => instance.name + }, uuid: { type: String, default: (instance) => instance.getUuid() @@ -30,27 +37,23 @@ describe('instantiating a structure', () => { }); it('has access to instance methods', () => { - const user = new User({ - name: 'Me' - }); + const user = new User(); expect(user.userMethod()).to.equal('I am a user'); }); it('has access to instance attributes created on constructor', () => { - const user = new User({ - name: 'Me' - }); + const user = new User(); expect(user.userInstanceStuff).to.equal('Stuff value'); }); it('has attributes passed to constructor assigned to the object', () => { const user = new User({ - name: 'Me' + password: 'My password' }); - expect(user.name).to.equal('Me'); + expect(user.password).to.equal('My password'); }); it('does not mutate the attributes object passed to the constructor', () => { @@ -63,7 +66,6 @@ describe('instantiating a structure', () => { it('ignores invalid attributes passed to constructor', () => { const user = new User({ - name: 'Myself', invalid: 'I will be ignored' }); @@ -72,11 +74,11 @@ describe('instantiating a structure', () => { it('reflects instance attributes to #attributes', () => { const user = new User({ - name: 'Self' + password: 'The password' }); - expect(user.name).to.equal('Self'); - expect(user.attributes.name).to.equal('Self'); + expect(user.password).to.equal('The password'); + expect(user.attributes.password).to.equal('The password'); }); describe('attributes initialization', () => { @@ -96,6 +98,30 @@ describe('instantiating a structure', () => { expect(user.uuid).to.equal('10'); }); }); + + context('when attribute dynamic default uses a static defaultable attribute', () => { + context('when static defaultable attribute uses default value', () => { + it('allows to access the value of that attribute', () => { + const user = new User(); + + expect(user.nickname).to.equal('Name'); + }); + }); + + context('when static defaultable attribute has a value passed to it', () => { + it('allows to access the value of that attribute', () => { + const user = new User({ name: 'This is my name' }); + + expect(user.nickname).to.equal('This is my name'); + }); + }); + }); + + it('overrides default value with passed value', () => { + const user = new User({ name: 'Not the default' }); + + expect(user.name).to.equal('Not the default'); + }); }); }); });