@@ -19,19 +19,26 @@ import {provideMockStore, MockStore} from '@ngrx/store/testing';
1919
2020import { PluginsContainer } from './plugins_container' ;
2121import { PluginsComponent } from './plugins_component' ;
22-
23- import { PluginId , LoadingMechanismType } from '../types/api' ;
22+ import { PluginRegistryModule } from './plugin_registry_module' ;
23+ import { ExtraDashboardModule } from './testing' ;
24+
25+ import {
26+ PluginId ,
27+ LoadingMechanismType ,
28+ CustomElementLoadingMechanism ,
29+ IframeLoadingMechanism ,
30+ NgElementLoadingMechanism ,
31+ } from '../types/api' ;
2432import { DataLoadState } from '../types/data' ;
25- import { createState , createCoreState } from '../core/testing' ;
2633import { State } from '../core/store' ;
27- // store/index.ts doesn't export this, but it's OK to use for testing
28- import { CoreState } from '../core/store/core_types' ;
29-
34+ import {
35+ getPlugins ,
36+ getActivePlugin ,
37+ getPluginsListLoaded ,
38+ } from '../core/store/core_selectors' ;
3039import { TestingDebuggerModule } from '../../plugins/debugger_v2/tf_debugger_v2_plugin/testing' ;
3140
3241/** @typehack */ import * as _typeHackStore from '@ngrx/store' ;
33- import { PluginRegistryModule } from './plugin_registry_module' ;
34- import { ExtraDashboardComponent , ExtraDashboardModule } from './testing' ;
3542
3643function expectPluginIframe ( element : HTMLElement , name : string ) {
3744 expect ( element . tagName ) . toBe ( 'IFRAME' ) ;
@@ -42,72 +49,61 @@ function expectPluginIframe(element: HTMLElement, name: string) {
4249
4350describe ( 'plugins_component' , ( ) => {
4451 let store : MockStore < State > ;
45- const INITIAL_CORE_STATE : Partial < CoreState > = {
46- plugins : {
47- bar : {
48- disable_reload : false ,
49- enabled : true ,
50- loading_mechanism : {
51- type : LoadingMechanismType . CUSTOM_ELEMENT ,
52- element_name : 'tb-bar' ,
53- } ,
54- tab_name : 'Bar' ,
55- remove_dom : false ,
56- } ,
57- 'extra-plugin' : {
58- disable_reload : false ,
59- enabled : true ,
60- loading_mechanism : {
61- type : LoadingMechanismType . NG_COMPONENT ,
62- } ,
63- tab_name : 'Extra' ,
64- remove_dom : false ,
65- } ,
66- foo : {
67- disable_reload : false ,
68- enabled : true ,
69- loading_mechanism : {
70- type : LoadingMechanismType . IFRAME ,
71- // This will cause 404 as test bundles do not serve
72- // data file in the karma server.
73- module_path : 'random_esmodule.js' ,
74- } ,
75- tab_name : 'Bar' ,
76- remove_dom : false ,
77- } ,
52+ const PLUGINS = {
53+ bar : {
54+ disable_reload : false ,
55+ enabled : true ,
56+ loading_mechanism : {
57+ type : LoadingMechanismType . CUSTOM_ELEMENT ,
58+ element_name : 'tb-bar' ,
59+ } as CustomElementLoadingMechanism ,
60+ tab_name : 'Bar' ,
61+ remove_dom : false ,
62+ } ,
63+ 'extra-plugin' : {
64+ disable_reload : false ,
65+ enabled : true ,
66+ loading_mechanism : {
67+ type : LoadingMechanismType . NG_COMPONENT ,
68+ } as NgElementLoadingMechanism ,
69+ tab_name : 'Extra' ,
70+ remove_dom : false ,
71+ } ,
72+ foo : {
73+ disable_reload : false ,
74+ enabled : true ,
75+ loading_mechanism : {
76+ type : LoadingMechanismType . IFRAME ,
77+ // This will cause 404 as test bundles do not serve
78+ // data file in the karma server.
79+ module_path : 'random_esmodule.js' ,
80+ } as IframeLoadingMechanism ,
81+ tab_name : 'Bar' ,
82+ remove_dom : false ,
7883 } ,
7984 } ;
8085
86+ function setActivePlugin ( plugin : PluginId ) {
87+ store . overrideSelector ( getActivePlugin , plugin ) ;
88+ store . refreshState ( ) ;
89+ }
90+
8191 beforeEach ( async ( ) => {
82- const initialState = createState (
83- createCoreState ( {
84- ...INITIAL_CORE_STATE ,
85- } )
86- ) ;
8792 await TestBed . configureTestingModule ( {
88- providers : [
89- provideMockStore ( { initialState} ) ,
90- PluginsContainer ,
91- PluginRegistryModule ,
92- ] ,
93+ providers : [ provideMockStore ( ) , PluginsContainer , PluginRegistryModule ] ,
9394 declarations : [ PluginsContainer , PluginsComponent ] ,
9495 imports : [ TestingDebuggerModule , ExtraDashboardModule ] ,
9596 } ) . compileComponents ( ) ;
9697 store = TestBed . get ( Store ) ;
98+ store . overrideSelector ( getPlugins , PLUGINS ) ;
99+ store . overrideSelector ( getActivePlugin , null ) ;
100+ store . overrideSelector ( getPluginsListLoaded , {
101+ state : DataLoadState . NOT_LOADED ,
102+ lastLoadedTimeInMs : null ,
103+ } ) ;
97104 } ) ;
98105
99106 describe ( 'plugin DOM creation' , ( ) => {
100- function setActivePlugin ( plugin : PluginId ) {
101- store . setState (
102- createState (
103- createCoreState ( {
104- ...INITIAL_CORE_STATE ,
105- activePlugin : plugin ,
106- } )
107- )
108- ) ;
109- }
110-
111107 it ( 'creates no plugin when there is no activePlugin' , ( ) => {
112108 const fixture = TestBed . createComponent ( PluginsContainer ) ;
113109 const el = fixture . debugElement . query ( By . css ( '.plugins' ) ) ;
@@ -219,42 +215,56 @@ describe('plugins_component', () => {
219215 timeInMs : number | null ,
220216 state = DataLoadState . LOADED
221217 ) {
222- store . setState (
223- createState (
224- createCoreState ( {
225- ...INITIAL_CORE_STATE ,
226- activePlugin : 'bar' ,
227- pluginsListLoaded : {
228- state,
229- lastLoadedTimeInMs : timeInMs ,
230- } ,
231- } )
232- )
233- ) ;
218+ store . overrideSelector ( getPluginsListLoaded , {
219+ state :
220+ timeInMs !== null ? DataLoadState . LOADED : DataLoadState . NOT_LOADED ,
221+ lastLoadedTimeInMs : timeInMs ,
222+ } ) ;
223+ store . refreshState ( ) ;
234224 }
235225
236226 it ( 'invokes reload method on the dashboard DOM' , ( ) => {
237227 const fixture = TestBed . createComponent ( PluginsContainer ) ;
238228
239229 setLastLoadedTime ( null , DataLoadState . NOT_LOADED ) ;
230+ setActivePlugin ( 'bar' ) ;
231+ fixture . detectChanges ( ) ;
232+ setActivePlugin ( 'foo' ) ;
233+ fixture . detectChanges ( ) ;
234+ setActivePlugin ( 'bar' ) ;
240235 fixture . detectChanges ( ) ;
241236
242237 const { nativeElement} = fixture . debugElement . query ( By . css ( '.plugins' ) ) ;
243- const [ barElement ] = nativeElement . children ;
244- const reloadSpy = jasmine . createSpy ( ) ;
245- barElement . reload = reloadSpy ;
238+ // Stamped 'bar' and 'foo'
239+ expect ( nativeElement . children . length ) . toBe ( 2 ) ;
240+ const [ barElement , fooElement ] = nativeElement . children ;
241+ const barReloadSpy = jasmine . createSpy ( ) ;
242+ barElement . reload = barReloadSpy ;
243+ const fooReloadSpy = jasmine . createSpy ( ) ;
244+ fooElement . reload = fooReloadSpy ;
246245
247246 setLastLoadedTime ( 1 ) ;
248247 fixture . detectChanges ( ) ;
249- expect ( reloadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
248+ expect ( barReloadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
249+ expect ( fooReloadSpy ) . not . toHaveBeenCalled ( ) ;
250250
251251 setLastLoadedTime ( 1 ) ;
252252 fixture . detectChanges ( ) ;
253- expect ( reloadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
253+ expect ( barReloadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
254+ expect ( fooReloadSpy ) . not . toHaveBeenCalled ( ) ;
254255
255256 setLastLoadedTime ( 2 ) ;
256257 fixture . detectChanges ( ) ;
257- expect ( reloadSpy ) . toHaveBeenCalledTimes ( 2 ) ;
258+ expect ( barReloadSpy ) . toHaveBeenCalledTimes ( 2 ) ;
259+ expect ( fooReloadSpy ) . not . toHaveBeenCalled ( ) ;
260+
261+ setActivePlugin ( 'foo' ) ;
262+ fixture . detectChanges ( ) ;
263+
264+ setLastLoadedTime ( 3 ) ;
265+ fixture . detectChanges ( ) ;
266+ expect ( barReloadSpy ) . toHaveBeenCalledTimes ( 2 ) ;
267+ expect ( fooReloadSpy ) . toHaveBeenCalledTimes ( 1 ) ;
258268 } ) ;
259269 } ) ;
260270} ) ;
0 commit comments