-
Notifications
You must be signed in to change notification settings - Fork 483
/
typed.spec.js
114 lines (100 loc) · 2.59 KB
/
typed.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
if (typeof(window) === 'undefined') var loki = require('../../src/lokijs.js');
describe('typed', function () {
it('works', function () {
var db = new loki('test.json');
var users;
function User(n) {
this.name = n || '';
this.log = function () {
console.log('Name: ' + this.name);
};
}
var json = {
"filename": "test.json",
"collections": [{
"name": "users",
"data": [{
"name": "joe",
"objType": "users",
"meta": {
"version": 0,
"created": 1415467401386,
"revision": 0
},
"$loki": 1
}, {
"name": "jack",
"objType": "users",
"meta": {
"version": 0,
"created": 1415467401388,
"revision": 0
},
"$loki": 2
}],
"idIndex": [1, 2],
"binaryIndices": {},
"objType": "users",
"transactional": false,
"cachedIndex": null,
"cachedBinaryIndex": null,
"cachedData": null,
"maxId": 2,
"DynamicViews": [],
"events": {
"insert": [null],
"update": [null],
"close": [],
"flushbuffer": [],
"error": [],
"delete": []
}
}],
"events": {
"close": []
},
"ENV": "NODEJS",
"fs": {}
};
// Loading only using proto:
db.loadJSON(JSON.stringify(json), {
users: {
proto: User
}
});
users = db.getCollection('users');
expect(users.get(1) instanceof User).toBe(true);
expect(users.get(1).name).toBe("joe");
// Loading using proto and inflate:
db.loadJSON(JSON.stringify(json), {
users: {
proto: User,
inflate: function(src, dest) {
dest.$loki = src.$loki;
dest.meta = src.meta;
dest.customInflater = true;
}
}
});
users = db.getCollection('users');
expect(users.get(1) instanceof User).toBe(true);
expect(users.get(1).name).toBe("");
expect(users.get(1).customInflater).toBe(true);
// Loading only using inflate:
db.loadJSON(JSON.stringify(json), {
users: {
inflate: function(src) {
var dest = {};
dest.$loki = src.$loki;
dest.meta = src.meta;
dest.onlyInflater = true;
return dest;
}
}
});
users = db.getCollection('users');
expect(users.get(1) instanceof User).toBe(false);
expect(users.get(1).name).toBe(undefined);
expect(users.get(1).onlyInflater).toBe(true);
});
});