Repository navigation
Expand file tree
/
Copy pathchanges.spec.ts
More file actions
146 lines (129 loc) · 5.13 KB
/
Copy pathchanges.spec.ts
File metadata and controls
146 lines (129 loc) · 5.13 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
import firebase from 'firebase/app';
import { AngularFireModule, FirebaseApp } from '@angular/fire';
import { AngularFireDatabase, AngularFireDatabaseModule, listChanges, URL } from '../public_api';
import { TestBed } from '@angular/core/testing';
import { COMMON_CONFIG } from '../../test-config';
import { skip, take } from 'rxjs/operators';
import 'firebase/database';
import { rando } from '../../firestore/utils.spec';
describe('listChanges', () => {
let app: FirebaseApp;
let db: AngularFireDatabase;
let ref: (path: string) => firebase.database.Reference;
let batch = {};
const items = [{ name: 'zero' }, { name: 'one' }, { name: 'two' }].map((item, i) => ({ key: i.toString(), ...item }));
Object.keys(items).forEach((key, i) => {
batch[i] = items[key];
});
// make batch immutable to preserve integrity
batch = Object.freeze(batch);
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, rando()),
AngularFireDatabaseModule
],
providers: [
{ provide: URL, useValue: 'http://localhost:9000' }
]
});
app = TestBed.inject(FirebaseApp);
db = TestBed.inject(AngularFireDatabase);
ref = (path: string) => db.database.ref(path);
});
afterEach(() => {
app.delete();
});
describe('events', () => {
it('should stream value at first', (done) => {
const someRef = ref(rando());
const obs = listChanges(someRef, ['child_added']);
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']);
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']);
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']);
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' });
});
/* FLAKES? aref.set not fufilling
it('should process a new child_removed event', done => {
const aref = ref(rando());
const obs = listChanges(aref, ['child_added','child_removed']);
aref.set(batch).then(() => {
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);
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'])
aref.set(batch).then(() => {
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);
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'])
aref.set(batch).then(() => {
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);
aref.child(items[0].key).setPriority('a', () => {});
});
});*/
});
});