Skip to content

Commit

Permalink
Improve test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
talyssonoc committed Mar 22, 2019
1 parent 47ae676 commit a411022
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 14 deletions.
66 changes: 54 additions & 12 deletions test/unit/instanceAndUpdate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,20 @@ 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()
},
attrUsingMethodUsingAttr: {
type: String,
default: (instance) => instance.someMethod()
}
})(class User {
constructor() {
Expand All @@ -26,31 +37,31 @@ describe('instantiating a structure', () => {
getUuid() {
return 10;
}

someMethod() {
return `Method => ${this.name}`;
}
});
});

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', () => {
Expand All @@ -63,7 +74,6 @@ describe('instantiating a structure', () => {

it('ignores invalid attributes passed to constructor', () => {
const user = new User({
name: 'Myself',
invalid: 'I will be ignored'
});

Expand All @@ -72,11 +82,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', () => {
Expand All @@ -96,6 +106,38 @@ 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');
});
});

context('when dynamic default uses a method that uses an attribute with default', () => {
it('generates the default value properly', () => {
const user = new User();

expect(user.attrUsingMethodUsingAttr).to.equal('Method => Name');
});
});
});

it('overrides default value with passed value', () => {
const user = new User({ name: 'Not the default' });

expect(user.name).to.equal('Not the default');
});
});
});
});
Expand Down
32 changes: 30 additions & 2 deletions test/unit/subclassingStructureClass.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,19 @@ describe('subclassing a structure with another structure', () => {

beforeEach(() => {
User = attributes({
name: String
name: String,
uuid: {
type: String,
default: () => 123
}
})(class User {});

Admin = attributes({
level: Number
level: Number,
identifier: {
type: String,
default: (instance) => `Admin-${instance.uuid}`
}
})(class Admin extends User {});
});

Expand All @@ -185,4 +193,24 @@ describe('subclassing a structure with another structure', () => {
expect(admin).to.be.instanceOf(Admin);
expect(admin).to.be.instanceOf(User);
});

context.only('default value', () => {
context('when subclass uses an attribute from superclass to generate a default value', () => {
context('when superclass uses default', () => {
it('allows to access it properly', () => {
const admin = new Admin();

expect(admin.identifier).to.equal('Admin-123');
});
});

context('when a value is passed to superclass defaultable attribute', () => {
it('allows to access it properly', () => {
const admin = new Admin({ uuid: '321' });

expect(admin.identifier).to.equal('Admin-321');
});
});
});
});
});

0 comments on commit a411022

Please sign in to comment.