1818 */
1919
2020import React from 'react' ;
21+ import { act } from 'react-dom/test-utils' ;
2122
22- import { chromeServiceMock } from '../chrome/chrome_service.mock' ;
2323import { RenderingService } from './rendering_service' ;
24- import { InternalApplicationStart } from '../application' ;
24+ import { applicationServiceMock } from '../application/application_service.mock' ;
25+ import { chromeServiceMock } from '../chrome/chrome_service.mock' ;
2526import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock' ;
2627import { overlayServiceMock } from '../overlays/overlay_service.mock' ;
28+ import { BehaviorSubject } from 'rxjs' ;
2729
2830describe ( 'RenderingService#start' , ( ) => {
29- const getService = ( { legacyMode = false } : { legacyMode ?: boolean } = { } ) => {
30- const rendering = new RenderingService ( ) ;
31- const application = {
32- getComponent : ( ) => < div > Hello application!</ div > ,
33- } as InternalApplicationStart ;
34- const chrome = chromeServiceMock . createStartContract ( ) ;
31+ let application : ReturnType < typeof applicationServiceMock . createInternalStartContract > ;
32+ let chrome : ReturnType < typeof chromeServiceMock . createStartContract > ;
33+ let overlays : ReturnType < typeof overlayServiceMock . createStartContract > ;
34+ let injectedMetadata : ReturnType < typeof injectedMetadataServiceMock . createStartContract > ;
35+ let targetDomElement : HTMLDivElement ;
36+ let rendering : RenderingService ;
37+
38+ beforeEach ( ( ) => {
39+ application = applicationServiceMock . createInternalStartContract ( ) ;
40+ application . getComponent . mockReturnValue ( < div > Hello application!</ div > ) ;
41+
42+ chrome = chromeServiceMock . createStartContract ( ) ;
3543 chrome . getHeaderComponent . mockReturnValue ( < div > Hello chrome!</ div > ) ;
36- const overlays = overlayServiceMock . createStartContract ( ) ;
44+
45+ overlays = overlayServiceMock . createStartContract ( ) ;
3746 overlays . banners . getComponent . mockReturnValue ( < div > I'm a banner!</ div > ) ;
3847
39- const injectedMetadata = injectedMetadataServiceMock . createStartContract ( ) ;
40- injectedMetadata . getLegacyMode . mockReturnValue ( legacyMode ) ;
41- const targetDomElement = document . createElement ( 'div' ) ;
42- const start = rendering . start ( {
48+ injectedMetadata = injectedMetadataServiceMock . createStartContract ( ) ;
49+
50+ targetDomElement = document . createElement ( 'div' ) ;
51+
52+ rendering = new RenderingService ( ) ;
53+ } ) ;
54+
55+ const startService = ( ) => {
56+ return rendering . start ( {
4357 application,
4458 chrome,
4559 injectedMetadata,
4660 overlays,
4761 targetDomElement,
4862 } ) ;
49- return { start, targetDomElement } ;
5063 } ;
5164
52- it ( 'renders application service into provided DOM element' , ( ) => {
53- const { targetDomElement } = getService ( ) ;
54- expect ( targetDomElement . querySelector ( 'div.application' ) ) . toMatchInlineSnapshot ( `
55- <div
56- class="application"
57- >
58- <div>
59- Hello application!
60- </div>
61- </div>
62- ` ) ;
63- } ) ;
65+ describe ( 'standard mode' , ( ) => {
66+ beforeEach ( ( ) => {
67+ injectedMetadata . getLegacyMode . mockReturnValue ( false ) ;
68+ } ) ;
6469
65- it ( 'contains wrapper divs' , ( ) => {
66- const { targetDomElement } = getService ( ) ;
67- expect ( targetDomElement . querySelector ( 'div.app-wrapper' ) ) . toBeDefined ( ) ;
68- expect ( targetDomElement . querySelector ( 'div.app-wrapper-pannel' ) ) . toBeDefined ( ) ;
69- } ) ;
70+ it ( 'renders application service into provided DOM element' , ( ) => {
71+ startService ( ) ;
72+ expect ( targetDomElement . querySelector ( 'div.application' ) ) . toMatchInlineSnapshot ( `
73+ <div
74+ class="application class-name"
75+ >
76+ <div>
77+ Hello application!
78+ </div>
79+ </div>
80+ ` ) ;
81+ } ) ;
82+
83+ it ( 'adds the `chrome-hidden` class to the AppWrapper when chrome is hidden' , ( ) => {
84+ const isVisible$ = new BehaviorSubject ( true ) ;
85+ chrome . getIsVisible$ . mockReturnValue ( isVisible$ ) ;
86+ startService ( ) ;
87+
88+ const appWrapper = targetDomElement . querySelector ( 'div.app-wrapper' ) ! ;
89+ expect ( appWrapper . className ) . toEqual ( 'app-wrapper' ) ;
90+
91+ act ( ( ) => isVisible$ . next ( false ) ) ;
92+ expect ( appWrapper . className ) . toEqual ( 'app-wrapper hidden-chrome' ) ;
7093
71- it ( 'renders the banner UI' , ( ) => {
72- const { targetDomElement } = getService ( ) ;
73- expect ( targetDomElement . querySelector ( '#globalBannerList' ) ) . toMatchInlineSnapshot ( `
74- <div
75- id="globalBannerList"
76- >
77- <div>
78- I'm a banner!
79- </div>
80- </div>
81- ` ) ;
94+ act ( ( ) => isVisible$ . next ( true ) ) ;
95+ expect ( appWrapper . className ) . toEqual ( 'app-wrapper' ) ;
96+ } ) ;
97+
98+ it ( 'adds the application classes to the AppContainer' , ( ) => {
99+ const applicationClasses$ = new BehaviorSubject < string [ ] > ( [ ] ) ;
100+ chrome . getApplicationClasses$ . mockReturnValue ( applicationClasses$ ) ;
101+ startService ( ) ;
102+
103+ const appContainer = targetDomElement . querySelector ( 'div.application' ) ! ;
104+ expect ( appContainer . className ) . toEqual ( 'application' ) ;
105+
106+ act ( ( ) => applicationClasses$ . next ( [ 'classA' , 'classB' ] ) ) ;
107+ expect ( appContainer . className ) . toEqual ( 'application classA classB' ) ;
108+
109+ act ( ( ) => applicationClasses$ . next ( [ 'classC' ] ) ) ;
110+ expect ( appContainer . className ) . toEqual ( 'application classC' ) ;
111+
112+ act ( ( ) => applicationClasses$ . next ( [ ] ) ) ;
113+ expect ( appContainer . className ) . toEqual ( 'application' ) ;
114+ } ) ;
115+
116+ it ( 'contains wrapper divs' , ( ) => {
117+ startService ( ) ;
118+ expect ( targetDomElement . querySelector ( 'div.app-wrapper' ) ) . toBeDefined ( ) ;
119+ expect ( targetDomElement . querySelector ( 'div.app-wrapper-pannel' ) ) . toBeDefined ( ) ;
120+ } ) ;
121+
122+ it ( 'renders the banner UI' , ( ) => {
123+ startService ( ) ;
124+ expect ( targetDomElement . querySelector ( '#globalBannerList' ) ) . toMatchInlineSnapshot ( `
125+ <div
126+ id="globalBannerList"
127+ >
128+ <div>
129+ I'm a banner!
130+ </div>
131+ </div>
132+ ` ) ;
133+ } ) ;
82134 } ) ;
83135
84- describe ( 'legacyMode' , ( ) => {
136+ describe ( 'legacy mode' , ( ) => {
137+ beforeEach ( ( ) => {
138+ injectedMetadata . getLegacyMode . mockReturnValue ( true ) ;
139+ } ) ;
140+
85141 it ( 'renders into provided DOM element' , ( ) => {
86- const { targetDomElement } = getService ( { legacyMode : true } ) ;
142+ startService ( ) ;
143+
87144 expect ( targetDomElement ) . toMatchInlineSnapshot ( `
88145 <div>
89146 <div
@@ -100,10 +157,8 @@ describe('RenderingService#start', () => {
100157 } ) ;
101158
102159 it ( 'returns a div for the legacy service to render into' , ( ) => {
103- const {
104- start : { legacyTargetDomElement } ,
105- targetDomElement,
106- } = getService ( { legacyMode : true } ) ;
160+ const { legacyTargetDomElement } = startService ( ) ;
161+
107162 expect ( targetDomElement . contains ( legacyTargetDomElement ! ) ) . toBe ( true ) ;
108163 } ) ;
109164 } ) ;
0 commit comments