|
| 1 | +import * as firebase from 'firebase/app'; |
| 2 | +import { FirebaseApp, FirebaseAppConfig, AngularFireModule } from 'angularfire2'; |
| 3 | +import { AngularFireDatabase, AngularFireDatabaseModule, createListSnapshotChanges, ChildEvent } from 'angularfire2/database'; |
| 4 | +import { TestBed, inject } from '@angular/core/testing'; |
| 5 | +import { COMMON_CONFIG } from '../test-config'; |
| 6 | +import 'rxjs/add/operator/skip'; |
| 7 | + |
| 8 | +// generate random string to test fidelity of naming |
| 9 | +const rando = () => (Math.random() + 1).toString(36).substring(7); |
| 10 | +const FIREBASE_APP_NAME = rando(); |
| 11 | + |
| 12 | +describe('snapshotChanges', () => { |
| 13 | + let app: FirebaseApp; |
| 14 | + let db: AngularFireDatabase; |
| 15 | + let createRef: (path: string) => firebase.database.Reference; |
| 16 | + let batch = {}; |
| 17 | + const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ( { key: i.toString(), ...item } )); |
| 18 | + Object.keys(items).forEach(function (key, i) { |
| 19 | + const itemValue = items[key]; |
| 20 | + batch[i] = itemValue; |
| 21 | + }); |
| 22 | + // make batch immutable to preserve integrity |
| 23 | + batch = Object.freeze(batch); |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + TestBed.configureTestingModule({ |
| 27 | + imports: [ |
| 28 | + AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME), |
| 29 | + AngularFireDatabaseModule |
| 30 | + ] |
| 31 | + }); |
| 32 | + inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => { |
| 33 | + app = app_; |
| 34 | + db = _db; |
| 35 | + app.database().goOffline(); |
| 36 | + createRef = (path: string) => { app.database().goOffline(); return app.database().ref(path); }; |
| 37 | + })(); |
| 38 | + }); |
| 39 | + |
| 40 | + afterEach(done => { |
| 41 | + app.delete().then(done, done.fail); |
| 42 | + }); |
| 43 | + |
| 44 | + function prepareSnapshotChanges(opts: { events?: ChildEvent[], skip: number } = { skip: 0 }) { |
| 45 | + const { events, skip } = opts; |
| 46 | + const aref = createRef(rando()); |
| 47 | + aref.set(batch); |
| 48 | + const snapshotChanges = createListSnapshotChanges(aref); |
| 49 | + return { |
| 50 | + snapshotChanges: snapshotChanges(events).skip(skip), |
| 51 | + ref: aref |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + it('should listen to all events by default', (done) => { |
| 56 | + const { snapshotChanges } = prepareSnapshotChanges({ skip: 2 }); |
| 57 | + const sub = snapshotChanges.subscribe(snaps => { |
| 58 | + const data = snaps.map(snap => snap.val()) |
| 59 | + expect(data).toEqual(items); |
| 60 | + done(); |
| 61 | + sub.unsubscribe(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should listen to only child_added events', (done) => { |
| 66 | + const { snapshotChanges } = prepareSnapshotChanges({ events: ['child_added'], skip: 2 }); |
| 67 | + const sub = snapshotChanges.subscribe(snaps => { |
| 68 | + const data = snaps.map(snap => snap.val()) |
| 69 | + expect(data).toEqual(items); |
| 70 | + done(); |
| 71 | + sub.unsubscribe(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should listen to only child_added, child_changed events', (done) => { |
| 76 | + const {snapshotChanges, ref } = prepareSnapshotChanges({ |
| 77 | + events: ['child_added', 'child_changed'], |
| 78 | + skip: 3 |
| 79 | + }); |
| 80 | + const name = 'ligatures'; |
| 81 | + const sub = snapshotChanges.subscribe(snaps => { |
| 82 | + const data = snaps.map(snap => snap.val()); |
| 83 | + const copy = [...items]; |
| 84 | + copy[0].name = name; |
| 85 | + expect(data).toEqual(copy); |
| 86 | + done(); |
| 87 | + sub.unsubscribe(); |
| 88 | + }); |
| 89 | + ref.child(items[0].key).update({ name }); |
| 90 | + }); |
| 91 | + |
| 92 | +}); |
0 commit comments