Repository navigation
Expand file tree
/
Copy pathfromRef.spec.ts
More file actions
190 lines (173 loc) · 5.98 KB
/
Copy pathfromRef.spec.ts
File metadata and controls
190 lines (173 loc) · 5.98 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
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
import { DatabaseReference } from '../interfaces';
import { FirebaseApp, AngularFireModule } from '@angular/fire';
import { AngularFireDatabase, AngularFireDatabaseModule, fromRef } from '@angular/fire/database';
import { TestBed, inject } from '@angular/core/testing';
import { COMMON_CONFIG } from '../test-config';
import { 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('fromRef', () => {
let app: FirebaseApp;
let ref: (path: string) => DatabaseReference;
let batch = {};
const items = [{ name: 'one' }, { name: 'two' }, { name: 'three' }].map(item => ( { key: rando(), ...item } ));
Object.keys(items).forEach(function (key) {
const itemValue = items[key];
batch[itemValue.key] = 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], (app_: FirebaseApp) => {
app = app_;
app.database().goOffline();
ref = (path: string) => { app.database().goOffline(); return app.database().ref(path); };
})();
});
afterEach(done => {
app.delete().then(done, done.fail);
});
it('it should be async', (done) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'value');
let count = 0;
expect(count).toEqual(0);
const sub = obs.subscribe(change => {
count = count + 1;
expect(count).toEqual(1);
done();
sub.unsubscribe();
});
expect(count).toEqual(0);
});
it('it should should handle non-existence', (done) => {
const itemRef = ref(rando());
itemRef.set({});
const obs = fromRef(itemRef, 'value');
const sub = obs.pipe(take(1)).subscribe(change => {
expect(change.payload.exists()).toEqual(false);
expect(change.payload.val()).toEqual(null);
}).add(done);
});
it('once should complete', (done) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'value', 'once');
obs.subscribe(change => {}, () => {}, done);
});
it('it should listen and then unsubscribe', (done) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'value');
let count = 0;
const sub = obs.subscribe(change => {
count = count + 1;
// hard coding count to one will fail if the unsub
// doesn't actually unsub
expect(count).toEqual(1);
done();
sub.unsubscribe();
itemRef.push({ name: 'anotha one' });
});
});
describe('events', () => {
it('should stream back a child_added event', async (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'child_added');
let count = 0;
const sub = obs.subscribe(change => {
count = count + 1;
const { type, payload } = change;
expect(type).toEqual('child_added');
expect(payload.val()).toEqual(batch[payload.key!]);
if (count === items.length) {
done();
sub.unsubscribe();
expect(sub.closed).toEqual(true);
}
});
});
it('should stream back a child_changed event', async (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'child_changed');
const name = 'look at what you made me do';
const key = items[0].key;
const sub = obs.subscribe(change => {
const { type, payload } = change;
expect(type).toEqual('child_changed');
expect(payload.key).toEqual(key);
expect(payload.val()).toEqual({ key, name });
sub.unsubscribe();
done();
});
itemRef.child(key).update({ name });
});
it('should stream back a child_removed event', async (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'child_removed');
const key = items[0].key;
const name = items[0].name;
const sub = obs.subscribe(change => {
const { type, payload } = change;
expect(type).toEqual('child_removed');
expect(payload.key).toEqual(key);
expect(payload.val()).toEqual({ key, name });
sub.unsubscribe();
done();
});
itemRef.child(key).remove();
});
it('should stream back a child_moved event', async (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'child_moved');
const key = items[2].key;
const name = items[2].name;
const sub = obs.subscribe(change => {
const { type, payload } = change;
expect(type).toEqual('child_moved');
expect(payload.key).toEqual(key);
expect(payload.val()).toEqual({ key, name });
sub.unsubscribe();
done();
});
itemRef.child(key).setPriority(-100, () => {});
});
it('should stream back a value event', (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const obs = fromRef(itemRef, 'value');
const sub = obs.subscribe(change => {
const { type, payload } = change;
expect(type).toEqual('value');
expect(payload.val()).toEqual(batch);
done();
sub.unsubscribe();
expect(sub.closed).toEqual(true);
});
});
it('should stream back query results', (done: any) => {
const itemRef = ref(rando());
itemRef.set(batch);
const query = itemRef.orderByChild('name').equalTo(items[0].name);
const obs = fromRef(query, 'value');
const sub = obs.subscribe(change => {
let child;
change.payload.forEach(snap => { child = snap.val(); return true; });
expect(child).toEqual(items[0]);
done();
});
});
});
});