Repository navigation
Expand file tree
/
Copy pathchanges.spec.ts
More file actions
148 lines (133 loc) · 5.4 KB
/
Copy pathchanges.spec.ts
File metadata and controls
148 lines (133 loc) · 5.4 KB
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
import { database } from 'firebase/app';
import { FirebaseApp, AngularFireModule } from '@angular/fire';
import { AngularFireDatabase, AngularFireDatabaseModule, listChanges } from '@angular/fire/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from '../test-config';
import { skip, take } from 'rxjs/operators';
// generate random string to test fidelity of naming
const rando = () => (Math.random() + 1).toString(36).substring(7);
const FIREBASE_APP_NAME = rando();
describe('listChanges', () => {
let app: FirebaseApp;
let db: AngularFireDatabase;
let ref: (path: string) => database.Reference;
let batch = {};
const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ( { key: i.toString(), ...item } ));
Object.keys(items).forEach(function (key, i) {
const itemValue = items[key];
batch[i] = itemValue;
});
// make batch immutable to preserve integrity
batch = Object.freeze(batch);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, FIREBASE_APP_NAME),
AngularFireDatabaseModule
]
});
inject([FirebaseApp, AngularFireDatabase], (app_: FirebaseApp, _db: AngularFireDatabase) => {
app = app_;
db = _db;
app.database().goOffline();
ref = (path: string) => { app.database().goOffline(); return app.database().ref(path); };
})();
});
afterEach(done => {
app.delete().then(done, done.fail);
});
describe('events', () => {
it('should stream value at first', (done) => {
const someRef = ref(rando());
const obs = listChanges(someRef, ['child_added']);
const sub = obs.pipe(take(1)).subscribe(changes => {
const data = changes.map(change => change.payload.val());
expect(data).toEqual(items);
}).add(done);
someRef.set(batch);
});
it('should process a new child_added event', done => {
const aref = ref(rando());
const obs = listChanges(aref, ['child_added']);
const sub = obs.pipe(skip(1),take(1)).subscribe(changes => {
const data = changes.map(change => change.payload.val());
expect(data[3]).toEqual({ name: 'anotha one' });
}).add(done);
aref.set(batch);
aref.push({ name: 'anotha one' });
});
it('should stream in order events', (done) => {
const aref = ref(rando());
const obs = listChanges(aref.orderByChild('name'), ['child_added']);
const sub = obs.pipe(take(1)).subscribe(changes => {
const names = changes.map(change => change.payload.val().name);
expect(names[0]).toEqual('one');
expect(names[1]).toEqual('two');
expect(names[2]).toEqual('zero');
}).add(done);
aref.set(batch);
});
it('should stream in order events w/child_added', (done) => {
const aref = ref(rando());
const obs = listChanges(aref.orderByChild('name'), ['child_added']);
const sub = obs.pipe(skip(1),take(1)).subscribe(changes => {
const names = changes.map(change => change.payload.val().name);
expect(names[0]).toEqual('anotha one');
expect(names[1]).toEqual('one');
expect(names[2]).toEqual('two');
expect(names[3]).toEqual('zero');
}).add(done);
aref.set(batch);
aref.push({ name: 'anotha one' });
});
it('should stream events filtering', (done) => {
const aref = ref(rando());
const obs = listChanges(aref.orderByChild('name').equalTo('zero'), ['child_added']);
obs.pipe(skip(1),take(1)).subscribe(changes => {
const names = changes.map(change => change.payload.val().name);
expect(names[0]).toEqual('zero');
expect(names[1]).toEqual('zero');
}).add(done);
aref.set(batch);
aref.push({ name: 'zero' });
});
it('should process a new child_removed event', done => {
const aref = ref(rando());
const obs = listChanges(aref, ['child_added','child_removed']);
const sub = obs.pipe(skip(1),take(1)).subscribe(changes => {
const data = changes.map(change => change.payload.val());
expect(data.length).toEqual(items.length - 1);
}).add(done);
app.database().goOnline();
aref.set(batch).then(() => {
aref.child(items[0].key).remove();
});
});
it('should process a new child_changed event', (done) => {
const aref = ref(rando());
const obs = listChanges(aref, ['child_added','child_changed'])
const sub = obs.pipe(skip(1),take(1)).subscribe(changes => {
const data = changes.map(change => change.payload.val());
expect(data[1].name).toEqual('lol');
}).add(done);
app.database().goOnline();
aref.set(batch).then(() => {
aref.child(items[1].key).update({ name: 'lol'});
});
});
it('should process a new child_moved event', (done) => {
const aref = ref(rando());
const obs = listChanges(aref, ['child_added','child_moved'])
const sub = obs.pipe(skip(1),take(1)).subscribe(changes => {
const data = changes.map(change => change.payload.val());
// We moved the first item to the last item, so we check that
// the new result is now the last result
expect(data[data.length - 1]).toEqual(items[0]);
}).add(done);
app.database().goOnline();
aref.set(batch).then(() => {
aref.child(items[0].key).setPriority('a', () => {});
});
});
});
});