/
loud-accessors.spec.js
191 lines (155 loc) · 5.28 KB
/
loud-accessors.spec.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/* Setup tests */
if ( typeof define !== "undefined" && define.amd ) {
// AMD
buster.spec.expose();
require.config({
paths: {
"wheels-loud-accessors": "./wheels-loud-accessors",
"wheels-pubsub": "spec/lib/dependencies-built"
}
});
describe("When required with AMD", function( run ) {
require( ["wheels-loud-accessors"], function( LoudAccessors ) {
run( function() {
runTests.call( this, LoudAccessors );
});
});
});
} else if ( typeof exports !== "undefined" ) {
// CommonJS / Node
var buster = require("buster"),
LoudAccessors = require("../wheels-loud-accessors");
runTests( LoudAccessors );
} else {
// Browser global
runTests( Wheels.LoudAccessors );
}
function runTests( LoudAccessors ) {
buster.spec.expose();
describe("LoudAccessors", function() {
before(function() {
this.loudProps = new LoudAccessors();
});
describe("set", function() {
it("sets the value of the attribute", function() {
this.loudProps.set( "foo", "bar" );
expect( this.loudProps._attributes.foo ).toEqual("bar");
});
it("publishes generic change event", function() {
var cbk = this.spy();
this.loudProps.on( "change", cbk );
this.loudProps.set( "foo", 123 );
expect( cbk ).toHaveBeenCalledOnce();
});
it("passes to generic change event handlers the name and value of the attribute", function() {
var cbk = this.spy();
this.loudProps.on( "change", cbk );
this.loudProps.set( "foo", 123 );
expect( cbk ).toHaveBeenCalledWith( "change", "foo", 123 );
});
it("publishes change:<attribute> event", function() {
var cbk = this.spy();
this.loudProps.on( "change:foo", cbk );
this.loudProps.set( "foo", 123 );
expect( cbk ).toHaveBeenCalledOnce();
});
it("passes to change:<attribute> event handlers the name and value of the attribute", function() {
var cbk = this.spy();
this.loudProps.on( "change:foo", cbk );
this.loudProps.set( "foo", 123 );
expect( cbk ).toHaveBeenCalledWith( "change:foo", "foo", 123 );
});
it("does not publish generic change event if silent option is true", function() {
var cbk = this.spy();
this.loudProps.on( "change", cbk );
this.loudProps.set( "foo", 123, { silent: true } );
refute.called( cbk );
});
it("does not publish change:<attribute> event if silent option is true", function() {
var cbk = this.spy();
this.loudProps.on( "change:foo", cbk );
this.loudProps.set( "foo", 123, { silent: true } );
refute.called( cbk );
});
});
describe("get", function() {
it("returns the value of attribute", function() {
this.loudProps._attributes = {};
this.loudProps._attributes.foo = "bar";
expect( this.loudProps.get("foo") ).toEqual("bar");
});
it("publishes generic read event", function() {
var cbk = this.spy();
this.loudProps.on( "read", cbk );
this.loudProps.get( "foo" );
expect( cbk ).toHaveBeenCalledOnce();
});
it("passes to generic read event handlers the name and value of the attribute", function() {
var cbk = this.spy();
this.loudProps._attributes = { foo: 123 };
this.loudProps.on( "read", cbk );
this.loudProps.get( "foo" );
expect( cbk ).toHaveBeenCalledWith( "read", "foo", 123 );
});
it("publishes read:<attribute> event", function() {
var cbk = this.spy();
this.loudProps.on( "read:foo", cbk );
this.loudProps.get( "foo" );
expect( cbk ).toHaveBeenCalledOnce();
});
it("passes to read:<attribute> event handlers the name and value of the attribute", function() {
var cbk = this.spy();
this.loudProps._attributes = { foo: 123 };
this.loudProps.on( "read:foo", cbk );
this.loudProps.get( "foo" );
expect( cbk ).toHaveBeenCalledWith( "read:foo", "foo", 123 );
});
it("does not publish generic read event if silent option is true", function() {
var cbk = this.spy();
this.loudProps.on( "read", cbk );
this.loudProps.get( "foo", { silent: true } );
refute.called( cbk );
});
it("does not publish read:<attribute> event if silent option is true", function() {
var cbk = this.spy();
this.loudProps.on( "read:foo", cbk );
this.loudProps.get( "foo", { silent: true } );
refute.called( cbk );
});
});
describe("class methods", function() {
describe("attrAccessors", function() {
it("defines a properties with loud setters", function() {
var spy = this.spy(),
Foo = LoudAccessors.subclass(function() {
this.attrAccessors("bar");
}),
foo = new Foo();
if ( Object.defineProperty ) {
this.stub( foo, "set", spy );
foo.bar = 123;
expect( spy ).toHaveBeenCalledWith( "bar", 123 );
} else {
// Dummy assertion for engines not supporting Object.defineProperty
expect( true ).toEqual( true );
}
});
it("defines properties with loud getters", function() {
var spy = this.spy(),
Foo = LoudAccessors.subclass(function() {
this.attrAccessors("bar");
}),
foo = new Foo();
if ( Object.defineProperty ) {
this.stub( foo, "get", spy );
var bar = foo.bar;
expect( spy ).toHaveBeenCalledWith( "bar" );
} else {
// Dummy assertion for engines not supporting Object.defineProperty
expect( true ).toEqual( true );
}
});
});
});
});
};