@@ -56,7 +56,7 @@ import { CoreSystem } from './core_system';
56
56
jest . spyOn ( CoreSystem . prototype , 'stop' ) ;
57
57
58
58
const defaultCoreSystemParams = {
59
- rootDomElement : null ! ,
59
+ rootDomElement : document . createElement ( 'div' ) ,
60
60
injectedMetadata : { } as any ,
61
61
requireLegacyFiles : jest . fn ( ) ,
62
62
} ;
@@ -91,8 +91,8 @@ describe('constructor', () => {
91
91
} ) ;
92
92
} ) ;
93
93
94
- it ( 'passes rootDomElement, requireLegacyFiles , and useLegacyTestHarness to LegacyPlatformService' , ( ) => {
95
- const rootDomElement = { rootDomElement : true } as any ;
94
+ it ( 'passes requireLegacyFiles, useLegacyTestHarness , and a dom element to LegacyPlatformService' , ( ) => {
95
+ const rootDomElement = document . createElement ( 'div' ) ;
96
96
const requireLegacyFiles = { requireLegacyFiles : true } as any ;
97
97
const useLegacyTestHarness = { useLegacyTestHarness : true } as any ;
98
98
@@ -106,14 +106,14 @@ describe('constructor', () => {
106
106
107
107
expect ( MockLegacyPlatformService ) . toHaveBeenCalledTimes ( 1 ) ;
108
108
expect ( MockLegacyPlatformService ) . toHaveBeenCalledWith ( {
109
- rootDomElement ,
109
+ targetDomElement : expect . any ( HTMLElement ) ,
110
110
requireLegacyFiles,
111
111
useLegacyTestHarness,
112
112
} ) ;
113
113
} ) ;
114
114
115
115
it ( 'passes injectedMetadata, rootDomElement, and a stopCoreSystem function to FatalErrorsService' , ( ) => {
116
- const rootDomElement = { rootDomElement : true } as any ;
116
+ const rootDomElement = document . createElement ( 'div' ) ;
117
117
const injectedMetadata = { injectedMetadata : true } as any ;
118
118
119
119
const coreSystem = new CoreSystem ( {
@@ -152,14 +152,22 @@ describe('#stop', () => {
152
152
} ) ;
153
153
154
154
describe ( '#start()' , ( ) => {
155
- function startCore ( ) {
155
+ function startCore ( rootDomElement = defaultCoreSystemParams . rootDomElement ) {
156
156
const core = new CoreSystem ( {
157
157
...defaultCoreSystemParams ,
158
+ rootDomElement,
158
159
} ) ;
159
160
160
161
core . start ( ) ;
161
162
}
162
163
164
+ it ( 'clears the children of the rootDomElement and appends container for legacyPlatform' , ( ) => {
165
+ const root = document . createElement ( 'div' ) ;
166
+ root . innerHTML = '<p>foo bar</p>' ;
167
+ startCore ( root ) ;
168
+ expect ( root . innerHTML ) . toBe ( '<div></div>' ) ;
169
+ } ) ;
170
+
163
171
it ( 'calls injectedMetadata#start()' , ( ) => {
164
172
startCore ( ) ;
165
173
const [ mockInstance ] = MockInjectedMetadataService . mock . instances ;
@@ -173,14 +181,29 @@ describe('#start()', () => {
173
181
expect ( mockInstance . start ) . toHaveBeenCalledTimes ( 1 ) ;
174
182
expect ( mockInstance . start ) . toHaveBeenCalledWith ( ) ;
175
183
} ) ;
184
+ } ) ;
176
185
177
- it ( 'calls legacyPlatform#start()' , ( ) => {
178
- startCore ( ) ;
179
- const [ mockInstance ] = MockLegacyPlatformService . mock . instances ;
180
- expect ( mockInstance . start ) . toHaveBeenCalledTimes ( 1 ) ;
181
- expect ( mockInstance . start ) . toHaveBeenCalledWith ( {
182
- injectedMetadata : mockInjectedMetadataStartContract ,
183
- fatalErrors : mockFatalErrorsStartContract ,
186
+ describe ( 'LegacyPlatform targetDomElement' , ( ) => {
187
+ it ( 'only mounts the element when started, before starting the legacyPlatformService' , ( ) => {
188
+ const rootDomElement = document . createElement ( 'div' ) ;
189
+ const core = new CoreSystem ( {
190
+ ...defaultCoreSystemParams ,
191
+ rootDomElement,
192
+ } ) ;
193
+
194
+ const [ legacyPlatform ] = MockLegacyPlatformService . mock . instances ;
195
+
196
+ let targetDomElementParentInStart : HTMLElement ;
197
+ ( legacyPlatform as any ) . start . mockImplementation ( ( ) => {
198
+ targetDomElementParentInStart = targetDomElement . parentElement ;
184
199
} ) ;
200
+
201
+ // targetDomElement should not have a parent element when the LegacyPlatformService is constructed
202
+ const [ [ { targetDomElement } ] ] = MockLegacyPlatformService . mock . calls ;
203
+ expect ( targetDomElement ) . toHaveProperty ( 'parentElement' , null ) ;
204
+
205
+ // starting the core system should mount the targetDomElement as a child of the rootDomElement
206
+ core . start ( ) ;
207
+ expect ( targetDomElementParentInStart ! ) . toBe ( rootDomElement ) ;
185
208
} ) ;
186
209
} ) ;
0 commit comments