Skip to content

Commit 7a36dde

Browse files
committed
refactor(db): snapshot changes tests
1 parent e337f56 commit 7a36dde

File tree

7 files changed

+110
-3
lines changed

7 files changed

+110
-3
lines changed

src/database/database.spec.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { COMMON_CONFIG } from './test-config';
77
// generate random string to test fidelity of naming
88
const FIREBASE_APP_NAME = (Math.random() + 1).toString(36).substring(7);
99

10-
describe('AngularFireDatabase', () => {
10+
fdescribe('AngularFireDatabase', () => {
1111
let app: FirebaseApp;
1212
let db: AngularFireDatabase;
1313

@@ -45,6 +45,7 @@ describe('AngularFireDatabase', () => {
4545

4646
it('should have an initialized Firebase database instance member', () => {
4747
expect(db.database.app.name).toEqual(FIREBASE_APP_NAME);
48+
debugger;
4849
});
4950

5051
});

src/database/database.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,15 @@ export class AngularFireDatabase {
3131

3232
}
3333

34+
export {
35+
PathReference,
36+
DatabaseQuery,
37+
DatabaseReference,
38+
DatabaseSnapshot,
39+
ChildEvent,
40+
ListenEvent,
41+
SnapshotChange,
42+
QueryFn,
43+
ListReference,
44+
ObjectReference
45+
} from './interfaces';

src/database/index.spec.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ import './database.spec';
22
import './utils.spec';
33
import './observable/fromRef.spec';
44
import './list/changes.spec';
5+
import './list/snapshot-changes.spec';

src/database/list/changes.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import 'rxjs/add/operator/skip';
99
const rando = () => (Math.random() + 1).toString(36).substring(7);
1010
const FIREBASE_APP_NAME = rando();
1111

12-
fdescribe('listChanges', () => {
12+
describe('listChanges', () => {
1313
let app: FirebaseApp;
1414
let db: AngularFireDatabase;
1515
let ref: (path: string) => firebase.database.Reference;
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
});

src/database/list/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { isNil } from '../utils';
22

33
export function validateEventsArray(events?: any[]) {
44
if(isNil(events) || events!.length === 0) {
5-
events = ['added', 'removed', 'changed', 'moved'];
5+
events = ['child_added', 'child_removed', 'child_changed', 'child_moved'];
66
}
77
return events;
88
}

src/root.spec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ export * from './packages-dist/database/database.spec';
1010
export * from './packages-dist/database/utils.spec';
1111
export * from './packages-dist/database/observable/fromRef.spec';
1212
export * from './packages-dist/database/list/changes.spec';
13+
export * from './packages-dist/database/list/snapshot-changes.spec';

0 commit comments

Comments
 (0)