-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcan-define-backup_test.js
136 lines (118 loc) · 3.71 KB
/
can-define-backup_test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
var DefineMap = require('can-define/map/map');
var Observation = require('can-observation');
var canReflect = require('can-reflect');
var defineBackup = require('can-define-backup');
var MyMap = defineBackup(DefineMap.extend({}));
require('steal-qunit');
var Recipe;
QUnit.module('can/define/backup', {
beforeEach: function() {
Recipe = MyMap.extend('Recipe', {
name: 'string'
});
}
});
QUnit.test('backing up', function(assert) {
var recipe = new Recipe({
name: 'cheese'
});
assert.ok(!recipe.isDirty(), 'not backedup, but clean');
recipe.backup();
assert.ok(!recipe.isDirty(), 'backedup, but clean');
recipe.name = 'blah';
assert.ok(recipe.isDirty(), 'dirty');
recipe.restore();
assert.ok(!recipe.isDirty(), 'restored, clean');
assert.equal(recipe.name, 'cheese', 'name back');
});
QUnit.test('backup / restore with associations', function(assert) {
var Instruction = MyMap.extend('Instruction', {
description: 'string'
});
var Cookbook = MyMap.extend('Cookbook', {
title: 'string'
});
var Recipe = MyMap.extend('Recipe', {
instructions: {
Type: Instruction.List
},
cookbook: {
Type: Cookbook
}
});
var recipe = new Recipe({
name: 'cheese burger',
instructions: [{
description: 'heat meat'
}, {
description: 'add cheese'
}],
cookbook: {
title: 'Justin\'s Grillin Times'
}
});
//test basic is dirty
assert.ok(!recipe.isDirty(), 'not backedup, but clean');
recipe.backup();
assert.ok(!recipe.isDirty(), 'backedup, but clean');
//recipe.attr('name', 'blah');
recipe.name = 'blah';
assert.ok(recipe.isDirty(), 'dirty');
recipe.restore();
assert.ok(!recipe.isDirty(), 'restored, clean');
assert.equal(recipe.name, 'cheese burger', 'name back');
// test belongs too
//ok(recipe.cookbook.isDirty(), 'cookbook not backedup, but clean');
recipe.cookbook.backup();
recipe.cookbook.title = 'Brian\'s Burgers';
// ok(!recipe.isDirty(), 'recipe itself is clean');
assert.ok(recipe.isDirty(true), 'recipe is dirty if checking associations');
recipe.cookbook.restore();
// ok(!recipe.isDirty(true), 'recipe is now clean with checking associations');
assert.equal(recipe.cookbook.title, 'Justin\'s Grillin Times', 'cookbook title back');
//try belongs to recursive restore
recipe.cookbook.title = 'Brian\'s Burgers';
recipe.restore();
assert.ok(recipe.isDirty(true), 'recipe is dirty if checking associations, after a restore');
recipe.restore(true);
// ok(!recipe.isDirty(true), 'cleaned all of recipe and its associations');
});
QUnit.test('backup restore nested observables', function(assert) {
var observe = new MyMap({
nested: {
test: 'property'
}
});
assert.equal(observe.nested.test, 'property', 'Nested object got converted');
observe.backup();
observe.nested.test = 'changed property';
assert.equal(observe.nested.test, 'changed property', 'Nested property changed');
assert.ok(observe.isDirty(true), 'Observe is dirty');
observe.restore(true);
assert.equal(observe.nested.test, 'property', 'Nested object got restored');
});
QUnit.test('backup removes properties that were added (#607)', function(assert) {
var map = new MyMap({
foo: 'string'
});
map.backup();
map.foo = 'bar';
assert.ok(map.isDirty(), 'the map with an additional property is dirty');
map.restore();
assert.ok(map.foo, undefined, 'there is no foo property');
});
QUnit.test('isDirty wrapped in an observation should trigger changes #1417', function(assert) {
assert.expect(2);
var recipe = new Recipe({
name: 'bread'
});
recipe.backup();
var obs = new Observation(function(){
return recipe.isDirty();
});
assert.ok(!obs.get(), 'isDirty is false');
canReflect.onValue(obs, function(){
assert.ok(obs.get(), 'isDirty is true and a change has occurred');
});
recipe.name = 'cheese';
});