1 | var sinon = require('sinon');
|
2 | var _ = require('underscore');
|
3 | var AppUser = require('../../src/js/models/appUser');
|
4 |
|
5 | var ClientScenario = require('../scenarios/clientScenario');
|
6 |
|
7 | describe('AppUser', function() {
|
8 | var scenario;
|
9 | var sandbox;
|
10 |
|
11 | before(function() {
|
12 | scenario = new ClientScenario();
|
13 | scenario.build();
|
14 |
|
15 | sandbox = sinon.sandbox.create();
|
16 | });
|
17 |
|
18 | afterEach(function() {
|
19 | sandbox.restore();
|
20 | });
|
21 |
|
22 | after(function() {
|
23 | scenario.clean();
|
24 | });
|
25 |
|
26 | describe('#parse', function() {
|
27 | describe('with object', function() {
|
28 | it('should set the object in the attributes', function() {
|
29 | var email = 'some@email.com';
|
30 | var givenName = 'Some';
|
31 | var surname = 'Name';
|
32 |
|
33 | var user = new AppUser({
|
34 | email: email,
|
35 | givenName: givenName,
|
36 | surname: surname
|
37 | }, {
|
38 | parse: true
|
39 | });
|
40 |
|
41 | user.get('email').should.equal(email);
|
42 | user.get('givenName').should.equal(givenName);
|
43 | user.get('surname').should.equal(surname);
|
44 |
|
45 | });
|
46 | });
|
47 |
|
48 | describe('with id', function() {
|
49 | it('should map the id in the attributes', function() {
|
50 | var id = '12345';
|
51 |
|
52 | var user = new AppUser(id, {
|
53 | parse: true
|
54 | });
|
55 |
|
56 | user.id.should.equals(id);
|
57 | user.get('id').should.equals(id);
|
58 | });
|
59 | });
|
60 | });
|
61 |
|
62 | describe('#isDirty', function() {
|
63 | it('should mark the user as dirty after init', function() {
|
64 | var email = 'some@email.com';
|
65 | var givenName = 'Some';
|
66 | var surname = 'Name';
|
67 | var id = '12345';
|
68 |
|
69 | var user = new AppUser({
|
70 | email: email,
|
71 | givenName: givenName,
|
72 | surname: surname,
|
73 | id: id
|
74 | }, {
|
75 | parse: true
|
76 | });
|
77 |
|
78 | user.isDirty().should.be.true;
|
79 | });
|
80 |
|
81 | it('should not be marked as dirty after saving', function(done) {
|
82 | var email = 'some@email.com';
|
83 | var givenName = 'Some';
|
84 | var surname = 'Name';
|
85 | var id = '12345';
|
86 |
|
87 | var user = new AppUser({
|
88 | email: email,
|
89 | givenName: givenName,
|
90 | surname: surname,
|
91 | id: id
|
92 | }, {
|
93 | parse: true
|
94 | });
|
95 |
|
96 | sandbox.stub(user, 'save', function(attributes, options) {
|
97 | return this._save(attributes, options);
|
98 | });
|
99 |
|
100 | user.isDirty().should.be.true;
|
101 |
|
102 | user.save().then(function() {
|
103 | user.isDirty().should.be.false;
|
104 | done();
|
105 | }).fail(done);
|
106 |
|
107 | });
|
108 |
|
109 | it('should not be marked as dirty if changes are reverted', function(done) {
|
110 | var email = 'some@email.com';
|
111 | var givenName = 'Some';
|
112 | var surname = 'Name';
|
113 | var id = '12345';
|
114 | var properties = {
|
115 | TEST: true
|
116 | };
|
117 |
|
118 | var user = new AppUser({
|
119 | email: email,
|
120 | givenName: givenName,
|
121 | surname: surname,
|
122 | properties: properties,
|
123 | id: id
|
124 | }, {
|
125 | parse: true
|
126 | });
|
127 |
|
128 | sandbox.stub(user, 'save', function(attributes, options) {
|
129 | return this._save(attributes, options);
|
130 | });
|
131 |
|
132 | user.isDirty().should.be.true;
|
133 |
|
134 | user.save()
|
135 | .then(function() {
|
136 | user.isDirty().should.be.false;
|
137 |
|
138 | user.set({
|
139 | email: 'other@email.com'
|
140 | });
|
141 |
|
142 | user.isDirty().should.be.true;
|
143 | })
|
144 | .then(function() {
|
145 | user.set({
|
146 | email: email
|
147 | });
|
148 | user.isDirty().should.be.false;
|
149 | done();
|
150 | })
|
151 | .fail(done);
|
152 | });
|
153 |
|
154 | it('should be true if different properties are passed', function() {
|
155 | var email = 'some@email.com';
|
156 | var givenName = 'Some';
|
157 | var surname = 'Name';
|
158 | var id = '12345';
|
159 | var properties = {
|
160 | TEST: true
|
161 | };
|
162 |
|
163 | var user = new AppUser({
|
164 | email: email,
|
165 | givenName: givenName,
|
166 | surname: surname,
|
167 | properties: properties,
|
168 | id: id
|
169 | }, {
|
170 | parse: true
|
171 | });
|
172 |
|
173 |
|
174 | user._lastPropertyValues = user.pick(AppUser.EDITABLE_PROPERTIES);
|
175 | user.isDirty().should.be.false;
|
176 |
|
177 | user.isDirty({
|
178 | email: 'other@email.com',
|
179 | properties: _.clone(properties)
|
180 | }).should.be.true;
|
181 |
|
182 | });
|
183 |
|
184 | it('should be false if same properties are passed', function() {
|
185 | var email = 'some@email.com';
|
186 | var givenName = 'Some';
|
187 | var surname = 'Name';
|
188 | var id = '12345';
|
189 | var properties = {
|
190 | TEST: true
|
191 | };
|
192 |
|
193 | var user = new AppUser({
|
194 | email: email,
|
195 | givenName: givenName,
|
196 | surname: surname,
|
197 | properties: properties,
|
198 | id: id
|
199 | }, {
|
200 | parse: true
|
201 | });
|
202 |
|
203 |
|
204 | user._lastPropertyValues = user.pick(AppUser.EDITABLE_PROPERTIES);
|
205 | user.isDirty().should.be.false;
|
206 |
|
207 | user.isDirty({
|
208 | email: email,
|
209 | properties: _.clone(properties)
|
210 | }).should.be.false;
|
211 |
|
212 | });
|
213 | });
|
214 | });
|