-
Notifications
You must be signed in to change notification settings - Fork 1
/
setter-test.js
113 lines (91 loc) · 2.87 KB
/
setter-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
var QUnit = require('steal-qunit');
var SetterObservable = require('./setter');
var SimpleObservable = require('../can-simple-observable');
var canReflect = require('can-reflect');
QUnit.module('can-simple-observable/setter');
QUnit.test('basics', function(assert){
var value = new SimpleObservable(2);
var obs = new SetterObservable(function(){
return value.get();
}, function(newVal){
value.set(newVal);
});
// Unbound and unobserved behavior
assert.equal(canReflect.getValue(obs), 2, 'getValue unbound');
canReflect.setValue(obs,3);
assert.equal(canReflect.getValue(value), 3, 'value set');
assert.equal(canReflect.getValue(obs), 3, 'getValue unbound');
});
QUnit.test('basics with .value', function(assert){
var value = new SimpleObservable(2);
var obs = new SetterObservable(function(){
return value.value;
}, function(newVal){
value.value = (newVal);
});
// Unbound and unobserved behavior
assert.equal(obs.value, 2, 'getValue unbound');
obs.value = 3;
assert.equal(value.value, 3, 'value set');
assert.equal(obs.value, 3, 'getValue unbound');
});
QUnit.test("get and set Priority", function(assert){
var value = new SimpleObservable(2);
var obs = new SetterObservable(function(){
return value.get();
}, function(newVal){
value.set(newVal);
});
canReflect.setPriority(obs, 5);
assert.equal(canReflect.getPriority(obs), 5, "set priority");
});
if(System.env.indexOf("production") < 0) {
QUnit.test("log observable changes", function(assert) {
var done = assert.async();
var value = new SimpleObservable(2);
var obs = new SetterObservable(function() {
return value.get();
}, function(newVal){
value.set(newVal);
});
// turn on logging
obs.log();
// override _log to spy on arguments
var changes = [];
obs._log = function(previous, current) {
changes.push({ current: current, previous: previous });
};
canReflect.onValue(obs, function() {}); // needs to be bound
canReflect.setValue(obs, 3);
canReflect.setValue(obs, 4);
assert.expect(1);
setTimeout(function() {
assert.deepEqual(
changes,
[{current: 3, previous: 2}, {current: 4, previous: 3}],
"should print out current/previous values"
);
done();
});
});
}
QUnit.test("getValueDependencies", function(assert) {
var value = new SimpleObservable(2);
var obs = new SetterObservable(function() {
return value.get();
}, function(newVal) {
value.set(newVal);
});
// unbound
assert.equal(
typeof canReflect.getValueDependencies(obs),
"undefined",
"should be undefined when observable is unbound"
);
// bound
canReflect.onValue(obs, function() {});
assert.deepEqual(
canReflect.getValueDependencies(obs).valueDependencies,
new Set([value])
);
});