Last active
November 26, 2017 10:24
-
-
Save cherifGsoul/3078af0dcd6f3b72f9a77921de42d8cb to your computer and use it in GitHub Desktop.
Simple example for email Address value object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var address; | |
var setAddress = function(address) { | |
/** | |
* @todo add validation here | |
*/ | |
address = address; | |
}; | |
function EmailAddress(anAddress) { | |
setAddress(address); | |
Object.defineProperty(this, 'address', { | |
value: () => { return address; } | |
}); | |
Object.freeze(this); | |
} | |
EmailAddress.prototype = { | |
equals(other) { | |
return this === other; | |
}, | |
toString(){ | |
return this.address(); | |
}, | |
valueOf() { | |
return this.address(); | |
} | |
}; | |
EmailAddress.fromString = function(anAddress) { | |
return new EmailAddress(anAddress); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let expect = require('chai').expect; | |
let EmailAddress = require('../../../../src/domain/model/passenger/email-address'); | |
describe('Email value object', () => { | |
describe('Immutability', () => { | |
beforeEach(() => { | |
this.emailAddress = new EmailAddress('cherif@site.com'); | |
}); | |
it('must be frozen', () => { | |
expect(Object.isFrozen(this.emailAddress)).to.be.true; | |
}); | |
it('is not enumerable', () => { | |
expect(this.emailAddress.hasOwnProperty('email')).to.be.false; | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment