@entropic-bond/firebase 1.7.9 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,44 +1,34 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
12
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
13
4
  };
14
5
  Object.defineProperty(exports, "__esModule", { value: true });
15
- const node_fetch_1 = __importDefault(require("node-fetch"));
16
6
  const entropic_bond_1 = require("entropic-bond");
17
7
  const firebase_datasource_1 = require("./firebase-datasource");
18
8
  const firebase_helper_1 = require("../firebase-helper");
19
9
  const test_user_1 = require("../mocks/test-user");
20
10
  const mock_data_json_1 = __importDefault(require("../mocks/mock-data.json"));
21
- const firestore_1 = require("firebase/firestore");
22
- function loadTestData(model) {
23
- return __awaiter(this, void 0, void 0, function* () {
24
- const users = mock_data_json_1.default.TestUser;
25
- const promises = [];
26
- for (const key in users) {
27
- const user = entropic_bond_1.Persistent.createInstance(users[key]);
28
- promises.push(model.save(user));
29
- }
30
- yield Promise.all(promises);
31
- });
11
+ const node_fetch_1 = __importDefault(require("node-fetch"));
12
+ async function loadTestData(model) {
13
+ const users = Object.values(mock_data_json_1.default.TestUser);
14
+ await Promise.all(users.map(userObj => {
15
+ const user = entropic_bond_1.Persistent.createInstance(userObj);
16
+ return model.save(user);
17
+ }));
32
18
  }
33
19
  describe('Firestore Model', () => {
34
20
  let model;
35
21
  let testUser;
36
- firebase_helper_1.FirebaseHelper.setFirebaseConfig({
37
- projectId: "demo-test",
38
- });
39
- firebase_helper_1.FirebaseHelper.useEmulator({ firestorePort: 9080 });
40
- beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
22
+ const host = 'localhost';
23
+ const firestorePort = 9080;
24
+ beforeAll(() => {
25
+ firebase_helper_1.FirebaseHelper.setFirebaseConfig({
26
+ projectId: "demo-test",
27
+ });
28
+ firebase_helper_1.FirebaseHelper.useEmulator({ host, firestorePort });
41
29
  entropic_bond_1.Store.useDataSource(new firebase_datasource_1.FirebaseDatasource());
30
+ });
31
+ beforeEach(async () => {
42
32
  testUser = new test_user_1.TestUser();
43
33
  testUser.name = {
44
34
  firstName: 'testUserFirstName',
@@ -47,37 +37,37 @@ describe('Firestore Model', () => {
47
37
  testUser.age = 35;
48
38
  testUser.skills = ['lazy', 'dirty'];
49
39
  model = entropic_bond_1.Store.getModel('TestUser');
50
- yield loadTestData(model);
51
- }));
52
- afterEach(() => __awaiter(void 0, void 0, void 0, function* () {
53
- yield (0, firestore_1.terminate)(firebase_helper_1.FirebaseHelper.instance.firestore());
54
- yield (0, node_fetch_1.default)('http://localhost:9080/emulator/v1/projects/demo-test/databases/(default)/documents', {
40
+ await loadTestData(model);
41
+ });
42
+ afterEach(async () => {
43
+ await (0, node_fetch_1.default)(`http://${host}:${firestorePort}/emulator/v1/projects/demo-test/databases/(default)/documents`, {
55
44
  method: 'DELETE'
56
45
  });
57
- }));
58
- it('should find document by id', () => __awaiter(void 0, void 0, void 0, function* () {
59
- yield model.save(testUser);
60
- const user = yield model.findById(testUser.id);
46
+ });
47
+ it('should find document by id', async () => {
48
+ var _a;
49
+ await model.save(testUser);
50
+ const user = await model.findById(testUser.id);
61
51
  expect(user).toBeInstanceOf(test_user_1.TestUser);
62
- expect(user.id).toEqual(testUser.id);
63
- expect(user.name.firstName).toEqual('testUserFirstName');
64
- }));
65
- it('should write a document', () => __awaiter(void 0, void 0, void 0, function* () {
66
- yield model.save(testUser);
67
- const newUser = yield model.findById(testUser.id);
68
- expect(newUser.name).toEqual({
52
+ expect(user === null || user === void 0 ? void 0 : user.id).toEqual(testUser.id);
53
+ expect((_a = user === null || user === void 0 ? void 0 : user.name) === null || _a === void 0 ? void 0 : _a.firstName).toEqual('testUserFirstName');
54
+ });
55
+ it('should write a document', async () => {
56
+ await model.save(testUser);
57
+ const newUser = await model.findById(testUser.id);
58
+ expect(newUser === null || newUser === void 0 ? void 0 : newUser.name).toEqual({
69
59
  firstName: 'testUserFirstName',
70
60
  lastName: 'testUserLastName'
71
61
  });
72
- }));
73
- it('should delete a document by id', () => __awaiter(void 0, void 0, void 0, function* () {
74
- yield model.save(testUser);
75
- const newUser = yield model.findById(testUser.id);
76
- expect(newUser.age).toBe(35);
77
- yield model.delete(testUser.id);
78
- const deletedUser = yield model.findById(testUser.id);
62
+ });
63
+ it('should delete a document by id', async () => {
64
+ await model.save(testUser);
65
+ const newUser = await model.findById(testUser.id);
66
+ expect(newUser === null || newUser === void 0 ? void 0 : newUser.age).toBe(35);
67
+ await model.delete(testUser.id);
68
+ const deletedUser = await model.findById(testUser.id);
79
69
  expect(deletedUser).toBeUndefined();
80
- }));
70
+ });
81
71
  it('should not throw if a document id doesn\'t exists', (done) => {
82
72
  expect(() => {
83
73
  model.findById('nonExistingId')
@@ -85,28 +75,28 @@ describe('Firestore Model', () => {
85
75
  .catch(done);
86
76
  }).not.toThrow();
87
77
  });
88
- it('should return undefined if a document id doesn\'t exists', () => __awaiter(void 0, void 0, void 0, function* () {
89
- expect(yield model.findById('nonExistingId')).toBeUndefined();
90
- }));
91
- it('should retrieve array fields', () => __awaiter(void 0, void 0, void 0, function* () {
92
- yield model.save(testUser);
93
- const newUser = yield model.findById(testUser.id);
94
- expect(Array.isArray(newUser.skills)).toBeTruthy();
95
- expect(newUser.skills).toEqual(expect.arrayContaining(['lazy', 'dirty']));
96
- }));
97
- it('should retrieve object fields', () => __awaiter(void 0, void 0, void 0, function* () {
98
- yield model.save(testUser);
99
- const newUser = yield model.findById(testUser.id);
100
- expect(newUser.name).toEqual({
78
+ it('should return undefined if a document id doesn\'t exists', async () => {
79
+ expect(await model.findById('nonExistingId')).toBeUndefined();
80
+ });
81
+ it('should retrieve array fields', async () => {
82
+ await model.save(testUser);
83
+ const newUser = await model.findById(testUser.id);
84
+ expect(Array.isArray(newUser === null || newUser === void 0 ? void 0 : newUser.skills)).toBeTruthy();
85
+ expect(newUser === null || newUser === void 0 ? void 0 : newUser.skills).toEqual(expect.arrayContaining(['lazy', 'dirty']));
86
+ });
87
+ it('should retrieve object fields', async () => {
88
+ await model.save(testUser);
89
+ const newUser = await model.findById(testUser.id);
90
+ expect(newUser === null || newUser === void 0 ? void 0 : newUser.name).toEqual({
101
91
  firstName: 'testUserFirstName',
102
92
  lastName: 'testUserLastName'
103
93
  });
104
- }));
94
+ });
105
95
  describe('Generic find', () => {
106
- it('should query all admins with query object', () => __awaiter(void 0, void 0, void 0, function* () {
96
+ it('should query all admins with query object', async () => {
107
97
  testUser.admin = true;
108
- yield model.save(testUser);
109
- const admins = yield model.query({
98
+ await model.save(testUser);
99
+ const admins = await model.query({
110
100
  operations: [{
111
101
  property: 'admin',
112
102
  operator: '==',
@@ -115,22 +105,24 @@ describe('Firestore Model', () => {
115
105
  });
116
106
  expect(admins.length).toBeGreaterThanOrEqual(1);
117
107
  expect(admins[0]).toBeInstanceOf(test_user_1.TestUser);
118
- }));
119
- it('should find all admins with where methods', () => __awaiter(void 0, void 0, void 0, function* () {
120
- const admins = yield model.find().where('admin', '==', true).get();
108
+ });
109
+ it('should find all admins with where methods', async () => {
110
+ const admins = await model.find().where('admin', '==', true).get();
121
111
  expect(admins.length).toBeGreaterThanOrEqual(1);
122
112
  expect(admins[0]).toBeInstanceOf(test_user_1.TestUser);
123
- }));
124
- it('should find admins with age less than 56', () => __awaiter(void 0, void 0, void 0, function* () {
125
- const admins = yield model.find()
113
+ });
114
+ it('should find admins with age less than 56', async () => {
115
+ var _a;
116
+ const admins = await model.find()
126
117
  .where('admin', '==', true)
127
118
  .where('age', '<', 50)
128
119
  .get();
129
120
  expect(admins.length).toBeGreaterThanOrEqual(1);
130
- expect(admins[0].age).toBeLessThan(50);
131
- }));
132
- it('should query by subproperties', () => __awaiter(void 0, void 0, void 0, function* () {
133
- const users = yield model.query({
121
+ expect((_a = admins[0]) === null || _a === void 0 ? void 0 : _a.age).toBeLessThan(50);
122
+ });
123
+ it('should query by subproperties', async () => {
124
+ var _a;
125
+ const users = await model.query({
134
126
  operations: [
135
127
  {
136
128
  property: 'name',
@@ -140,48 +132,52 @@ describe('Firestore Model', () => {
140
132
  { property: 'age', operator: '!=', value: 134 }
141
133
  ]
142
134
  });
143
- expect(users[0].id).toBe('user3');
144
- }));
145
- it('should find by subproperties', () => __awaiter(void 0, void 0, void 0, function* () {
146
- const users = yield model.find()
135
+ expect((_a = users[0]) === null || _a === void 0 ? void 0 : _a.id).toBe('user3');
136
+ });
137
+ it('should find by subproperties', async () => {
138
+ var _a;
139
+ const users = await model.find()
147
140
  .where('name', '==', { firstName: 'userFirstName3' })
148
141
  .get();
149
- expect(users[0].id).toBe('user3');
150
- }));
151
- it('should find by property path', () => __awaiter(void 0, void 0, void 0, function* () {
152
- const users = yield model.find()
142
+ expect((_a = users[0]) === null || _a === void 0 ? void 0 : _a.id).toBe('user3');
143
+ });
144
+ it('should find by property path', async () => {
145
+ var _a;
146
+ const users = await model.find()
153
147
  .whereDeepProp('name.firstName', '==', 'userFirstName3')
154
148
  .get();
155
- expect(users[0].id).toBe('user3');
156
- }));
157
- it('should find by superdeep property path', () => __awaiter(void 0, void 0, void 0, function* () {
158
- const users = yield model.find()
149
+ expect((_a = users[0]) === null || _a === void 0 ? void 0 : _a.id).toBe('user3');
150
+ });
151
+ it('should find by superdeep property path', async () => {
152
+ var _a;
153
+ const users = await model.find()
159
154
  .whereDeepProp('name.ancestorName.father', '==', 'user3Father')
160
155
  .get();
161
- expect(users[0].id).toEqual('user3');
162
- }));
163
- it('should find by swallow property path', () => __awaiter(void 0, void 0, void 0, function* () {
164
- const users = yield model.find()
156
+ expect((_a = users[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user3');
157
+ });
158
+ it('should find by swallow property path', async () => {
159
+ var _a;
160
+ const users = await model.find()
165
161
  .whereDeepProp('age', '==', 21)
166
162
  .get();
167
- expect(users[0].id).toEqual('user2');
168
- }));
163
+ expect((_a = users[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user2');
164
+ });
169
165
  });
170
166
  describe('Derived classes should fit on parent collection', () => {
171
- it('should save derived object in parent collection', () => __awaiter(void 0, void 0, void 0, function* () {
167
+ it('should save derived object in parent collection', async () => {
172
168
  const derived = new test_user_1.DerivedUser();
173
169
  derived.name = { firstName: 'Fulanito', lastName: 'Derived' };
174
170
  derived.salary = 3900;
175
- yield model.save(derived);
176
- const newUser = yield model.findById(derived.id);
171
+ await model.save(derived);
172
+ const newUser = await model.findById(derived.id);
177
173
  expect(newUser).toBeInstanceOf(test_user_1.DerivedUser);
178
174
  expect(newUser.salary).toBe(3900);
179
175
  expect(newUser.className).toEqual('DerivedUser');
180
- }));
176
+ });
181
177
  });
182
178
  describe('References to documents', () => {
183
179
  let ref1, ref2;
184
- beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
180
+ beforeEach(async () => {
185
181
  testUser.documentRef = new test_user_1.SubClass();
186
182
  testUser.documentRef.year = 2045;
187
183
  ref1 = new test_user_1.SubClass();
@@ -195,141 +191,157 @@ describe('Firestore Model', () => {
195
191
  testUser.manyDerived = [new test_user_1.DerivedUser(), new test_user_1.DerivedUser()];
196
192
  testUser.manyDerived[0].salary = 990;
197
193
  testUser.manyDerived[1].salary = 1990;
198
- yield model.save(testUser);
199
- }));
200
- it('should save a document as a reference', () => __awaiter(void 0, void 0, void 0, function* () {
194
+ await model.save(testUser);
195
+ });
196
+ it('should save a document as a reference', async () => {
201
197
  const subClassModel = entropic_bond_1.Store.getModel('SubClass');
202
198
  expect(subClassModel).toBeDefined();
203
- const newDocument = yield subClassModel.findById(testUser.documentRef.id);
199
+ const newDocument = await subClassModel.findById(testUser.documentRef.id);
204
200
  expect(newDocument).toBeInstanceOf(test_user_1.SubClass);
205
201
  expect(newDocument.year).toBe(2045);
206
- }));
207
- it('should read a swallow document reference', () => __awaiter(void 0, void 0, void 0, function* () {
208
- const loadedUser = yield model.findById(testUser.id);
209
- expect(loadedUser.documentRef).toBeInstanceOf(test_user_1.SubClass);
210
- expect(loadedUser.documentRef.id).toBeDefined();
211
- expect(loadedUser.documentRef.year).toBeUndefined();
212
- }));
213
- it('should fill data of swallow document reference', () => __awaiter(void 0, void 0, void 0, function* () {
214
- const loadedUser = yield model.findById(testUser.id);
215
- yield entropic_bond_1.Store.populate(loadedUser.documentRef);
216
- expect(loadedUser.documentRef.id).toBeDefined();
217
- expect(loadedUser.documentRef.year).toBe(2045);
218
- }));
219
- it('should save and array of references', () => __awaiter(void 0, void 0, void 0, function* () {
202
+ });
203
+ it('should read a swallow document reference', async () => {
204
+ var _a, _b;
205
+ const loadedUser = await model.findById(testUser.id);
206
+ expect(loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.documentRef).toBeInstanceOf(test_user_1.SubClass);
207
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.documentRef) === null || _a === void 0 ? void 0 : _a.id).toBeDefined();
208
+ expect((_b = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.documentRef) === null || _b === void 0 ? void 0 : _b.year).toBeUndefined();
209
+ });
210
+ it('should fill data of swallow document reference', async () => {
211
+ var _a, _b;
212
+ const loadedUser = await model.findById(testUser.id);
213
+ await entropic_bond_1.Store.populate(loadedUser.documentRef);
214
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.documentRef) === null || _a === void 0 ? void 0 : _a.id).toBeDefined();
215
+ expect((_b = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.documentRef) === null || _b === void 0 ? void 0 : _b.year).toBe(2045);
216
+ });
217
+ it('should save and array of references', async () => {
220
218
  const subClassModel = entropic_bond_1.Store.getModel('SubClass');
221
- const newDocument = yield subClassModel.findById(testUser.documentRef.id);
219
+ const newDocument = await subClassModel.findById(testUser.documentRef.id);
222
220
  expect(newDocument).toBeInstanceOf(test_user_1.SubClass);
223
221
  expect(newDocument.year).toBe(2045);
224
- }));
225
- it('should read an array of references', () => __awaiter(void 0, void 0, void 0, function* () {
226
- const loadedUser = yield model.findById(testUser.id);
227
- expect(loadedUser.manyRefs).toHaveLength(2);
228
- expect(loadedUser.manyRefs[0]).toBeInstanceOf(test_user_1.SubClass);
229
- expect(loadedUser.manyRefs[0].id).toEqual(testUser.manyRefs[0].id);
230
- expect(loadedUser.manyRefs[0].year).toBeUndefined();
231
- expect(loadedUser.manyRefs[1]).toBeInstanceOf(test_user_1.SubClass);
232
- expect(loadedUser.manyRefs[1].id).toEqual(testUser.manyRefs[1].id);
233
- expect(loadedUser.manyRefs[1].year).toBeUndefined();
234
- }));
235
- it('should fill array of refs', () => __awaiter(void 0, void 0, void 0, function* () {
236
- const loadedUser = yield model.findById(testUser.id);
237
- yield entropic_bond_1.Store.populate(loadedUser.manyRefs);
238
- expect(loadedUser.manyRefs[0].year).toBe(2081);
239
- expect(loadedUser.manyRefs[1].year).toBe(2082);
240
- }));
241
- it('should save a reference when declared @persistentAt', () => __awaiter(void 0, void 0, void 0, function* () {
242
- const loadedUser = yield model.findById(testUser.id);
243
- expect(loadedUser.derived.id).toEqual(testUser.derived.id);
244
- expect(loadedUser.derived.salary).toBeUndefined();
245
- yield entropic_bond_1.Store.populate(loadedUser.derived);
246
- expect(loadedUser.derived.salary).toBe(1350);
247
- expect(loadedUser.derived.id).toBe(testUser.derived.id);
248
- }));
249
- it('should populate from special collection when declared with @persistentRefAt', () => __awaiter(void 0, void 0, void 0, function* () {
250
- const loadedUser = yield model.findById('user6');
251
- yield entropic_bond_1.Store.populate(loadedUser.derived);
252
- expect(loadedUser.derived.salary).toBe(2800);
253
- expect(loadedUser.derived.id).toBe('user4');
254
- }));
255
- it('should save a reference when declared @persistentAt as array', () => __awaiter(void 0, void 0, void 0, function* () {
256
- const loadedUser = yield model.findById(testUser.id);
257
- expect(loadedUser.manyDerived[0].id).toEqual(testUser.manyDerived[0].id);
258
- expect(loadedUser.manyDerived[0].salary).toBeUndefined();
259
- expect(loadedUser.manyDerived[1].salary).toBeUndefined();
260
- yield entropic_bond_1.Store.populate(loadedUser.manyDerived);
261
- expect(loadedUser.manyDerived[0].salary).toBe(990);
262
- expect(loadedUser.manyDerived[0].id).toBe(testUser.manyDerived[0].id);
263
- expect(loadedUser.manyDerived[1].salary).toBe(1990);
264
- expect(loadedUser.manyDerived[1].id).toBe(testUser.manyDerived[1].id);
265
- }));
266
- it('should not overwrite not filled ref in collection', () => __awaiter(void 0, void 0, void 0, function* () {
267
- const loadedUser = yield model.findById('user6');
268
- yield model.save(loadedUser);
269
- const refInCollection = yield model.findById('user4');
270
- expect(refInCollection.salary).toBe(2800);
271
- }));
272
- it('should save loaded ref with assigned new instance', () => __awaiter(void 0, void 0, void 0, function* () {
273
- const loadedUser = yield model.findById('user6');
222
+ });
223
+ it('should read an array of references', async () => {
224
+ var _a, _b, _c, _d, _e, _f;
225
+ const loadedUser = await model.findById(testUser.id);
226
+ expect(loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs).toHaveLength(2);
227
+ expect(loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[0]).toBeInstanceOf(test_user_1.SubClass);
228
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual((_b = testUser.manyRefs[0]) === null || _b === void 0 ? void 0 : _b.id);
229
+ expect((_c = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[0]) === null || _c === void 0 ? void 0 : _c.year).toBeUndefined();
230
+ expect(loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[1]).toBeInstanceOf(test_user_1.SubClass);
231
+ expect((_d = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[1]) === null || _d === void 0 ? void 0 : _d.id).toEqual((_e = testUser.manyRefs[1]) === null || _e === void 0 ? void 0 : _e.id);
232
+ expect((_f = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[1]) === null || _f === void 0 ? void 0 : _f.year).toBeUndefined();
233
+ });
234
+ it('should fill array of refs', async () => {
235
+ var _a, _b;
236
+ const loadedUser = await model.findById(testUser.id);
237
+ await entropic_bond_1.Store.populate(loadedUser.manyRefs);
238
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[0]) === null || _a === void 0 ? void 0 : _a.year).toBe(2081);
239
+ expect((_b = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyRefs[1]) === null || _b === void 0 ? void 0 : _b.year).toBe(2082);
240
+ });
241
+ it('should save a reference when declared @persistentAt', async () => {
242
+ var _a, _b, _c, _d, _e, _f;
243
+ const loadedUser = await model.findById(testUser.id);
244
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _a === void 0 ? void 0 : _a.id).toEqual((_b = testUser.derived) === null || _b === void 0 ? void 0 : _b.id);
245
+ expect((_c = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _c === void 0 ? void 0 : _c.salary).toBeUndefined();
246
+ await entropic_bond_1.Store.populate(loadedUser.derived);
247
+ expect((_d = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _d === void 0 ? void 0 : _d.salary).toBe(1350);
248
+ expect((_e = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _e === void 0 ? void 0 : _e.id).toBe((_f = testUser.derived) === null || _f === void 0 ? void 0 : _f.id);
249
+ });
250
+ it('should populate from special collection when declared with @persistentRefAt', async () => {
251
+ var _a, _b;
252
+ const loadedUser = await model.findById('user6');
253
+ await entropic_bond_1.Store.populate(loadedUser.derived);
254
+ expect((_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _a === void 0 ? void 0 : _a.salary).toBe(2800);
255
+ expect((_b = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.derived) === null || _b === void 0 ? void 0 : _b.id).toBe('user4');
256
+ });
257
+ it('should save a reference when declared @persistentAt as array', async () => {
258
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
259
+ const loadedUser = await model.findById(testUser.id);
260
+ expect((_b = (_a = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.id).toEqual((_d = (_c = testUser.manyDerived) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.id);
261
+ expect((_f = (_e = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _e === void 0 ? void 0 : _e[0]) === null || _f === void 0 ? void 0 : _f.salary).toBeUndefined();
262
+ expect((_h = (_g = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _g === void 0 ? void 0 : _g[1]) === null || _h === void 0 ? void 0 : _h.salary).toBeUndefined();
263
+ await entropic_bond_1.Store.populate(loadedUser.manyDerived);
264
+ expect((_k = (_j = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _j === void 0 ? void 0 : _j[0]) === null || _k === void 0 ? void 0 : _k.salary).toBe(990);
265
+ expect((_m = (_l = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _l === void 0 ? void 0 : _l[0]) === null || _m === void 0 ? void 0 : _m.id).toBe((_p = (_o = testUser.manyDerived) === null || _o === void 0 ? void 0 : _o[0]) === null || _p === void 0 ? void 0 : _p.id);
266
+ expect((_r = (_q = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _q === void 0 ? void 0 : _q[1]) === null || _r === void 0 ? void 0 : _r.salary).toBe(1990);
267
+ expect((_t = (_s = loadedUser === null || loadedUser === void 0 ? void 0 : loadedUser.manyDerived) === null || _s === void 0 ? void 0 : _s[1]) === null || _t === void 0 ? void 0 : _t.id).toBe((_v = (_u = testUser.manyDerived) === null || _u === void 0 ? void 0 : _u[1]) === null || _v === void 0 ? void 0 : _v.id);
268
+ });
269
+ it('should not overwrite not filled ref in collection', async () => {
270
+ const loadedUser = await model.findById('user6');
271
+ await model.save(loadedUser);
272
+ const refInCollection = await model.findById('user4');
273
+ expect(refInCollection === null || refInCollection === void 0 ? void 0 : refInCollection.salary).toBe(2800);
274
+ });
275
+ it('should save loaded ref with assigned new instance', async () => {
276
+ var _a;
277
+ const loadedUser = await model.findById('user6');
274
278
  loadedUser.derived = new test_user_1.DerivedUser();
275
279
  loadedUser.derived.salary = 345;
276
- yield model.save(loadedUser);
277
- const refInCollection = yield model.findById(loadedUser.derived.id);
278
- expect(refInCollection.salary).toBe(345);
279
- }));
280
- it('should save loaded ref with modified ref data', () => __awaiter(void 0, void 0, void 0, function* () {
281
- const loadedUser = yield model.findById('user6');
282
- yield entropic_bond_1.Store.populate(loadedUser.derived);
280
+ await model.save(loadedUser);
281
+ const refInCollection = await model.findById((_a = loadedUser.derived) === null || _a === void 0 ? void 0 : _a.id);
282
+ expect(refInCollection === null || refInCollection === void 0 ? void 0 : refInCollection.salary).toBe(345);
283
+ });
284
+ it('should save loaded ref with modified ref data', async () => {
285
+ const loadedUser = await model.findById('user6');
286
+ await entropic_bond_1.Store.populate(loadedUser.derived);
283
287
  loadedUser.derived.salary = 1623;
284
- yield model.save(loadedUser);
285
- const refInCollection = yield model.findById('user4');
286
- expect(refInCollection.salary).toBe(1623);
287
- }));
288
+ await model.save(loadedUser);
289
+ const refInCollection = await model.findById('user4');
290
+ expect(refInCollection === null || refInCollection === void 0 ? void 0 : refInCollection.salary).toBe(1623);
291
+ });
288
292
  });
289
293
  describe('Operations on queries', () => {
290
- it('should limit the result set', () => __awaiter(void 0, void 0, void 0, function* () {
291
- const unlimited = yield model.find().get();
292
- const limited = yield model.find().limit(2).get();
294
+ it('should limit the result set', async () => {
295
+ const unlimited = await model.find().get();
296
+ const limited = await model.find().limit(2).get();
293
297
  expect(unlimited.length).not.toBe(limited.length);
294
298
  expect(limited).toHaveLength(2);
295
- }));
296
- it('should sort ascending the result set', () => __awaiter(void 0, void 0, void 0, function* () {
297
- const docs = yield model.find().orderBy('age').get();
298
- expect(docs[0].id).toEqual('user2');
299
- expect(docs[1].id).toEqual('user1');
300
- }));
301
- it('should sort descending the result set', () => __awaiter(void 0, void 0, void 0, function* () {
302
- const docs = yield model.find().orderBy('age', 'desc').get();
303
- expect(docs[0].id).toEqual('user3');
304
- expect(docs[1].id).toEqual('user5');
305
- }));
306
- it('should sort by deep property path', () => __awaiter(void 0, void 0, void 0, function* () {
307
- const docs = yield model.find().orderByDeepProp('name.firstName', 'desc').get();
308
- expect(docs[0].id).toEqual('user6');
309
- expect(docs[1].id).toEqual('user5');
310
- }));
311
- it('should sort by swallow property path', () => __awaiter(void 0, void 0, void 0, function* () {
312
- const docs = yield model.find().orderByDeepProp('age').get();
313
- expect(docs[0].id).toEqual('user2');
314
- expect(docs[1].id).toEqual('user1');
315
- }));
299
+ });
300
+ it('should sort ascending the result set', async () => {
301
+ var _a, _b;
302
+ const docs = await model.find().orderBy('age').get();
303
+ expect((_a = docs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user2');
304
+ expect((_b = docs[1]) === null || _b === void 0 ? void 0 : _b.id).toEqual('user1');
305
+ });
306
+ it('should sort descending the result set', async () => {
307
+ var _a, _b;
308
+ const docs = await model.find().orderBy('age', 'desc').get();
309
+ expect((_a = docs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user3');
310
+ expect((_b = docs[1]) === null || _b === void 0 ? void 0 : _b.id).toEqual('user5');
311
+ });
312
+ it('should sort by deep property path', async () => {
313
+ var _a, _b;
314
+ const docs = await model.find().orderByDeepProp('name.firstName', 'desc').get();
315
+ expect((_a = docs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user6');
316
+ expect((_b = docs[1]) === null || _b === void 0 ? void 0 : _b.id).toEqual('user5');
317
+ });
318
+ it('should sort by swallow property path', async () => {
319
+ var _a, _b;
320
+ const docs = await model.find().orderByDeepProp('age').get();
321
+ expect((_a = docs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual('user2');
322
+ expect((_b = docs[1]) === null || _b === void 0 ? void 0 : _b.id).toEqual('user1');
323
+ });
324
+ it('should count documents in collection', async () => {
325
+ expect(await model.find().count()).toBe(6);
326
+ });
316
327
  describe('Data Cursors', () => {
317
- beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
318
- yield model.find().get(2);
319
- }));
320
- it('should get next result set', () => __awaiter(void 0, void 0, void 0, function* () {
321
- const docs = yield model.next();
328
+ beforeEach(async () => {
329
+ await model.find().get(2);
330
+ });
331
+ it('should get next result set', async () => {
332
+ var _a, _b, _c;
333
+ const docs = await model.next();
322
334
  const mockDataArr = Object.values(mock_data_json_1.default.TestUser);
323
335
  expect(docs).toHaveLength(2);
324
- expect(docs[0].id).toEqual(mockDataArr[2].id);
325
- expect(docs[0].id).toEqual('user3');
326
- }));
327
- it('should not go beyond the end of result set', () => __awaiter(void 0, void 0, void 0, function* () {
328
- yield model.next();
329
- yield model.next();
330
- const docs = yield model.next();
336
+ expect((_a = docs[0]) === null || _a === void 0 ? void 0 : _a.id).toEqual((_b = mockDataArr[2]) === null || _b === void 0 ? void 0 : _b.id);
337
+ expect((_c = docs[0]) === null || _c === void 0 ? void 0 : _c.id).toEqual('user3');
338
+ });
339
+ it('should not go beyond the end of result set', async () => {
340
+ await model.next();
341
+ await model.next();
342
+ const docs = await model.next();
331
343
  expect(docs).toHaveLength(0);
332
- }));
344
+ });
333
345
  });
334
346
  });
335
347
  describe('SubCollections', () => {
@@ -337,13 +349,13 @@ describe('Firestore Model', () => {
337
349
  beforeEach(() => {
338
350
  model = entropic_bond_1.Store.getModelForSubCollection(testUser, 'SubClass');
339
351
  });
340
- it('should retrieve from subcollection', () => __awaiter(void 0, void 0, void 0, function* () {
352
+ it('should retrieve from subcollection', async () => {
341
353
  const subClass = new test_user_1.SubClass();
342
354
  subClass.year = 3452;
343
- yield model.save(subClass);
344
- const loaded = yield model.findById(subClass.id);
345
- expect(loaded.year).toBe(3452);
346
- }));
355
+ await model.save(subClass);
356
+ const loaded = await model.findById(subClass.id);
357
+ expect(loaded === null || loaded === void 0 ? void 0 : loaded.year).toBe(3452);
358
+ });
347
359
  });
348
360
  });
349
361
  //# sourceMappingURL=firebase-datasource.spec.js.map