forked from gaperton/Type-R
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbasic.test.js
More file actions
237 lines (190 loc) · 7.21 KB
/
basic.test.js
File metadata and controls
237 lines (190 loc) · 7.21 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
var Nested = require( '@type-r/models' ),
expect = require( 'chai' ).expect;
require( '@type-r/globals' );
var { value } = Nested;
describe( 'Basic functionality', function(){
function canHaveNativeProperties( Type ){
var C = Type.extend({
something : false,
properties : {
readOnly : function(){ return this.something; },
rw : {
get : function(){ return this.something; },
set : function( value ){
return this.something = value;
}
}
}
});
var c = new C();
expect( c.readOnly ).to.be.false;
c.rw = true;
expect( c.rw ).to.be.true;
expect( c.readOnly ).to.be.true;
}
describe( 'Events', function(){
it( 'subscription act as no op on empty source', function(){
var target = new Nested.Messenger();
target.listenTo( null, 'change', function(){} );
target.listenToOnce( null, 'change', function(){} );
target.stopListening( null );
});
});
describe( 'Nested.Model', function(){
var M = Nested.Model.extend({
urlRoot : '/root',
defaults : {
a : 'a'
}
});
it( 'may use "Model.attributes" instead of "Model.defaults"', function(){
var M = Nested.Model.extend({
attributes : {
a : 'a'
}
});
var m = new M();
expect( m.get( 'a' ) ).to.eql( 'a' );
});
it( 'create native properties for every default attribute', function(){
var m = new M();
expect( m.a ).to.eql( 'a' );
m.a = 'b';
expect( m.get( 'a' ) ).to.eql( 'b' );
expect( m.a ).to.eql( 'b' );
});
it( 'can have explicitly defined native properties', function(){
canHaveNativeProperties( Nested.Model );
});
describe( 'Record.defaults', () =>{
var Test = Nested.Model.extend({
attributes : {
text : String
}
});
const t = new Test();
it( 'has default attribute value', () =>{
expect( t.text ).to.eql( "" );
});
it( 'return default values', ()=>{
const values = t.defaults();
expect( values.text ).to.eql( "" );
});
});
it( 'inherit default attributes from the base model', function(){
var B = M.extend({
defaults : {
b : 'b'
}
});
m = new B();
expect( m.a ).to.eql( 'a' );
expect( m.b ).to.eql( 'b' );
});
it( 'deep copy defaults JSON literals on model creation', function(){
var A = Nested.Model.extend({
defaults : {
a : { first : value([ 1 ]), second : value([ 2 ]), third : value({}) }
}
});
var m = new A(),
n = new A();
m.a.first.push( 2 );
expect( m.a.first ).to.eql( [ 1, 2 ] );
expect( n.a.first ).to.eql( [ 1 ] );
});
it( 'can define a tree', function(){
var M = Nested.Model.extend();
M.define({
defaults : {
nested : M.value( null ),
elements : M.Collection
}
})
var m = new M();
m.elements.add({});
expect( m.elements.first().elements.length ).to.eql( 0 );
});
});
describe( 'Nested.Collection', function(){
var M = Nested.Model.extend({
urlRoot : '/root',
defaults : {
a : 'a'
},
collection : {
initialize : function(){
this.b = 'b';
}
}
});
it( 'can have explicitly defined native properties', function(){
canHaveNativeProperties( Nested.Collection );
});
it( 'is automatically defined for every model', function(){
var c = new M.Collection();
expect( c.model ).to.eql( M );
});
it( 'can be defined in Model.collection', function(){
var c = new M.Collection();
expect( c.b ).to.eql( 'b' );
});
it( 'inherits from the base Model.collection', function(){
var B = M.extend({
urlRoot : '/myroot',
collection : {
c : 'c'
}
});
var c = new B.Collection();
expect( c.c ).to.eql( 'c' );
expect( c.b ).to.eql( 'b' );
//expect( c.url ).to.eql( '/myroot' );
});
});
describe( 'Class type', function(){
var C = Nested.Class.extend({
a : 'a',
constructor : function(){
this.b = 'b';
}
});
it( 'has custom constructor method', function(){
var c = new C();
expect( c.a ).to.eql( 'a' );
expect( c.b ).to.eql( 'b' );
});
it( 'can be extended', function(){
var D = C.extend({
d : 'd'
});
var d = new D();
expect( d.a ).to.eql( 'a' );
expect( d.b ).to.eql( 'b' );
expect( d.d ).to.eql( 'd' );
});
it( 'Messenger can trigger/listen to backbone events', function(){
var C = Nested.Messenger.extend({
constructor : function(){
Nested.Messenger.apply( this, arguments );
this.listenTo( this, {
'hello' : function(){
this.hello = true;
},
'a b c' : function(){
this.abc = true;
}
});
}
});
var c = new C();
c.trigger( 'hello' );
expect( c.hello ).to.be.true;
c.trigger( 'b' );
expect( c.abc ).to.be.true;
});
it( 'Messenger can have explicitly defined native properties', function(){
canHaveNativeProperties( Nested.Messenger );
});
});
});