|
1 | 1 | import { TestBed, inject } from '@angular/core/testing';
|
2 |
| -import { PlatformRef, NgModule, CompilerFactory } from '@angular/core'; |
| 2 | +import { PlatformRef, NgModule, CompilerFactory, NgZone } from '@angular/core'; |
3 | 3 | import { FirebaseApp, AngularFireModule } from '@angular/fire';
|
4 |
| -import { Subscription } from 'rxjs'; |
| 4 | +import { Subscription, Observable, Subject, of } from 'rxjs'; |
5 | 5 | import { COMMON_CONFIG } from './test-config';
|
6 | 6 | import { BrowserModule } from '@angular/platform-browser';
|
7 | 7 | import { database } from 'firebase/app';
|
| 8 | +import { ɵZoneScheduler, ɵkeepUnstableUntilFirstFactory, ɵAngularFireSchedulers } from './angularfire2'; |
| 9 | +import { ɵPLATFORM_BROWSER_ID, ɵPLATFORM_SERVER_ID } from '@angular/common'; |
| 10 | +import { tap } from 'rxjs/operators'; |
| 11 | +import { TestScheduler } from 'rxjs/testing'; |
8 | 12 |
|
9 | 13 | describe('angularfire', () => {
|
10 | 14 | let subscription:Subscription;
|
@@ -40,6 +44,196 @@ describe('angularfire', () => {
|
40 | 44 | app.delete().then(done, done.fail);
|
41 | 45 | });
|
42 | 46 |
|
| 47 | + describe('ZoneScheduler', () => { |
| 48 | + it('should execute the scheduled work inside the specified zone', done => { |
| 49 | + let ngZone = Zone.current.fork({ |
| 50 | + name: 'ngZone' |
| 51 | + }); |
| 52 | + const rootZone = Zone.current; |
| 53 | + |
| 54 | + // Mimic real behavior: Executing in Angular |
| 55 | + ngZone.run(() => { |
| 56 | + const outsideAngularScheduler = new ɵZoneScheduler(rootZone); |
| 57 | + outsideAngularScheduler.schedule(() => { |
| 58 | + expect(Zone.current.name).not.toEqual('ngZone'); |
| 59 | + done(); |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should execute nested scheduled work inside the specified zone', done => { |
| 65 | + const testScheduler = new TestScheduler(null!); |
| 66 | + testScheduler.run(helpers => { |
| 67 | + const outsideAngularScheduler = new ɵZoneScheduler(Zone.current, testScheduler); |
| 68 | + |
| 69 | + let ngZone = Zone.current.fork({ |
| 70 | + name: 'ngZone' |
| 71 | + }); |
| 72 | + |
| 73 | + let callbacksRan = 0; |
| 74 | + |
| 75 | + // Mimic real behavior: Executing in Angular |
| 76 | + ngZone.run(() => { |
| 77 | + outsideAngularScheduler.schedule(() => { |
| 78 | + callbacksRan++; |
| 79 | + expect(Zone.current.name).not.toEqual('ngZone'); |
| 80 | + |
| 81 | + ngZone.run(() => { |
| 82 | + // Sync queueing |
| 83 | + outsideAngularScheduler.schedule(() => { |
| 84 | + callbacksRan++; |
| 85 | + expect(Zone.current.name).not.toEqual('ngZone'); |
| 86 | + }); |
| 87 | + |
| 88 | + // Async (10ms delay) nested scheduling |
| 89 | + outsideAngularScheduler.schedule(() => { |
| 90 | + callbacksRan++; |
| 91 | + expect(Zone.current.name).not.toEqual('ngZone'); |
| 92 | + }, 10); |
| 93 | + |
| 94 | + // Simulate flush from inside angular- |
| 95 | + helpers.flush(); |
| 96 | + done(); |
| 97 | + expect(callbacksRan).toEqual(3); |
| 98 | + }) |
| 99 | + }); |
| 100 | + helpers.flush(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }) |
| 104 | + }) |
| 105 | + |
| 106 | + describe('keepUnstableUntilFirstFactory', () => { |
| 107 | + let schedulers: ɵAngularFireSchedulers; |
| 108 | + let outsideZone: Zone; |
| 109 | + let insideZone: Zone; |
| 110 | + beforeAll(() => { |
| 111 | + outsideZone = Zone.current; |
| 112 | + insideZone = Zone.current.fork({ |
| 113 | + name: 'ngZone' |
| 114 | + }); |
| 115 | + const ngZone = { |
| 116 | + run: insideZone.run.bind(insideZone), |
| 117 | + runGuarded: insideZone.runGuarded.bind(insideZone), |
| 118 | + runOutsideAngular: outsideZone.runGuarded.bind(outsideZone), |
| 119 | + runTask: insideZone.run.bind(insideZone) |
| 120 | + } as NgZone; |
| 121 | + schedulers = new ɵAngularFireSchedulers(ngZone); |
| 122 | + }) |
| 123 | + |
| 124 | + it('should re-schedule emissions asynchronously', done => { |
| 125 | + const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(schedulers, ɵPLATFORM_SERVER_ID); |
| 126 | + |
| 127 | + let ran = false; |
| 128 | + of(null).pipe( |
| 129 | + keepUnstableOp, |
| 130 | + tap(() => ran = true) |
| 131 | + ).subscribe(() => { |
| 132 | + expect(ran).toEqual(true); |
| 133 | + done(); |
| 134 | + }, () => fail("Should not error")); |
| 135 | + |
| 136 | + expect(ran).toEqual(false); |
| 137 | + }); |
| 138 | + |
| 139 | + [ɵPLATFORM_SERVER_ID, ɵPLATFORM_BROWSER_ID].map(platformId => |
| 140 | + it(`should subscribe outside angular and observe inside angular (${platformId})`, done => { |
| 141 | + const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(schedulers, platformId); |
| 142 | + |
| 143 | + insideZone.run(() => { |
| 144 | + new Observable(s => { |
| 145 | + expect(Zone.current).toEqual(outsideZone); |
| 146 | + s.next("test"); |
| 147 | + }).pipe( |
| 148 | + keepUnstableOp, |
| 149 | + tap(() => { |
| 150 | + expect(Zone.current).toEqual(insideZone); |
| 151 | + }) |
| 152 | + ).subscribe(() => { |
| 153 | + expect(Zone.current).toEqual(insideZone); |
| 154 | + done(); |
| 155 | + }, err => { |
| 156 | + fail(err); |
| 157 | + }); |
| 158 | + }); |
| 159 | + }) |
| 160 | + ); |
| 161 | + |
| 162 | + it('should block until first emission on server platform', done => { |
| 163 | + const testScheduler = new TestScheduler(null!); |
| 164 | + testScheduler.run(helpers => { |
| 165 | + const outsideZone = Zone.current; |
| 166 | + const taskTrack = new Zone['TaskTrackingZoneSpec'](); |
| 167 | + const insideZone = Zone.current.fork(taskTrack); |
| 168 | + const trackingSchedulers: ɵAngularFireSchedulers = { |
| 169 | + ngZone: { |
| 170 | + run: insideZone.run.bind(insideZone), |
| 171 | + runGuarded: insideZone.runGuarded.bind(insideZone), |
| 172 | + runOutsideAngular: outsideZone.runGuarded.bind(outsideZone), |
| 173 | + runTask: insideZone.run.bind(insideZone) |
| 174 | + } as NgZone, |
| 175 | + outsideAngular: new ɵZoneScheduler(outsideZone, testScheduler), |
| 176 | + insideAngular: new ɵZoneScheduler(insideZone, testScheduler), |
| 177 | + }; |
| 178 | + const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(trackingSchedulers, ɵPLATFORM_SERVER_ID); |
| 179 | + |
| 180 | + const s = new Subject(); |
| 181 | + s.pipe( |
| 182 | + keepUnstableOp, |
| 183 | + ).subscribe(() => { }, err => { fail(err); }, () => { }); |
| 184 | + |
| 185 | + // Flush to ensure all async scheduled functions are run |
| 186 | + helpers.flush(); |
| 187 | + // Should now be blocked until first item arrives |
| 188 | + expect(taskTrack.macroTasks.length).toBe(1); |
| 189 | + expect(taskTrack.macroTasks[0].source).toBe('firebaseZoneBlock'); |
| 190 | + |
| 191 | + // Emit next item |
| 192 | + s.next(123); |
| 193 | + helpers.flush(); |
| 194 | + |
| 195 | + // Should not be blocked after first item |
| 196 | + expect(taskTrack.macroTasks.length).toBe(0); |
| 197 | + |
| 198 | + done(); |
| 199 | + }); |
| 200 | + }) |
| 201 | + |
| 202 | + it('should not block on client platform', done => { |
| 203 | + const testScheduler = new TestScheduler(null!); |
| 204 | + testScheduler.run(helpers => { |
| 205 | + const outsideZone = Zone.current; |
| 206 | + const taskTrack = new Zone['TaskTrackingZoneSpec'](); |
| 207 | + const insideZone = Zone.current.fork(taskTrack); |
| 208 | + const trackingSchedulers: ɵAngularFireSchedulers = { |
| 209 | + ngZone: { |
| 210 | + run: insideZone.run.bind(insideZone), |
| 211 | + runGuarded: insideZone.runGuarded.bind(insideZone), |
| 212 | + runOutsideAngular: outsideZone.runGuarded.bind(outsideZone), |
| 213 | + runTask: insideZone.run.bind(insideZone) |
| 214 | + } as NgZone, |
| 215 | + outsideAngular: new ɵZoneScheduler(outsideZone, testScheduler), |
| 216 | + insideAngular: new ɵZoneScheduler(insideZone, testScheduler), |
| 217 | + }; |
| 218 | + const keepUnstableOp = ɵkeepUnstableUntilFirstFactory(trackingSchedulers, ɵPLATFORM_BROWSER_ID); |
| 219 | + |
| 220 | + const s = new Subject(); |
| 221 | + s.pipe( |
| 222 | + keepUnstableOp, |
| 223 | + ).subscribe(() => { }, err => { fail(err); }, () => { }); |
| 224 | + |
| 225 | + // Flush to ensure all async scheduled functions are run |
| 226 | + helpers.flush(); |
| 227 | + |
| 228 | + // Zone should not be blocked |
| 229 | + expect(taskTrack.macroTasks.length).toBe(0); |
| 230 | + expect(taskTrack.microTasks.length).toBe(0); |
| 231 | + |
| 232 | + done(); |
| 233 | + }); |
| 234 | + }) |
| 235 | + }) |
| 236 | + |
43 | 237 | describe('FirebaseApp', () => {
|
44 | 238 | it('should provide a FirebaseApp for the FirebaseApp binding', () => {
|
45 | 239 | expect(typeof app.delete).toBe('function');
|
|
0 commit comments