Last active
November 15, 2015 13:07
-
-
Save juanpujol/eb1d2aca614a53937834 to your computer and use it in GitHub Desktop.
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
(function() { | |
'use strict'; | |
angular.module('angularModelTest').factory('Account', AccountModel); | |
function AccountModel() { | |
var defaults = { | |
name: 'Unnamed Account' | |
}; | |
var Account = function(properties) { | |
var p = properties || {}; | |
for (var key in p){ | |
this[key] = p[key]; | |
} | |
this.name = p.name || defaults.name; | |
this.email = p.email; | |
}; | |
function $destroy() { | |
delete this.name; | |
delete this.email; | |
}; | |
angular.extend(Account.prototype, { | |
$destroy: $destroy | |
}); | |
return Account; | |
} | |
}()); |
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
'use strict'; | |
describe('Model: Account', function() { | |
var account, model, Account; | |
beforeEach(function() { | |
module('angularModelTest') | |
inject(function($injector) { | |
Account = $injector.get('Account'); | |
}); | |
model = { | |
name: 'John Doe', | |
email: '[email protected]' | |
}; | |
account = new Account(model); | |
}); | |
it('should be an instance of Account', function() { | |
expect(account instanceof Account).toBeTruthy(); | |
}); | |
describe('on initialization', function() { | |
it('should set default values on an empty account', function() { | |
account = new Account(); | |
expect(account.name).toBe('Unnamed Account'); | |
}) | |
it('should set model on the account', function() { | |
expect(account.name).toBe('John Doe'); | |
expect(account.email).toBe('[email protected]'); | |
}); | |
it('should override any default values', function () { | |
model = { name: 'Jane Doe' }; | |
account = new Account(model); | |
expect(account.name).toBe('Jane Doe'); | |
}); | |
it('should set default values on an account if they are missing', function() { | |
model = { email: '[email protected]' }; | |
account = new Account(model); | |
expect(account.email).toBe(model.email); | |
expect(account.name).toBe('Unnamed Account'); | |
}); | |
it('should set and get any unknown attribute', function() { | |
var key = 'random' + (new Date()).getTime(); | |
model[key] = true; | |
account = new Account(model); | |
expect(account[key]).toBeTruthy(); | |
}); | |
}); | |
describe('$destroy()', function() { | |
it('should remove all properties from account', function () { | |
account.$destroy(); | |
expect(account.name).toBeUndefined(); | |
expect(account.email).toBeUndefined(); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment