-
Notifications
You must be signed in to change notification settings - Fork 422
/
backup_test.js
118 lines (107 loc) · 3.32 KB
/
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
/*global Recipe*/
steal("can/map/backup", "can/model", "can/test", function () {
QUnit.module('can/map/backup', {
setup: function () {
can.Map.extend('Recipe');
}
});
test('backing up', function () {
var recipe = new Recipe({
name: 'cheese'
});
ok(!recipe.isDirty(), 'not backedup, but clean');
recipe.backup();
ok(!recipe.isDirty(), 'backedup, but clean');
recipe.attr('name', 'blah');
ok(recipe.isDirty(), 'dirty');
recipe.restore();
ok(!recipe.isDirty(), 'restored, clean');
equal(recipe.name, 'cheese', 'name back');
});
test('backup / restore with associations', function () {
can.Map('Instruction');
can.Map('Cookbook');
can.Map('Recipe', {
attributes: {
instructions: 'Instruction.models',
cookbook: 'Cookbook.model'
}
}, {});
var recipe = new Recipe({
name: 'cheese burger',
instructions: [{
description: 'heat meat'
}, {
description: 'add cheese'
}],
cookbook: {
title: 'Justin\'s Grillin Times'
}
});
//test basic is dirty
ok(!recipe.isDirty(), 'not backedup, but clean');
recipe.backup();
ok(!recipe.isDirty(), 'backedup, but clean');
recipe.attr('name', 'blah');
ok(recipe.isDirty(), 'dirty');
recipe.restore();
ok(!recipe.isDirty(), 'restored, clean');
equal(recipe.name, 'cheese burger', 'name back');
// test belongs too
ok(!recipe.cookbook.isDirty(), 'cookbook not backedup, but clean');
recipe.cookbook.backup();
recipe.cookbook.attr('title', 'Brian\'s Burgers');
ok(!recipe.isDirty(), 'recipe itself is clean');
ok(recipe.isDirty(true), 'recipe is dirty if checking associations');
recipe.cookbook.restore();
ok(!recipe.isDirty(true), 'recipe is now clean with checking associations');
equal(recipe.cookbook.title, 'Justin\'s Grillin Times', 'cookbook title back');
//try belongs to recursive restore
recipe.cookbook.attr('title', 'Brian\'s Burgers');
recipe.restore();
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');
});
test('backup restore nested observables', function () {
var observe = new can.Map({
nested: {
test: 'property'
}
});
equal(observe.attr('nested')
.attr('test'), 'property', 'Nested object got converted');
observe.backup();
observe.attr('nested')
.attr('test', 'changed property');
equal(observe.attr('nested')
.attr('test'), 'changed property', 'Nested property changed');
ok(observe.isDirty(true), 'Observe is dirty');
observe.restore(true);
equal(observe.attr('nested')
.attr('test'), 'property', 'Nested object got restored');
});
test('backup removes properties that were added (#607)', function () {
var map = new can.Map({});
map.backup();
map.attr('foo', 'bar');
ok(map.isDirty(), 'the map with an additional property is dirty');
map.restore();
ok(!map.attr('foo'), 'there is no foo property');
});
test('isDirty wrapped in a compute should trigger changes #1417', function() {
expect(2);
var recipe = new Recipe({
name: 'bread'
});
recipe.backup();
var c = can.compute(function() {
return recipe.isDirty();
});
ok(!c(), 'isDirty is false');
c.bind('change', function() {
ok(c(), 'isDirty is true and a change has occurred');
});
recipe.attr('name', 'cheese');
});
});