@@ -4,42 +4,138 @@ User = require '../../models/user'
44
55expectedAttributes =
66 name : ' Abraham Lincoln'
7- userSince : new Date ( )
7+ userSince : Date . parse ( ' April 14, 1865 ' )
88 slugs : [' abe' , ' penniesrock' ]
99 handles : [{type : ' twitter' , handle : ' abelincoln' }]
1010 credentials : [{provider : ' twitter' , id : 1234 }]
1111 _id : 1234
1212
1313vows .describe (' User (models/user)' ).addBatch (
14- " When creating a user model" :
14+ ' When creating a user model' :
1515 topic : () ->
1616 new User ()
17- " it should have default values " : (user ) ->
18- assert .isUndefined user .name
19- assert .isUndefined user .userSince
20- assert .isArray user .slugs
21- assert .isArray user .handles
22- assert .isArray user .credentials
23- " Creating a user model from attributes" :
17+
18+ ' it should have default values ' : (user ) ->
19+ assert .isUndefined user .attributes .name
20+ assert .isUndefined user .attributes .userSince
21+ assert .isEmpty user .attributes .slugs
22+ assert .isEmpty user .attributes .handles
23+ assert .isEmpty user .attributes .credentials
24+ assert .isUndefined user .attributes ._id
25+
26+ ' and toJSON is called on the new user with a name.' :
27+ topic : (user ) ->
28+ user .attributes .name = expectedAttributes .name
29+ user .toJSON ()
30+
31+ ' it should have the users name populated. ' : (jsonResult ) ->
32+ assert .equal jsonResult .name , expectedAttributes .name
33+
34+ ' it should not have any slugs ' : (jsonResult ) ->
35+ assert .isEmpty jsonResult .slugs
36+
37+ ' it should not have any handles ' : (jsonResult ) ->
38+ assert .isEmpty jsonResult .handles
39+
40+ ' it should not have any credentials ' : (jsonResult ) ->
41+ assert .isEmpty jsonResult .credentials
42+
43+ ' it should not have the user since date set ' : (jsonResult )->
44+ assert .isUndefined jsonResult .userSince
45+
46+ ' it should not set an id ' : (jsonResult ) ->
47+ assert .isUndefined jsonResult ._id
48+
49+ ' and a brand new credential is added' :
50+ topic : () ->
51+ user = new User ()
52+ user .setCredential ' twitter' , 1865
53+ user
54+
55+ ' it should add the credential ' : (user ) ->
56+ assert .equal user .attributes .credentials .length , 1
57+ assert .equal user .attributes .credentials [0 ].provider , ' twitter'
58+ assert .equal user .attributes .credentials [0 ].id , 1865
59+
60+ ' and the same credential is added again with a different id' :
61+ topic : (user ) ->
62+ user .setCredential ' twitter' , 1234
63+ user
64+
65+ ' it should override the existing value ' : (user ) ->
66+ assert .equal user .attributes .credentials .length , 1
67+ assert .equal user .attributes .credentials [0 ].provider , ' twitter'
68+ assert .equal user .attributes .credentials [0 ].id , 1234
69+
70+ ' and the same credential is added with different casing' :
71+ topic : (user ) ->
72+ user .setCredential ' Twitter' , 1234
73+ user
74+
75+ ' it should override the existing value ' : (user ) ->
76+ assert .equal user .attributes .credentials .length , 1
77+ assert .equal user .attributes .credentials [0 ].provider , ' twitter'
78+ assert .equal user .attributes .credentials [0 ].id , 1234
79+
80+ ' and the same credential is added with different casing and white space' :
81+ topic : (user ) ->
82+ user .setCredential ' Twitter ' , 1234
83+ user
84+
85+ ' it should override the existing value ' : (user ) ->
86+ console .log user .attributes .credentials
87+ assert .equal user .attributes .credentials .length , 1
88+ assert .equal user .attributes .credentials [0 ].provider , ' twitter'
89+ assert .equal user .attributes .credentials [0 ].id , 1234
90+
91+ ' and a completely different credential is added' :
92+ topic : (user ) ->
93+ user .setCredential ' github' , 100
94+ user
95+
96+ ' it should be appended to the credentials ' : (user ) ->
97+ assert .equal user .attributes .credentials .length , 2
98+ assert .equal user .attributes .credentials [1 ].provider , ' github'
99+
100+ ' Creating a user model from attributes' :
24101 topic : () ->
25- new User expectedAttributes
102+ user = new User expectedAttributes
103+
104+ ' it should set the correct name ' : (user ) ->
105+ assert .equal user .attributes .name , expectedAttributes .name
26106
27- " it should set the correct name " : (user ) ->
28- assert .equal user .name , expectedAttributes .name
107+ ' it should set the correct user since date ' : (user ) ->
108+ assert .equal user .attributes . userSince , expectedAttributes .userSince
29109
30- " it should set the correct user since date " : (user ) ->
31- assert .equal user .userSince , expectedAttributes .userSince
110+ ' it should set the correct slugs ' : (user ) ->
111+ assert .deepEqual user .attributes . slugs , expectedAttributes .slugs
32112
33- " it should set the correct slugs " : (user ) ->
34- assert .deepEqual user .slugs , expectedAttributes .slugs
113+ ' it should set the correct handles ' : (user ) ->
114+ assert .deepEqual user .attributes . handles , expectedAttributes .handles
35115
36- " it should set the correct handles " : (user ) ->
37- assert .deepEqual user .handles , expectedAttributes .handles
116+ ' it should set the correct credentials ' : (user ) ->
117+ assert .deepEqual user .attributes . credentials , expectedAttributes .credentials
38118
39- " it should set the correct credentials " : (user ) ->
40- assert .deepEqual user .credentials , expectedAttributes .credentials
119+ ' it should set the correct id ' : (user ) ->
120+ assert .equal user .attributes . _id , expectedAttributes ._id
41121
42- " it should set the correct id " : (user ) ->
43- assert .equal user ._id , expectedAttributes ._id
122+ ' and toJSON() is called' :
123+ topic : (user ) ->
124+ user .toJSON ()
44125
126+ ' it should set the correct name ' : (jsonResult ) ->
127+ assert .equal jsonResult .name , expectedAttributes .name
128+
129+ ' it should set the correct slugs ' : (jsonResult ) ->
130+ assert .deepEqual jsonResult .slugs , expectedAttributes .slugs
131+
132+ ' it should set the correct handles ' : (jsonResult ) ->
133+ assert .deepEqual jsonResult .handles , expectedAttributes .handles
134+
135+ ' it should set the correct credentials ' : (jsonResult ) ->
136+ assert .deepEqual jsonResult .credentials , expectedAttributes .credentials
137+
138+ ' it should set the correct id ' : (jsonResult ) ->
139+ assert .equal jsonResult ._id , expectedAttributes ._id
140+
45141).export module
0 commit comments