/
observableBehaviors.js
73 lines (60 loc) · 2.43 KB
/
observableBehaviors.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
/// <reference path="../src/observable.js" />
describe('Observable', {
'Should be subscribable': function () {
var instance = new ko.observable();
value_of(ko.isSubscribable(instance)).should_be(true);
},
'Should advertise that instances are observable': function () {
var instance = new ko.observable();
value_of(ko.isObservable(instance)).should_be(true);
},
'Should be able to write values to it': function () {
var instance = new ko.observable();
instance(123);
},
'Should advertise that instances can have values written to them': function () {
var instance = new ko.observable(function () { });
value_of(ko.isWriteableObservable(instance)).should_be(true);
},
'Should be able to read back most recent value': function () {
var instance = new ko.observable();
instance(123);
instance(234);
value_of(instance()).should_be(234);
},
'Should initially have undefined value': function () {
var instance = new ko.observable();
value_of(instance()).should_be(undefined);
},
'Should be able to set initial value as constructor param': function () {
var instance = new ko.observable('Hi!');
value_of(instance()).should_be('Hi!');
},
'Should notify subscribers about each new value': function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value);
});
instance('A');
instance('B');
value_of(notifiedValues.length).should_be(2);
value_of(notifiedValues[0]).should_be('A');
value_of(notifiedValues[1]).should_be('B');
},
'Should be able to tell it that its value has mutated, at which point it notifies subscribers': function () {
var instance = new ko.observable();
var notifiedValues = [];
instance.subscribe(function (value) {
notifiedValues.push(value.childProperty);
});
var someUnderlyingObject = { childProperty : "A" };
instance(someUnderlyingObject);
value_of(notifiedValues.length).should_be(1);
value_of(notifiedValues[0]).should_be("A");
someUnderlyingObject.childProperty = "B";
instance.valueHasMutated();
value_of(notifiedValues.length).should_be(2);
value_of(notifiedValues[1]).should_be("B");
}
});