-
Notifications
You must be signed in to change notification settings - Fork 76
/
BaseFirestoreRepository.spec.ts
716 lines (573 loc) · 25.1 KB
/
BaseFirestoreRepository.spec.ts
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
import { initialize } from './MetadataStorage';
import { getFixture, Album, Coordinates, FirestoreDocumentReference } from '../test/fixture';
import { BaseFirestoreRepository } from './BaseFirestoreRepository';
import { Band } from '../test/BandCollection';
import { Firestore } from '@google-cloud/firestore';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const MockFirebase = require('mock-cloud-firestore');
describe('BaseFirestoreRepository', () => {
class BandRepository extends BaseFirestoreRepository<Band> {}
let bandRepository: BaseFirestoreRepository<Band> = null;
let firestore: Firestore = null;
beforeEach(() => {
const fixture = Object.assign({}, getFixture());
const firebase = new MockFirebase(fixture, {
isNaiveSnapshotListenerEnabled: false,
});
firestore = firebase.firestore();
initialize(firestore);
bandRepository = new BandRepository('bands');
});
describe('limit', () => {
it('must limit the documents in a collection', async () => {
const twoBands = await bandRepository.limit(2).find();
expect(twoBands.length).toEqual(2);
});
it('must limit the results of a query', async () => {
const eightiesBands = await bandRepository
.whereGreaterOrEqualThan('formationYear', 1980)
.limit(1)
.find();
expect(eightiesBands.length).toEqual(1);
});
it('must not throw any exceptions if a query with no results is limited', async () => {
const oldBands = await bandRepository
.whereLessOrEqualThan('formationYear', 1930)
.limit(4)
.find();
expect(oldBands.length).toEqual(0);
});
it('must limit subcollections', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
const albumsLimited = await albumsSubColl.limit(2).find();
expect(albumsLimited.length).toEqual(2);
});
it('must throw an exception if limit call more than once', async () => {
expect(() => bandRepository.limit(2).limit(2).find()).toThrow();
});
it.todo('must return if limit is 0');
it.todo('must throw if the limit is less than 0');
});
describe('Ordering', () => {
describe('orderByAscending', () => {
it('must order repository objects', async () => {
const bands = await bandRepository.orderByAscending('formationYear').find();
expect(bands[0].id).toEqual('pink-floyd');
});
it('must order the objects in a subcollection', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
const discographyNewestFirst = await albumsSubColl.orderByAscending('releaseDate').find();
expect(discographyNewestFirst[0].id).toEqual('lightbulb-sun');
});
it('must be chainable with where* filters', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
const discographyNewestFirst = await albumsSubColl
.whereGreaterOrEqualThan('releaseDate', new Date('2001-01-01'))
.orderByAscending('releaseDate')
.find();
expect(discographyNewestFirst[0].id).toEqual('in-absentia');
});
it('must be chainable with limit', async () => {
const bands = await bandRepository.orderByAscending('formationYear').limit(2).find();
const lastBand = bands[bands.length - 1];
expect(lastBand.id).toEqual('red-hot-chili-peppers');
});
it('must throw an Error if an orderBy* function is called more than once in the same expression', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});
});
describe('orderByDescending', () => {
it('must order repository objects', async () => {
const bands = await bandRepository.orderByDescending('formationYear').find();
expect(bands[0].id).toEqual('porcupine-tree');
});
it('must order the objects in a subcollection', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
const discographyNewestFirst = await albumsSubColl.orderByDescending('releaseDate').find();
expect(discographyNewestFirst[0].id).toEqual('fear-blank-planet');
});
it('must be chainable with where* filters', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
const discographyNewestFirst = await albumsSubColl
.whereGreaterOrEqualThan('releaseDate', new Date('2001-01-01'))
.orderByDescending('releaseDate')
.find();
expect(discographyNewestFirst[0].id).toEqual('fear-blank-planet');
});
it('must be chainable with limit', async () => {
const bands = await bandRepository.orderByDescending('formationYear').limit(2).find();
const lastBand = bands[bands.length - 1];
expect(lastBand.id).toEqual('red-hot-chili-peppers');
});
it('must throw an Error if an orderBy* function is called more than once in the same expression', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const albumsSubColl = pt.albums;
expect(() => {
albumsSubColl.orderByAscending('releaseDate').orderByDescending('releaseDate');
}).toThrow();
});
});
});
describe('findById', () => {
it('must find by id', async () => {
const pt = await bandRepository.findById('porcupine-tree');
expect(pt).toBeInstanceOf(Band);
expect(pt.id).toEqual('porcupine-tree');
expect(pt.name).toEqual('Porcupine Tree');
});
it('must have proper getters', async () => {
const pt = await bandRepository.findById('porcupine-tree');
expect(pt.getLastShowYear()).toEqual(2010);
});
it('return null if not found', async () => {
const sw = await bandRepository.findById('steven-wilson');
expect(sw).toBeNull();
});
});
describe('create', () => {
it('should return T when an item is created', async () => {
const entity = new Band();
entity.id = 'rush';
entity.name = 'Rush';
entity.formationYear = 1968;
entity.genres = ['progressive-rock', 'hard-rock', 'heavy-metal'];
const band = await bandRepository.create(entity);
expect(band).toBeInstanceOf(Band);
expect(band.getPopularGenre()).toEqual('progressive-rock');
});
it('must not validate if the validate config by default', async () => {
initialize(firestore);
bandRepository = new BandRepository('bands');
const entity = new Band();
entity.contactEmail = 'Not an email';
const band = await bandRepository.create(entity);
expect(band.contactEmail).toEqual('Not an email');
});
it('must not validate if the validateModels: false', async () => {
initialize(firestore, { validateModels: false });
bandRepository = new BandRepository('bands');
const entity = new Band();
entity.contactEmail = 'Not an email';
const band = await bandRepository.create(entity);
expect(band.contactEmail).toEqual('Not an email');
});
it('must fail validation if an invalid class is given', async () => {
initialize(firestore, { validateModels: true });
const entity = new Band();
entity.contactEmail = 'Not an email';
try {
await bandRepository.create(entity);
} catch (error) {
expect(error[0].constraints.isEmail).toEqual('Invalid email!');
}
});
it('must fail validation if an invalid object is given', async () => {
initialize(firestore, { validateModels: true });
const entity: Partial<Band> = {
contactEmail: 'Not an email',
id: '1234',
};
try {
await bandRepository.create(entity as Band);
} catch (error) {
expect(error[0].constraints.isEmail).toEqual('Invalid email!');
}
});
it('must create items when id is passed', async () => {
const entity = new Band();
entity.id = 'perfect-circle';
entity.name = 'A Perfect Circle';
entity.formationYear = 1999;
entity.genres = ['alternative-rock', 'alternative-metal', 'hard-rock'];
const band = await bandRepository.create(entity);
expect(band.id).toEqual(entity.id);
expect(band.name).toEqual(entity.name);
expect(band.formationYear).toEqual(entity.formationYear);
expect(band.genres).toEqual(entity.genres);
});
it('must create items and assign a custom id if no id is passed', async () => {
const entity = new Band();
entity.name = 'The Pinapple Thief';
entity.formationYear = 1999;
entity.genres = ['progressive-rock'];
const band = await bandRepository.create(entity);
expect(typeof band.id).toEqual('string');
expect(band.id).not.toBeUndefined();
expect(band.name).toEqual(entity.name);
expect(band.formationYear).toEqual(entity.formationYear);
expect(band.genres).toEqual(entity.genres);
});
it('must save autogenerated id field in document if no id is passed', async () => {
const entity = new Band();
entity.name = 'Deftones';
entity.formationYear = 1988;
entity.genres = ['alternative-metal'];
const band = await bandRepository.create(entity);
const foundBand = await bandRepository.findById(band.id);
expect(band.id).toEqual(foundBand.id);
});
});
describe('update', () => {
it('must update and return updated item', async () => {
const band = await bandRepository.findById('porcupine-tree');
const albums = band.albums;
band.name = 'Steven Wilson';
const updatedBand = await bandRepository.update(band);
expect(band.name).toEqual(updatedBand.name);
// should not mutate other fields or relations on updated item
expect(band.albums).toEqual(albums);
});
it('must not validate if the validate config property is false', async () => {
initialize(firestore, { validateModels: false });
bandRepository = new BandRepository('bands');
const band = await bandRepository.findById('porcupine-tree');
band.contactEmail = 'Not an email';
await bandRepository.update(band);
const updatedBand = await bandRepository.findById('porcupine-tree');
expect(updatedBand.contactEmail).toEqual('Not an email');
});
it('must fail validation if an invalid class is given', async () => {
initialize(firestore, { validateModels: true });
const band = await bandRepository.findById('porcupine-tree');
band.contactEmail = 'Not an email';
try {
await bandRepository.update(band);
} catch (error) {
expect(error[0].constraints.isEmail).toEqual('Invalid email!');
}
});
it('must fail validation if an invalid object is given', async () => {
initialize(firestore, { validateModels: true });
const band = await bandRepository.findById('porcupine-tree');
band.contactEmail = 'Not an Email';
try {
await bandRepository.update(band);
} catch (error) {
expect(error[0].constraints.isEmail).toEqual('Invalid email!');
}
});
it.todo('must only update changed fields');
it.todo('must throw if item is not found');
});
describe('delete', () => {
it('must delete item', async () => {
await bandRepository.delete('porcupine-tree');
const roy = await bandRepository.findById('porcupine-tree');
expect(roy).toBeNull();
});
// mock-cloud-firestore won't throw here
it.skip('must throw if item is not found', async () => {
expect(async () => await bandRepository.delete('lol')).toThrow();
});
});
describe('.where*', () => {
it('whereEqualTo must accept function as first parameter', async () => {
const list = await bandRepository.whereEqualTo(b => b.name, 'Porcupine Tree').find();
expect(list.length).toEqual(1);
expect(list[0].name).toEqual('Porcupine Tree');
});
it('must return T[]', async () => {
const progressiveRockBands = await bandRepository
.whereArrayContains('genres', 'progressive-rock')
.find();
progressiveRockBands.forEach(b => {
expect(b.getPopularGenre()).toEqual(b.genres[0]);
});
});
it("must return same list if where filter doesn't apply", async () => {
const list = await bandRepository.whereGreaterOrEqualThan('formationYear', 1983).find();
expect(list.length).toEqual(2);
});
it('must filter with whereEqualTo', async () => {
const list = await bandRepository.whereEqualTo('name', 'Porcupine Tree').find();
expect(list.length).toEqual(1);
expect(list[0].name).toEqual('Porcupine Tree');
});
it('must filter with whereGreaterThan', async () => {
const list = await bandRepository.whereGreaterThan('formationYear', 1983).find();
expect(list.length).toEqual(1);
});
it('must filter with whereGreaterOrEqualThan', async () => {
const list = await bandRepository.whereGreaterOrEqualThan('formationYear', 1983).find();
expect(list.length).toEqual(2);
});
it('must filter with whereLessThan', async () => {
const list = await bandRepository.whereLessThan('formationYear', 1983).find();
expect(list.length).toEqual(1);
});
it('must filter with whereLessOrEqualThan', async () => {
const list = await bandRepository.whereLessOrEqualThan('formationYear', 1983).find();
expect(list.length).toEqual(2);
});
it('must filter with whereArrayContains', async () => {
const list = await bandRepository.whereArrayContains('genres', 'progressive-rock').find();
expect(list.length).toEqual(2);
});
it('must filter with whereArrayContainsAny', async () => {
const list = await bandRepository
.whereArrayContainsAny('genres', ['psychedelic-rock', 'funk-rock'])
.find();
expect(list.length).toEqual(3);
});
it('must filter with whereIn', async () => {
const list = await bandRepository.whereIn('formationYear', [1965, 1983, 1987]).find();
expect(list.length).toEqual(3);
});
it('should throw with whereArrayContainsAny and more than 10 items in val array', async () => {
expect(async () => {
await bandRepository
.whereArrayContainsAny('genres', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11])
.find();
}).rejects.toThrow(Error);
});
it('should throw with whereIn and more than 10 items in val array', async () => {
expect(async () => {
await bandRepository.whereIn('formationYear', [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]).find();
}).rejects.toThrow(Error);
});
it('must filter with two or more operators', async () => {
const list = await bandRepository
.whereLessOrEqualThan('formationYear', 1983)
.whereArrayContains('genres', 'funk-rock')
.find();
expect(list.length).toEqual(1);
expect(list[0].id).toEqual('red-hot-chili-peppers');
});
it('must support document references in where methods', async () => {
const docRef = firestore.collection('bands').doc('steven-wilson');
const band = await bandRepository.findById('porcupine-tree');
band.relatedBand = docRef;
await bandRepository.update(band);
const byReference = await bandRepository.whereEqualTo(b => b.relatedBand, docRef).find();
expect(byReference.length).toEqual(1);
expect(byReference[0].name).toEqual('Porcupine Tree');
});
});
describe('findOne', () => {
it('must return T', async () => {
const result = await bandRepository
.whereLessOrEqualThan('formationYear', 1983)
.whereArrayContains('genres', 'funk-rock')
.findOne();
expect(result).toBeInstanceOf(Band);
expect(result.id).toEqual('red-hot-chili-peppers');
});
it('must return null if not found', async () => {
const result = await bandRepository.whereLessThan('formationYear', 0).findOne();
expect(result).toBeNull();
});
it('should work within transactions', async () => {
await bandRepository.runTransaction(async tran => {
const result = await tran.whereLessThan('formationYear', 0).findOne();
expect(result).toBeNull();
});
});
});
describe('miscellaneous', () => {
it('should correctly parse dates', async () => {
const pt = await bandRepository.findById('porcupine-tree');
expect(pt.lastShow).toBeInstanceOf(Date);
expect(pt.lastShow.toISOString()).toEqual('2010-10-14T00:00:00.000Z');
});
it('should correctly parse geopoints', async () => {
const pt = await bandRepository.findById('porcupine-tree');
expect(pt.lastShowCoordinates).toBeInstanceOf(Coordinates);
expect(pt.lastShowCoordinates.latitude).toEqual(51.5009088);
expect(pt.lastShowCoordinates.longitude).toEqual(-0.1795547);
});
it('should correctly parse references', async () => {
const docRef = firestore.collection('bands').doc('opeth');
const band = await bandRepository.findById('porcupine-tree');
band.relatedBand = docRef;
await bandRepository.update(band);
const foundBand = await bandRepository.findById('porcupine-tree');
expect(foundBand.relatedBand).toBeInstanceOf(FirestoreDocumentReference);
expect(foundBand.relatedBand.id).toEqual('opeth');
// firestore mock doesn't set this property, it should be bands/opeth
expect(foundBand.relatedBand.path).toEqual(undefined);
});
});
describe('transactions', () => {
it('should be able to open transactions', async () => {
await bandRepository.runTransaction(async tran => {
const band = await tran.findById('porcupine-tree');
band.name = 'Árbol de Puercoespín';
await tran.update(band);
});
const updated = await bandRepository.findById('porcupine-tree');
expect(updated.name).toEqual('Árbol de Puercoespín');
});
it('should return TransactionRepository', async () => {
await bandRepository.runTransaction(async tran => {
expect(tran.constructor.name).toEqual('TransactionRepository');
});
});
});
describe('batch', () => {
it('should be able to create batches from repository', async () => {
const batch = bandRepository.createBatch();
const entity1 = new Band();
entity1.id = 'entity1';
entity1.name = 'Entity1';
entity1.formationYear = 2099;
const entity2 = new Band();
entity2.id = 'entity2';
entity2.name = 'Entity2';
entity2.formationYear = 2099;
const entity3 = new Band();
entity3.id = 'entity3';
entity3.name = 'Entity3';
entity3.formationYear = 2099;
batch.create(entity1);
batch.create(entity2);
batch.create(entity3);
await batch.commit();
const batchedBands = await bandRepository.whereEqualTo('formationYear', 2099).find();
expect(batchedBands.map(b => b.name)).toEqual(['Entity1', 'Entity2', 'Entity3']);
});
});
describe('must handle subcollections', () => {
it('should initialize subcollections', async () => {
const pt = await bandRepository.findById('porcupine-tree');
expect(pt.name).toEqual('Porcupine Tree');
expect(pt.albums).toBeInstanceOf(BaseFirestoreRepository);
});
it('should initialize nested subcollections', async () => {
const pt = await bandRepository.findById('red-hot-chili-peppers');
const album = await pt.albums.findById('stadium-arcadium');
expect(album.images).toBeInstanceOf(BaseFirestoreRepository);
});
it('should be able to execute operations in the subcollection', async () => {
const band = await bandRepository.findById('red-hot-chili-peppers');
const bestAlbum = await band.albums.findById('stadium-arcadium');
expect(bestAlbum.id).toEqual('stadium-arcadium');
});
it('should be able to create subcollections', async () => {
const band = new Band();
band.id = '30-seconds-to-mars';
band.name = '30 Seconds To Mars';
band.formationYear = 1998;
band.genres = ['alternative-rock'];
await bandRepository.create(band);
const firstAlbum = new Album();
firstAlbum.id = '30-seconds-to-mars';
firstAlbum.name = '30 Seconds to Mars';
firstAlbum.releaseDate = new Date('2002-07-22');
const secondAlbum = new Album();
secondAlbum.id = 'a-beautiful-lie';
secondAlbum.name = 'A Beautiful Lie';
secondAlbum.releaseDate = new Date('2005-07-30');
const thirdAlbum = new Album();
thirdAlbum.id = 'this-is-war';
thirdAlbum.name = 'This Is War';
thirdAlbum.releaseDate = new Date('2009-12-08');
await band.albums.create(firstAlbum);
await band.albums.create(secondAlbum);
await band.albums.create(thirdAlbum);
const albums = await band.albums.find();
expect(albums.length).toEqual(3);
});
it('should initialize nested subcollections on create', async () => {
const band = new Band();
band.id = '30-seconds-to-mars';
band.name = '30 Seconds To Mars';
band.formationYear = 1998;
band.genres = ['alternative-rock'];
await bandRepository.create(band);
const firstAlbum = new Album();
firstAlbum.id = '30-seconds-to-mars';
firstAlbum.name = '30 Seconds to Mars';
firstAlbum.releaseDate = new Date('2002-07-22');
const album = await band.albums.create(firstAlbum);
expect(album.images).toBeInstanceOf(BaseFirestoreRepository);
});
it('should be able to validate subcollections on create', async () => {
initialize(firestore, { validateModels: true });
const band = new Band();
band.id = '30-seconds-to-mars';
band.name = '30 Seconds To Mars';
band.formationYear = 1998;
band.genres = ['alternative-rock'];
await bandRepository.create(band);
const firstAlbum = new Album();
firstAlbum.id = 'invalid-album-name';
firstAlbum.name = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
firstAlbum.releaseDate = new Date('2002-07-22');
try {
await band.albums.create(firstAlbum);
} catch (error) {
expect(error[0].constraints.length).toEqual('Name is too long');
}
});
it('should be able to update subcollections', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const album = await pt.albums.findById('fear-blank-planet');
album.comment = 'Anesthethize is top 3 IMHO';
await pt.albums.update(album);
const updatedAlbum = await pt.albums.findById('fear-blank-planet');
expect(updatedAlbum.comment).toEqual('Anesthethize is top 3 IMHO');
});
it('should be able to validate subcollections on update', async () => {
initialize(firestore, { validateModels: true });
const pt = await bandRepository.findById('porcupine-tree');
const album = await pt.albums.findById('fear-blank-planet');
album.name = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
try {
await pt.albums.update(album);
} catch (error) {
expect(error[0].constraints.length).toEqual('Name is too long');
}
});
it('should be able to update collections with subcollections', async () => {
const pt = await bandRepository.findById('porcupine-tree');
pt.name = 'Porcupine Tree IS THE BEST';
const updatedPt = await bandRepository.update(pt);
const foundUpdatedPt = await bandRepository.update(pt);
expect(updatedPt.name).toEqual(pt.name);
expect(foundUpdatedPt.name).toEqual(pt.name);
});
it('should be able to delete subcollections', async () => {
const pt = await bandRepository.findById('porcupine-tree');
await pt.albums.delete('fear-blank-planet');
const updatedBandAlbums = await pt.albums.find();
expect(updatedBandAlbums.length).toEqual(3);
});
describe('miscellaneous', () => {
it('should correctly parse dates', async () => {
const pt = await bandRepository.findById('porcupine-tree');
const { releaseDate } = await pt.albums.findById('deadwing');
expect(releaseDate).toBeInstanceOf(Date);
expect(releaseDate.toISOString()).toEqual('2005-03-25T00:00:00.000Z');
});
});
});
describe('fetching documents created w/o id inside object', () => {
let docId: string = null;
beforeEach(async () => {
const bandWithoutId = new Band();
docId = (await firestore.collection('bands').add(bandWithoutId)).id;
});
it('Get by id - entity should contain id', async () => {
const band = await bandRepository.findById(docId);
expect(band).toHaveProperty('id');
expect(band.id).toEqual(docId);
});
it('Get list - all entities should contain id', async () => {
const bands = await bandRepository.find();
for (const b of bands) {
expect(b.id).not.toBeUndefined();
}
const possibleDocWithoutId = bands.find(band => band.id === docId);
expect(possibleDocWithoutId).not.toBeUndefined();
});
});
});