/
main.js
139 lines (130 loc) · 3.61 KB
/
main.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
var test = require('tape');
var Registry = require('../ampersand-registry');
var State = require('ampersand-state');
test('should throw if no type attribute', function (t) {
var r = new Registry();
var S = State.extend({
props: {
id: 'string'
}
});
var model = new S({id: '1'});
t.throws(function () {
r.store(model);
});
t.end();
});
test('should store/retrieve with a type and an id (for backward compat)', function (t) {
var r = new Registry();
var S = State.extend({
type: 'thing',
props: {
id: 'string'
}
});
var model = new S({id: '1'});
r.store(model);
t.equal(r.lookup('thing', '1'), model);
t.notEqual(r.lookup('thing', '2'), model);
t.notEqual(r.lookup('', '1'), model);
t.end();
});
test('should store/retrieve with default type attribute', function (t) {
var r = new Registry();
var S = State.extend({
modelType: 'thing',
props: {
id: 'string'
}
});
var model = new S({id: '1'});
r.store(model);
t.equal(r.lookup('thing', '1'), model);
t.notEqual(r.lookup('thing', '2'), model);
t.notEqual(r.lookup('', '1'), model);
t.end();
});
test('should store/retrieve with custom type attribute', function (t) {
var r = new Registry();
var S = State.extend({
customType: 'thing',
typeAttribute: 'customType',
props: {
id: 'string'
}
});
var model = new S({id: '1'});
r.store(model);
t.equal(r.lookup('thing', '1'), model);
t.notEqual(r.lookup('thing', '2'), model);
t.notEqual(r.lookup('', '1'), model);
t.end();
});
test('should be able to use namespaces to group further', function (t) {
var r = new Registry();
var S = State.extend({
type: 'thing',
props: {
id: 'string',
teamId: 'string'
},
namespaceAttribute: 'teamId'
});
var model1 = new S({id: '1', teamId: '1'});
var model2 = new S({id: '1', teamId: '2'});
r.store(model1);
r.store(model2);
t.notOk(r.lookup('thing', '1'), 'should fail with no namespace');
t.equal(r.lookup('thing', '1', '1'), model1);
t.equal(r.lookup('thing', '1', '2'), model2);
t.end();
});
test('should be able to pass array of models to `store`', function (t) {
var r = new Registry();
var S = State.extend({
type: 'thing',
props: {
id: 'string'
}
});
var model1 = new S({id: '1'});
var model2 = new S({id: '2'});
var model3 = new S({id: '3'});
r.store([model1, model2, model3]);
t.equal(r.lookup('thing', '1'), model1);
t.equal(r.lookup('thing', '2'), model2);
t.equal(r.lookup('thing', '3'), model3);
t.end();
});
test('should be able remove sans namespace', function (t) {
var r = new Registry();
var S = State.extend({
type: 'thing',
props: {
id: 'string'
}
});
var model = new S({id: '1'});
r.store(model);
t.equal(r.lookup('thing', '1'), model);
r.remove('thing', '1');
t.notEqual(r.lookup('thing', '1'), model);
t.end();
});
test('should be able remove with namespace', function (t) {
var r = new Registry();
var S = State.extend({
type: 'thing',
props: {
id: 'string',
teamId: 'string'
},
namespaceAttribute: 'teamId'
});
var model = new S({id: '1', teamId: '1'});
r.store(model);
t.equal(r.lookup('thing', '1', '1'), model);
r.remove('thing', '1', '1');
t.notEqual(r.lookup('thing', '1', '1'), model);
t.end();
});