forked from angular/angularfire
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangularfire2.spec.ts
More file actions
246 lines (208 loc) · 7.5 KB
/
angularfire2.spec.ts
File metadata and controls
246 lines (208 loc) · 7.5 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import { TestBed } from '@angular/core/testing';
import { CompilerFactory, NgModule, NgZone, PlatformRef } from '@angular/core';
import { AngularFireModule, FirebaseApp } from './public_api';
import { Observable, of, Subject } from 'rxjs';
import { COMMON_CONFIG } from '../test-config';
import { BrowserModule } from '@angular/platform-browser';
import firebase from 'firebase/app';
import { ɵAngularFireSchedulers, ɵkeepUnstableUntilFirstFactory, ɵZoneScheduler } from './angularfire2';
import { tap } from 'rxjs/operators';
import { TestScheduler } from 'rxjs/testing';
import { rando } from '../firestore/utils.spec';
describe('angularfire', () => {
let app: FirebaseApp;
let rootRef: firebase.database.Reference;
let questionsRef: firebase.database.Reference;
let listOfQuestionsRef: firebase.database.Reference;
let defaultPlatform: PlatformRef;
let appName: string;
beforeEach(() => {
appName = rando();
TestBed.configureTestingModule({
imports: [AngularFireModule.initializeApp(COMMON_CONFIG, appName)]
});
app = TestBed.inject(FirebaseApp);
defaultPlatform = TestBed.inject(PlatformRef);
rootRef = app.database().ref();
questionsRef = rootRef.child('questions');
listOfQuestionsRef = rootRef.child('list-of-questions');
});
afterEach(() => {
rootRef.remove();
app.delete();
});
describe('ZoneScheduler', () => {
it('should execute the scheduled work inside the specified zone', done => {
const ngZone = Zone.current.fork({
name: 'ngZone'
});
const rootZone = Zone.current;
// Mimic real behavior: Executing in Angular
ngZone.run(() => {
const outsideAngularScheduler = new ɵZoneScheduler(rootZone);
outsideAngularScheduler.schedule(() => {
expect(Zone.current.name).not.toEqual('ngZone');
done();
});
});
});
it('should execute nested scheduled work inside the specified zone', done => {
const testScheduler = new TestScheduler(null);
testScheduler.run(helpers => {
const outsideAngularScheduler = new ɵZoneScheduler(Zone.current, testScheduler);
const ngZone = Zone.current.fork({
name: 'ngZone'
});
let callbacksRan = 0;
// Mimic real behavior: Executing in Angular
ngZone.run(() => {
outsideAngularScheduler.schedule(() => {
callbacksRan++;
expect(Zone.current.name).not.toEqual('ngZone');
ngZone.run(() => {
// Sync queueing
outsideAngularScheduler.schedule(() => {
callbacksRan++;
expect(Zone.current.name).not.toEqual('ngZone');
});
// Async (10ms delay) nested scheduling
outsideAngularScheduler.schedule(() => {
callbacksRan++;
expect(Zone.current.name).not.toEqual('ngZone');
}, 10);
// Simulate flush from inside angular-
helpers.flush();
done();
expect(callbacksRan).toEqual(3);
});
});
helpers.flush();
});
});
});
});
describe('keepUnstableUntilFirstFactory', () => {
let schedulers: ɵAngularFireSchedulers;
let outsideZone: Zone;
let insideZone: Zone;
beforeAll(() => {
outsideZone = Zone.current;
insideZone = Zone.current.fork({
name: 'ngZone'
});
const ngZone = {
run: insideZone.run.bind(insideZone),
runGuarded: insideZone.runGuarded.bind(insideZone),
runOutsideAngular: outsideZone.runGuarded.bind(outsideZone),
runTask: insideZone.run.bind(insideZone)
} as NgZone;
schedulers = new ɵAngularFireSchedulers(ngZone);
});
it('should re-schedule emissions asynchronously', done => {
const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(schedulers);
let ran = false;
of(null).pipe(
keepUnstableOp,
tap(() => ran = true)
).subscribe(() => {
expect(ran).toEqual(true);
done();
}, () => fail('Should not error'));
expect(ran).toEqual(false);
});
it(`should subscribe outside angular and observe inside angular`, done => {
const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(schedulers);
insideZone.run(() => {
new Observable(s => {
expect(Zone.current).toEqual(outsideZone);
s.next('test');
}).pipe(
keepUnstableOp,
tap(() => {
expect(Zone.current).toEqual(insideZone);
})
).subscribe(() => {
expect(Zone.current).toEqual(insideZone);
done();
}, err => {
fail(err);
});
});
});
it('should block until first emission', done => {
const testScheduler = new TestScheduler(null);
testScheduler.run(helpers => {
const outsideZone = Zone.current;
// tslint:disable-next-line:no-string-literal
const taskTrack = new Zone['TaskTrackingZoneSpec']();
const insideZone = Zone.current.fork(taskTrack);
const trackingSchedulers: ɵAngularFireSchedulers = {
ngZone: {
run: insideZone.run.bind(insideZone),
runGuarded: insideZone.runGuarded.bind(insideZone),
runOutsideAngular: outsideZone.runGuarded.bind(outsideZone),
runTask: insideZone.run.bind(insideZone)
} as NgZone,
outsideAngular: new ɵZoneScheduler(outsideZone, testScheduler),
insideAngular: new ɵZoneScheduler(insideZone, testScheduler)
};
const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(trackingSchedulers);
const s = new Subject();
s.pipe(
keepUnstableOp
).subscribe(() => {
}, err => {
fail(err);
}, () => {
});
// Flush to ensure all async scheduled functions are run
helpers.flush();
// Should now be blocked until first item arrives
expect(taskTrack.macroTasks.length).toBe(1);
expect(taskTrack.macroTasks[0].source).toBe('firebaseZoneBlock');
// Emit next item
s.next(123);
helpers.flush();
// TODO drop this, it's to work around my 15ms timeout hack
setTimeout(() => {
// Should not be blocked after first item
expect(taskTrack.macroTasks.length).toBe(0);
done();
}, 150);
});
});
});
describe('FirebaseApp', () => {
it('should provide a FirebaseApp for the FirebaseApp binding', () => {
expect(typeof app.delete).toBe('function');
});
if (typeof window !== 'undefined') {
it('should have the provided name', () => {
expect(app.name).toBe(appName);
});
it('should use an already intialized firebase app if it exists', done => {
@NgModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, appName),
BrowserModule
]
})
class MyModule {
ngDoBootstrap() {
}
}
const compilerFactory: CompilerFactory =
defaultPlatform.injector.get(CompilerFactory, null);
const moduleFactory = compilerFactory.createCompiler().compileModuleSync(MyModule);
defaultPlatform.bootstrapModuleFactory(moduleFactory)
.then(moduleRef => {
const ref = moduleRef.injector.get(FirebaseApp);
expect(ref.name).toEqual(app.name);
}).then(done, e => {
fail(e);
done();
});
});
}
});
});