77import { HttpClientTestingModule } from '@angular/common/http/testing'
88import { RouterTestingModule } from '@angular/router/testing'
99import { CUSTOM_ELEMENTS_SCHEMA , Component } from '@angular/core'
10+ import { PortalModule } from '@angular/cdk/portal'
11+ import { MatCardModule } from '@angular/material/card'
1012import { of , Subject , NEVER , throwError } from 'rxjs'
1113
1214import { WINDOW } from '../../../cdk/window'
@@ -22,10 +24,21 @@ import { InterstitialObservabilityService } from 'src/app/core/login-interstitia
2224
2325import { AuthorizeComponent } from './authorize.component'
2426
25- // Dummy interstitial component used for typing purposes in tests
26- @Component ( { template : '' , standalone : true } )
27+ // Dummy interstitial component used for typing purposes in tests. It renders a
28+ // marker so tests can tell "attached to the outlet" from "the flag was set".
29+ @Component ( {
30+ template : '<p class="interstitial-body">interstitial</p>' ,
31+ standalone : true ,
32+ } )
2733class DummyInterstitialComponent {
34+ // Set on construction so tests that attach through a real outlet can reach
35+ // the instance the portal built, rather than a hand-made stand-in.
36+ static lastInstance : DummyInterstitialComponent | null = null
2837 finish = new Subject < void > ( )
38+
39+ constructor ( ) {
40+ DummyInterstitialComponent . lastInstance = this
41+ }
2942}
3043
3144describe ( 'AuthorizeComponent' , ( ) => {
@@ -84,6 +97,12 @@ describe('AuthorizeComponent', () => {
8497 imports : [
8598 HttpClientTestingModule ,
8699 RouterTestingModule ,
100+ // The real AuthorizeModule imports both. Without them cdkPortalOutlet is
101+ // an unknown attribute here, the outlet ViewChild can never resolve, and
102+ // no test can tell a mounted outlet from a missing one — which is how
103+ // PD-8720 shipped green.
104+ PortalModule ,
105+ MatCardModule ,
87106 DummyInterstitialComponent ,
88107 ] ,
89108 declarations : [ AuthorizeComponent ] ,
@@ -112,11 +131,24 @@ describe('AuthorizeComponent', () => {
112131 } )
113132
114133 function createComponent ( ) {
134+ DummyInterstitialComponent . lastInstance = null
115135 fixture = TestBed . createComponent ( AuthorizeComponent )
116136 component = fixture . componentInstance
117137 fixture . detectChanges ( )
118138 }
119139
140+ /**
141+ * Renders the authorization card so the `#interstitialOutlet` ViewChild
142+ * resolves against a real CdkPortalOutlet. Hand-assigning `component.outlet`
143+ * instead is what hid PD-8720: it guarantees the one thing that was actually
144+ * undefined in production.
145+ */
146+ function mountAuthorizationCard ( ) {
147+ component . loading = false
148+ component . showAuthorizationComponent = true
149+ fixture . detectChanges ( )
150+ }
151+
120152 it ( 'should create' , ( ) => {
121153 createComponent ( )
122154 expect ( component ) . toBeTruthy ( )
@@ -324,13 +356,7 @@ describe('AuthorizeComponent', () => {
324356 ; ( component as any ) . interstitialComponent =
325357 DummyInterstitialComponent as any
326358
327- const finish$ = new Subject < void > ( )
328- ; ( component as any ) . outlet = {
329- attachComponentPortal : ( ) => ( {
330- instance : { finish : finish$ . asObservable ( ) } ,
331- changeDetectorRef : { detectChanges : ( ) => { } } ,
332- } ) ,
333- }
359+ mountAuthorizationCard ( )
334360
335361 const finishSpy = spyOn < any > (
336362 component as any ,
@@ -340,7 +366,7 @@ describe('AuthorizeComponent', () => {
340366 component . handleRedirect ( '/x' )
341367 expect ( component . showInterstital ) . toBeTrue ( )
342368
343- finish$ . next ( )
369+ DummyInterstitialComponent . lastInstance . finish . next ( )
344370
345371 expect ( finishSpy ) . toHaveBeenCalled ( )
346372 // The dialog path closes on afterClosed(); here `finish` is the only signal
@@ -354,41 +380,28 @@ describe('AuthorizeComponent', () => {
354380 // card's *ngIf does not also test showInterstital the interstitial is
355381 // destroyed in the same tick and the user is left on a blank page.
356382 createComponent ( )
357- component . loading = false
358- component . showAuthorizationComponent = true
359- fixture . detectChanges ( )
383+ mountAuthorizationCard ( )
360384 expect ( fixture . nativeElement . querySelector ( 'mat-card' ) ) . toBeTruthy ( )
361385 ; ( component as any ) . interstitialComponent =
362386 DummyInterstitialComponent as any
363- ; ( component as any ) . outlet = {
364- attachComponentPortal : ( ) => ( {
365- instance : { finish : new Subject < void > ( ) . asObservable ( ) } ,
366- changeDetectorRef : { detectChanges : ( ) => { } } ,
367- } ) ,
368- }
369387
370388 component . handleRedirect ( '/x' )
371389 fixture . detectChanges ( )
372390
373391 expect ( component . showAuthorizationComponent ) . toBeFalse ( )
374392 expect ( component . showInterstital ) . toBeTrue ( )
375393 expect ( fixture . nativeElement . querySelector ( 'mat-card' ) ) . toBeTruthy ( )
394+ expect (
395+ fixture . nativeElement . querySelector ( '.interstitial-body' )
396+ ) . toBeTruthy ( )
376397 } )
377398
378399 it ( 'handleRedirect: with interstitial -> shows interstitial instead of redirect' , ( ) => {
379400 createComponent ( )
401+ mountAuthorizationCard ( )
380402 ; ( component as any ) . interstitialComponent =
381403 DummyInterstitialComponent as any
382404
383- // mock outlet to avoid CDK dependency
384- const finish$ = new Subject < void > ( )
385- ; ( component as any ) . outlet = {
386- attachComponentPortal : ( ) => ( {
387- instance : { finish : finish$ . asObservable ( ) } ,
388- changeDetectorRef : { detectChanges : ( ) => { } } ,
389- } ) ,
390- }
391-
392405 spyOn < any > ( component as any , 'finishRedirect' ) . and . returnValue ( NEVER )
393406
394407 component . handleRedirect ( '/x' )
@@ -397,6 +410,66 @@ describe('AuthorizeComponent', () => {
397410 expect ( component . showAuthorizationComponent ) . toBeFalse ( )
398411 } )
399412
413+ it ( 'ngOnInit: already authorized WITH interstitial -> renders it instead of a blank page' , fakeAsync ( ( ) => {
414+ // PD-8720. This path skips the authorization component, so nothing mounts
415+ // the card that holds the outlet. showInterstitial() has to mount it
416+ // itself; before it did, the ViewChild was undefined and attaching the
417+ // portal threw, leaving no interstitial and no redirect.
418+ userServiceSpy . getUserSession . and . returnValue (
419+ of ( {
420+ loggedIn : true ,
421+ oauthSession : { redirectUrl : '/here?code=1' , responseType : 'code' } ,
422+ } as any )
423+ )
424+ recordServiceSpy . getRecord . and . returnValue ( of ( { } as any ) )
425+ loginInterstitialsSpy . isUserFullyLoaded . and . returnValue ( true )
426+ loginInterstitialsSpy . checkLoginInterstitials . and . returnValue (
427+ of ( DummyInterstitialComponent as any )
428+ )
429+
430+ createComponent ( )
431+
432+ // Nothing has mounted the card at this point, which is the state the bug
433+ // was reported in.
434+ expect ( component . showAuthorizationComponent ) . toBeFalse ( )
435+ expect ( fixture . nativeElement . querySelector ( 'mat-card' ) ) . toBeFalsy ( )
436+
437+ tick ( )
438+ fixture . detectChanges ( )
439+
440+ expect ( component . outlet ) . toBeDefined ( )
441+ expect ( component . showInterstital ) . toBeTrue ( )
442+ expect ( fixture . nativeElement . querySelector ( 'mat-card' ) ) . toBeTruthy ( )
443+ expect (
444+ fixture . nativeElement . querySelector ( '.interstitial-body' )
445+ ) . toBeTruthy ( )
446+ // The flow waits on the interstitial rather than redirecting past it
447+ expect ( windowMock . outOfRouterNavigation ) . not . toHaveBeenCalled ( )
448+ } ) )
449+
450+ it ( 'showInterstitial: outlet that cannot be mounted redirects instead of blanking' , ( ) => {
451+ createComponent ( )
452+ // `loading` keeps <main> and the card it holds out of the DOM, so the
453+ // outlet cannot resolve. The user is still owed the redirect_uri.
454+ component . loading = true
455+ fixture . detectChanges ( )
456+ ; ( component as any ) . interstitialComponent =
457+ DummyInterstitialComponent as any
458+
459+ const finishSpy = spyOn < any > (
460+ component as any ,
461+ 'finishRedirect'
462+ ) . and . returnValue ( of ( true ) )
463+
464+ expect ( ( ) => component . handleRedirect ( '/x' ) ) . not . toThrow ( )
465+
466+ expect ( component . outlet ) . toBeUndefined ( )
467+ expect ( finishSpy ) . toHaveBeenCalled ( )
468+ expect ( component . showInterstital ) . toBeFalse ( )
469+ // The journey opened by shown() has to close, or it stays open forever
470+ expect ( interstitialObservabilitySpy . closed ) . toHaveBeenCalled ( )
471+ } )
472+
400473 it ( 'handleRedirect: without interstitial -> calls finishRedirect' , ( ) => {
401474 createComponent ( )
402475
0 commit comments