Skip to content

Commit d1641c1

Browse files
realityendsherepixelhandler
authored andcommitted
Add failing tests for previousAttributes and…
…rollbackAttributes Current behavior is to stash an attribute’s value on EVERY call to .set(). The intent seems to be to rollback to the value fetched from the persistence layer. As illustrated by the tests: ``` post.get(‘excerpt’); // ‘Was a gambler.’ post.set('excerpt', 'Became the sheriff.'); post.set('excerpt', 'Became the chief.'); post.set('excerpt', 'Became the mayor.'); post.get('previousAttributes.excerpt’); // 'Became the chief.' ``` The previous value is simply the previous change ('Became the chief’) instead of the “original” value (“Was a gambler”). The new tests FAIL to show that “previousAttributes” and “rollbackAttributes” to not behave as expected.
1 parent b145a7a commit d1641c1

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

tests/unit/models/resource-test.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,21 +153,36 @@ test('#changedAttributes', function(assert) {
153153

154154
test('#previousAttributes', function(assert) {
155155
let post = createPost.call(this);
156+
let previous;
157+
156158
assert.equal(post.get('excerpt'), 'Was a gambler.', 'excerpt is set to "Was a gambler."');
157159
post.set('excerpt', 'Became a deputy.');
158160
assert.equal(post.get('excerpt'), 'Became a deputy.', 'excerpt is set to "Became a deputy."');
159161

160-
let previous = post.get('previousAttributes');
162+
previous = post.get('previousAttributes');
163+
assert.equal(Object.keys(previous).join(''), 'excerpt', 'previous attributes include only excerpt');
164+
assert.equal(previous.excerpt, 'Was a gambler.', 'previous excerpt value is "Was a gambler."');
165+
166+
// Several changes
167+
post.set('excerpt', 'Became the sheriff.');
168+
post.set('excerpt', 'Became the chief.');
169+
post.set('excerpt', 'Became the mayor.');
170+
171+
previous = post.get('previousAttributes');
161172
assert.equal(Object.keys(previous).join(''), 'excerpt', 'previous attributes include only excerpt');
162173
assert.equal(previous.excerpt, 'Was a gambler.', 'previous excerpt value is "Was a gambler."');
163174
});
164175

165176
test('#rollbackAttributes resets attributes based on #previousAttributes', function(assert) {
166177
let post = createPost.call(this);
178+
let previous;
179+
167180
assert.equal(post.get('excerpt'), 'Was a gambler.', 'excerpt is set to "Was a gambler."');
168181
post.set('excerpt', 'Became a deputy.');
169182
assert.equal(post.get('excerpt'), 'Became a deputy.', 'excerpt is set to "Became a deputy."');
170-
let previous = post.get('previousAttributes');
183+
184+
previous = post.get('previousAttributes');
185+
171186
assert.equal(previous.excerpt, 'Was a gambler.', 'previous excerpt value is "Was a gambler."');
172187
assert.equal(Object.keys(previous).length, 1, 'previous attribues have one change tracked');
173188

@@ -176,6 +191,19 @@ test('#rollbackAttributes resets attributes based on #previousAttributes', funct
176191
previous = post.get('previousAttributes');
177192
assert.equal(post.get('excerpt'), 'Was a gambler.', 'excerpt is set to "Was a gambler."');
178193
assert.equal(Object.keys(previous).length, 0, 'previous attribues are empty');
194+
195+
// Several changes
196+
post.set('excerpt', 'Became the sheriff.');
197+
post.set('excerpt', 'Became the chief.');
198+
post.set('excerpt', 'Became the mayor.');
199+
200+
post.rollbackAttributes();
201+
202+
previous = post.get('previousAttributes');
203+
204+
// Should rollback to initially fetched value
205+
assert.equal(post.get('excerpt'), 'Was a gambler.', 'excerpt is set to "Was a gambler."');
206+
assert.equal(Object.keys(previous).length, 0, 'previous attribues are empty');
179207
});
180208

181209
test('#rollbackRelationships resets relationships', function(assert) {

0 commit comments

Comments
 (0)