@@ -4,18 +4,24 @@ import {
4
4
TestBed ,
5
5
tick ,
6
6
} from '@angular/core/testing' ;
7
- import { ApplicationRef , NgZone } from '@angular/core' ;
7
+ import { ApplicationRef , NgZone , PLATFORM_ID } from '@angular/core' ;
8
8
import {
9
- AnimationFrameTickScheduler ,
9
+ ZonelessTickScheduler ,
10
10
NoopTickScheduler ,
11
11
TickScheduler ,
12
12
} from '../../src/core/tick-scheduler' ;
13
13
import { ngZoneMock , noopNgZoneMock } from '../fixtures/fixtures' ;
14
14
15
15
describe ( 'TickScheduler' , ( ) => {
16
- function setup ( ngZone : unknown ) {
16
+ function setup ( ngZone : unknown , server : boolean = false ) {
17
17
TestBed . configureTestingModule ( {
18
- providers : [ { provide : NgZone , useValue : ngZone } ] ,
18
+ providers : [
19
+ { provide : NgZone , useValue : ngZone } ,
20
+ {
21
+ provide : PLATFORM_ID ,
22
+ useValue : server ? 'server' : 'browser' ,
23
+ } ,
24
+ ] ,
19
25
} ) ;
20
26
const tickScheduler = TestBed . inject ( TickScheduler ) ;
21
27
const appRef = TestBed . inject ( ApplicationRef ) ;
@@ -31,16 +37,16 @@ describe('TickScheduler', () => {
31
37
} ) ;
32
38
} ) ;
33
39
34
- describe ( 'when NgZone is not provided' , ( ) => {
40
+ describe ( 'when NgZone is not provided and running in server context ' , ( ) => {
35
41
// `fakeAsync` uses 16ms as `requestAnimationFrame` delay
36
42
const animationFrameDelay = 16 ;
37
43
38
- it ( 'should initialize AnimationFrameTickScheduler ' , ( ) => {
44
+ it ( 'should initialize ZonelessTickScheduler ' , ( ) => {
39
45
const { tickScheduler } = setup ( noopNgZoneMock ) ;
40
- expect ( tickScheduler instanceof AnimationFrameTickScheduler ) . toBe ( true ) ;
46
+ expect ( tickScheduler instanceof ZonelessTickScheduler ) . toBe ( true ) ;
41
47
} ) ;
42
48
43
- it ( 'should schedule tick using the animationFrameScheduler ' , fakeAsync ( ( ) => {
49
+ it ( 'should schedule tick using the ZonelessTickScheduler ' , fakeAsync ( ( ) => {
44
50
const { tickScheduler, appRef } = setup ( noopNgZoneMock ) ;
45
51
46
52
tickScheduler . schedule ( ) ;
@@ -87,4 +93,56 @@ describe('TickScheduler', () => {
87
93
expect ( appRef . tick ) . toHaveBeenCalledTimes ( 3 ) ;
88
94
} ) ) ;
89
95
} ) ;
96
+
97
+ describe ( 'when NgZone is not provided' , ( ) => {
98
+ it ( 'should initialize ZonelessTickScheduler' , ( ) => {
99
+ const { tickScheduler } = setup ( noopNgZoneMock , true ) ;
100
+ expect ( tickScheduler instanceof ZonelessTickScheduler ) . toBe ( true ) ;
101
+ } ) ;
102
+
103
+ it ( 'should schedule tick using the ZonelessTickScheduler' , fakeAsync ( ( ) => {
104
+ const { tickScheduler, appRef } = setup ( noopNgZoneMock , true ) ;
105
+
106
+ tickScheduler . schedule ( ) ;
107
+
108
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
109
+ tick ( ) ;
110
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 1 ) ;
111
+ } ) ) ;
112
+
113
+ it ( 'should coalesce multiple synchronous schedule calls' , fakeAsync ( ( ) => {
114
+ const { tickScheduler, appRef } = setup ( noopNgZoneMock , true ) ;
115
+
116
+ tickScheduler . schedule ( ) ;
117
+ tickScheduler . schedule ( ) ;
118
+ tickScheduler . schedule ( ) ;
119
+
120
+ tick ( ) ;
121
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 1 ) ;
122
+ } ) ) ;
123
+
124
+ it ( 'should coalesce multiple schedule calls that are queued to the microtask queue' , fakeAsync ( ( ) => {
125
+ const { tickScheduler, appRef } = setup ( noopNgZoneMock , true ) ;
126
+
127
+ queueMicrotask ( ( ) => tickScheduler . schedule ( ) ) ;
128
+ queueMicrotask ( ( ) => tickScheduler . schedule ( ) ) ;
129
+ queueMicrotask ( ( ) => tickScheduler . schedule ( ) ) ;
130
+
131
+ flushMicrotasks ( ) ;
132
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 0 ) ;
133
+ tick ( ) ;
134
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 1 ) ;
135
+ } ) ) ;
136
+
137
+ it ( 'should schedule multiple ticks for multiple asynchronous schedule calls' , fakeAsync ( ( ) => {
138
+ const { tickScheduler, appRef } = setup ( noopNgZoneMock , true ) ;
139
+
140
+ setTimeout ( ( ) => tickScheduler . schedule ( ) , 100 ) ;
141
+ setTimeout ( ( ) => tickScheduler . schedule ( ) , 200 ) ;
142
+ setTimeout ( ( ) => tickScheduler . schedule ( ) , 300 ) ;
143
+
144
+ tick ( 300 ) ;
145
+ expect ( appRef . tick ) . toHaveBeenCalledTimes ( 3 ) ;
146
+ } ) ) ;
147
+ } ) ;
90
148
} ) ;
0 commit comments