|
| 1 | +var User, assert, expectedAttributes, vows; |
| 2 | + |
| 3 | +vows = require('vows'); |
| 4 | + |
| 5 | +assert = require('assert'); |
| 6 | + |
| 7 | +User = require('../../models/user'); |
| 8 | + |
| 9 | +expectedAttributes = { |
| 10 | + name: 'Abraham Lincoln', |
| 11 | + userSince: Date.parse('April 14, 1865'), |
| 12 | + slugs: ['abe', 'penniesrock'], |
| 13 | + handles: [ |
| 14 | + { |
| 15 | + type: 'twitter', |
| 16 | + handle: 'abelincoln' |
| 17 | + } |
| 18 | + ], |
| 19 | + credentials: [ |
| 20 | + { |
| 21 | + provider: 'twitter', |
| 22 | + id: 1234 |
| 23 | + } |
| 24 | + ], |
| 25 | + _id: 1234 |
| 26 | +}; |
| 27 | + |
| 28 | +vows.describe('User (models/user)').addBatch({ |
| 29 | + 'When creating a user model': { |
| 30 | + topic: function() { |
| 31 | + return new User(); |
| 32 | + }, |
| 33 | + 'it should have default values': function(user) { |
| 34 | + assert.isUndefined(user.attributes.name); |
| 35 | + assert.isUndefined(user.attributes.userSince); |
| 36 | + assert.isEmpty(user.attributes.slugs); |
| 37 | + assert.isEmpty(user.attributes.handles); |
| 38 | + assert.isEmpty(user.attributes.credentials); |
| 39 | + return assert.isUndefined(user.attributes._id); |
| 40 | + }, |
| 41 | + 'and toJSON is called on the new user with a name.': { |
| 42 | + topic: function(user) { |
| 43 | + user.attributes.name = expectedAttributes.name; |
| 44 | + return user.toJSON(); |
| 45 | + }, |
| 46 | + 'it should have the users name populated.': function(jsonResult) { |
| 47 | + return assert.equal(jsonResult.name, expectedAttributes.name); |
| 48 | + }, |
| 49 | + 'it should not have any slugs': function(jsonResult) { |
| 50 | + return assert.isEmpty(jsonResult.slugs); |
| 51 | + }, |
| 52 | + 'it should not have any handles': function(jsonResult) { |
| 53 | + return assert.isEmpty(jsonResult.handles); |
| 54 | + }, |
| 55 | + 'it should not have any credentials': function(jsonResult) { |
| 56 | + return assert.isEmpty(jsonResult.credentials); |
| 57 | + }, |
| 58 | + 'it should not have the user since date set': function(jsonResult) { |
| 59 | + return assert.isUndefined(jsonResult.userSince); |
| 60 | + }, |
| 61 | + 'it should not set an id': function(jsonResult) { |
| 62 | + return assert.isUndefined(jsonResult._id); |
| 63 | + } |
| 64 | + }, |
| 65 | + 'and a brand new credential is added': { |
| 66 | + topic: function() { |
| 67 | + var user; |
| 68 | + user = new User(); |
| 69 | + user.setCredential('twitter', 1865); |
| 70 | + return user; |
| 71 | + }, |
| 72 | + 'it should add the credential': function(user) { |
| 73 | + assert.equal(user.attributes.credentials.length, 1); |
| 74 | + assert.equal(user.attributes.credentials[0].provider, 'twitter'); |
| 75 | + return assert.equal(user.attributes.credentials[0].id, 1865); |
| 76 | + }, |
| 77 | + 'and the same credential is added again with a different id': { |
| 78 | + topic: function(user) { |
| 79 | + user.setCredential('twitter', 1234); |
| 80 | + return user; |
| 81 | + }, |
| 82 | + 'it should override the existing value': function(user) { |
| 83 | + assert.equal(user.attributes.credentials.length, 1); |
| 84 | + assert.equal(user.attributes.credentials[0].provider, 'twitter'); |
| 85 | + return assert.equal(user.attributes.credentials[0].id, 1234); |
| 86 | + } |
| 87 | + }, |
| 88 | + 'and the same credential is added with different casing': { |
| 89 | + topic: function(user) { |
| 90 | + user.setCredential('Twitter', 1234); |
| 91 | + return user; |
| 92 | + }, |
| 93 | + 'it should override the existing value': function(user) { |
| 94 | + assert.equal(user.attributes.credentials.length, 1); |
| 95 | + assert.equal(user.attributes.credentials[0].provider, 'twitter'); |
| 96 | + return assert.equal(user.attributes.credentials[0].id, 1234); |
| 97 | + } |
| 98 | + }, |
| 99 | + 'and the same credential is added with different casing and white space': { |
| 100 | + topic: function(user) { |
| 101 | + user.setCredential(' Twitter ', 1234); |
| 102 | + return user; |
| 103 | + }, |
| 104 | + 'it should override the existing value': function(user) { |
| 105 | + assert.equal(user.attributes.credentials.length, 1); |
| 106 | + assert.equal(user.attributes.credentials[0].provider, 'twitter'); |
| 107 | + return assert.equal(user.attributes.credentials[0].id, 1234); |
| 108 | + } |
| 109 | + }, |
| 110 | + 'and a completely different credential is added': { |
| 111 | + topic: function(user) { |
| 112 | + user.setCredential('github', 100); |
| 113 | + return user; |
| 114 | + }, |
| 115 | + 'it should be appended to the credentials': function(user) { |
| 116 | + assert.equal(user.attributes.credentials.length, 2); |
| 117 | + return assert.equal(user.attributes.credentials[1].provider, 'github'); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + 'Creating a user model from attributes': { |
| 123 | + topic: function() { |
| 124 | + var user; |
| 125 | + return user = new User(expectedAttributes); |
| 126 | + }, |
| 127 | + 'it should set the correct name': function(user) { |
| 128 | + return assert.equal(user.attributes.name, expectedAttributes.name); |
| 129 | + }, |
| 130 | + 'it should set the correct user since date': function(user) { |
| 131 | + return assert.equal(user.attributes.userSince, expectedAttributes.userSince); |
| 132 | + }, |
| 133 | + 'it should set the correct slugs': function(user) { |
| 134 | + return assert.deepEqual(user.attributes.slugs, expectedAttributes.slugs); |
| 135 | + }, |
| 136 | + 'it should set the correct handles': function(user) { |
| 137 | + return assert.deepEqual(user.attributes.handles, expectedAttributes.handles); |
| 138 | + }, |
| 139 | + 'it should set the correct credentials': function(user) { |
| 140 | + return assert.deepEqual(user.attributes.credentials, expectedAttributes.credentials); |
| 141 | + }, |
| 142 | + 'it should set the correct id': function(user) { |
| 143 | + return assert.equal(user.attributes._id, expectedAttributes._id); |
| 144 | + }, |
| 145 | + 'and toJSON() is called': { |
| 146 | + topic: function(user) { |
| 147 | + return user.toJSON(); |
| 148 | + }, |
| 149 | + 'it should set the correct name': function(jsonResult) { |
| 150 | + return assert.equal(jsonResult.name, expectedAttributes.name); |
| 151 | + }, |
| 152 | + 'it should set the correct slugs': function(jsonResult) { |
| 153 | + return assert.deepEqual(jsonResult.slugs, expectedAttributes.slugs); |
| 154 | + }, |
| 155 | + 'it should set the correct handles': function(jsonResult) { |
| 156 | + return assert.deepEqual(jsonResult.handles, expectedAttributes.handles); |
| 157 | + }, |
| 158 | + 'it should set the correct credentials': function(jsonResult) { |
| 159 | + return assert.deepEqual(jsonResult.credentials, expectedAttributes.credentials); |
| 160 | + }, |
| 161 | + 'it should set the correct id': function(jsonResult) { |
| 162 | + return assert.equal(jsonResult._id, expectedAttributes._id); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | +})["export"](module); |
0 commit comments