1111import '@angular/compiler' ;
1212/* eslint-enable import/no-unassigned-import */
1313
14- import { Component , PlatformRef , inject , provideZonelessChangeDetection } from '@angular/core' ;
14+ import {
15+ ApplicationRef ,
16+ Component ,
17+ PlatformRef ,
18+ inject ,
19+ provideZonelessChangeDetection ,
20+ } from '@angular/core' ;
1521import { bootstrapApplication } from '@angular/platform-browser' ;
1622import { setTimeout } from 'node:timers/promises' ;
1723import { renderAngular } from '../../src/utils/ng' ;
@@ -77,28 +83,26 @@ describe('renderAngular', () => {
7783 controller . signal ,
7884 ) ;
7985
80- expect ( result . hasNavigationError ) . toBeFalse ( ) ;
86+ expect ( result ) . toEqual (
87+ jasmine . objectContaining ( {
88+ hasNavigationError : false ,
89+ content : jasmine . any ( Function ) ,
90+ } ) ,
91+ ) ;
8192 if ( ! ( 'content' in result ) ) {
82- fail ( 'Expected renderAngular result to expose content()' ) ;
83-
8493 return ;
8594 }
8695
8796 controller . abort ( 'client disconnected' ) ;
8897
89- let caughtError : unknown ;
90- try {
91- await result . content ( ) ;
92- fail ( 'Expected content() to reject with AbortError' ) ;
93- } catch ( error ) {
94- caughtError = error ;
95- }
96-
97- expect ( caughtError ) . toBeInstanceOf ( DOMException ) ;
98- expect ( ( caughtError as DOMException ) . name ) . toBe ( 'AbortError' ) ;
99- expect ( ( caughtError as DOMException ) . message ) . toContain (
98+ const contentPromise = result . content ( ) ;
99+ await expectAsync ( contentPromise ) . toBeRejectedWithError (
100+ DOMException ,
100101 'Request for: http://localhost/ was aborted.\nclient disconnected' ,
101102 ) ;
103+ await expectAsync ( contentPromise ) . toBeRejectedWith (
104+ jasmine . objectContaining ( { name : 'AbortError' } ) ,
105+ ) ;
102106
103107 await setTimeout ( 0 ) ;
104108 expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
@@ -130,9 +134,13 @@ describe('renderAngular', () => {
130134 'ssr' ,
131135 ) ;
132136
137+ expect ( result ) . toEqual (
138+ jasmine . objectContaining ( {
139+ content : jasmine . any ( Function ) ,
140+ destroy : jasmine . any ( Function ) ,
141+ } ) ,
142+ ) ;
133143 if ( ! ( 'content' in result ) ) {
134- fail ( 'Expected renderAngular result to expose content()' ) ;
135-
136144 return ;
137145 }
138146
@@ -171,9 +179,8 @@ describe('renderAngular', () => {
171179 'ssr' ,
172180 ) ;
173181
182+ expect ( result ) . toEqual ( jasmine . objectContaining ( { destroy : jasmine . any ( Function ) } ) ) ;
174183 if ( ! ( 'destroy' in result ) ) {
175- fail ( 'Expected renderAngular result to expose destroy()' ) ;
176-
177184 return ;
178185 }
179186
@@ -185,4 +192,92 @@ describe('renderAngular', () => {
185192 await result . destroy ( ) ;
186193 expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
187194 } ) ;
195+
196+ it ( 'should reuse the in-flight promise when content() is called concurrently' , async ( ) => {
197+ let destroySpy : jasmine . Spy | undefined ;
198+
199+ @Component ( {
200+ selector : 'app-root' ,
201+ template : 'Concurrent content works' ,
202+ } )
203+ class AppComponent {
204+ constructor ( ) {
205+ destroySpy = spyOn ( inject ( PlatformRef ) , 'destroy' ) . and . callThrough ( ) ;
206+ }
207+ }
208+
209+ const result = await renderAngular (
210+ BASE_HTML ,
211+ ( context ) =>
212+ bootstrapApplication (
213+ AppComponent ,
214+ { providers : [ provideZonelessChangeDetection ( ) ] } ,
215+ context ,
216+ ) ,
217+ new URL ( 'http://localhost/' ) ,
218+ [ ] ,
219+ 'ssr' ,
220+ ) ;
221+
222+ expect ( result ) . toEqual ( jasmine . objectContaining ( { content : jasmine . any ( Function ) } ) ) ;
223+ if ( ! ( 'content' in result ) ) {
224+ return ;
225+ }
226+
227+ const p1 = result . content ( ) ;
228+ const p2 = result . content ( ) ;
229+ expect ( p1 ) . toBe ( p2 ) ;
230+
231+ const [ html1 , html2 ] = await Promise . all ( [ p1 , p2 ] ) ;
232+ expect ( html1 ) . toContain ( 'Concurrent content works' ) ;
233+ expect ( html2 ) . toBe ( html1 ) ;
234+
235+ await setTimeout ( 0 ) ;
236+ expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
237+ } ) ;
238+
239+ it ( 'should reject content() with AbortError if applicationRef is destroyed before rendering runs' , async ( ) => {
240+ let appRef : ApplicationRef | undefined ;
241+ let destroySpy : jasmine . Spy | undefined ;
242+
243+ @Component ( {
244+ selector : 'app-root' ,
245+ template : 'External app destroy works' ,
246+ } )
247+ class AppComponent {
248+ constructor ( ) {
249+ appRef = inject ( ApplicationRef ) ;
250+ destroySpy = spyOn ( inject ( PlatformRef ) , 'destroy' ) . and . callThrough ( ) ;
251+ }
252+ }
253+
254+ const result = await renderAngular (
255+ BASE_HTML ,
256+ ( context ) =>
257+ bootstrapApplication (
258+ AppComponent ,
259+ { providers : [ provideZonelessChangeDetection ( ) ] } ,
260+ context ,
261+ ) ,
262+ new URL ( 'http://localhost/' ) ,
263+ [ ] ,
264+ 'ssr' ,
265+ ) ;
266+
267+ expect ( result ) . toEqual ( jasmine . objectContaining ( { content : jasmine . any ( Function ) } ) ) ;
268+ if ( ! ( 'content' in result ) ) {
269+ return ;
270+ }
271+
272+ const contentPromise = result . content ( ) ;
273+ appRef ?. destroy ( ) ;
274+
275+ await expectAsync ( contentPromise ) . toBeRejectedWithError (
276+ DOMException ,
277+ / R e q u e s t f o r : h t t p : \/ \/ l o c a l h o s t \/ w a s a b o r t e d / ,
278+ ) ;
279+
280+ await setTimeout ( 0 ) ;
281+ expect ( destroySpy ) . toHaveBeenCalledTimes ( 1 ) ;
282+ } ) ;
188283} ) ;
0 commit comments