77
88import type { CoreSetup , CoreStart , Logger } from '@kbn/core/server' ;
99import { coreMock , loggingSystemMock } from '@kbn/core/server/mocks' ;
10- import { PDF_REPORT_TYPE_V2 , PNG_REPORT_TYPE_V2 } from '../common/constants/report_types' ;
10+ import {
11+ CSV_REPORT_TYPE ,
12+ CSV_REPORT_TYPE_V2 ,
13+ PDF_REPORT_TYPE ,
14+ PDF_REPORT_TYPE_V2 ,
15+ PNG_REPORT_TYPE_V2 ,
16+ } from '../common/constants' ;
1117import type { ReportingCore , ReportingInternalStart } from './core' ;
18+ import { ExportTypesRegistry } from './lib/export_types_registry' ;
1219import { ReportingPlugin } from './plugin' ;
1320import {
1421 createMockConfigSchema ,
@@ -30,6 +37,8 @@ describe('Reporting Plugin', () => {
3037 let plugin : ReportingPlugin ;
3138
3239 beforeEach ( async ( ) => {
40+ jest . clearAllMocks ( ) ;
41+
3342 configSchema = createMockConfigSchema ( ) ;
3443 initContext = coreMock . createPluginInitializerContext ( configSchema ) ;
3544 coreSetup = coreMock . createSetup ( configSchema ) ;
@@ -81,50 +90,62 @@ describe('Reporting Plugin', () => {
8190 ` ) ;
8291 expect ( logger . error ) . toHaveBeenCalledTimes ( 2 ) ;
8392 } ) ;
84- describe ( 'config and export types registry validation' , ( ) => {
85- it ( 'expect image reporting to be in registry by default' , async ( ) => {
86- // wait for the setup phase background work
87- plugin . setup ( coreSetup , pluginSetup ) ;
88- await new Promise ( setImmediate ) ;
89-
90- // create a way for an error to happen
91- const reportingCore = ( plugin as unknown as { reportingCore : ReportingCore } ) . reportingCore ;
92-
93- // wait for the startup phase background work
94- plugin . start ( coreStart , pluginStart ) ;
95- await new Promise ( setImmediate ) ;
96- expect ( reportingCore . getExportTypesRegistry ( ) . getById ( PDF_REPORT_TYPE_V2 ) ) . toHaveProperty (
97- 'id' ,
98- PDF_REPORT_TYPE_V2
99- ) ;
100- expect ( reportingCore . getExportTypesRegistry ( ) . getById ( PNG_REPORT_TYPE_V2 ) ) . toHaveProperty (
101- 'id' ,
102- PNG_REPORT_TYPE_V2
103- ) ;
93+
94+ describe ( 'config and export types registration' , ( ) => {
95+ jest . mock ( './lib/export_types_registry' ) ;
96+ ExportTypesRegistry . prototype . getAll = jest . fn ( ( ) => [ ] ) ; // code breaks if getAll returns undefined
97+ let registerSpy : jest . SpyInstance ;
98+
99+ beforeEach ( async ( ) => {
100+ registerSpy = jest . spyOn ( ExportTypesRegistry . prototype , 'register' ) ;
101+ pluginSetup = createMockPluginSetup ( { } ) as unknown as ReportingSetupDeps ;
102+ pluginStart = await createMockPluginStart ( coreStart , configSchema ) ;
103+ plugin = new ReportingPlugin ( initContext ) ;
104+ } ) ;
105+
106+ it ( 'expect all report types to be in registry' , async ( ) => {
107+ // check the spy function
108+ expect ( registerSpy ) . toHaveBeenCalledTimes ( 5 ) ;
109+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : CSV_REPORT_TYPE } ) ) ;
110+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : CSV_REPORT_TYPE_V2 } ) ) ;
111+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : PDF_REPORT_TYPE } ) ) ;
112+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : PDF_REPORT_TYPE_V2 } ) ) ;
113+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : PNG_REPORT_TYPE_V2 } ) ) ;
104114 } ) ;
105- it ( 'expect pdf to not be in registry if config does not enable it' , async ( ) => {
106- configSchema = { ...createMockConfigSchema ( ) , export_types : { pdf : { enabled : false } } } ;
115+
116+ it ( 'expect image report types not to be in registry if disabled' , async ( ) => {
117+ jest . clearAllMocks ( ) ;
118+
119+ configSchema = createMockConfigSchema ( {
120+ export_types : {
121+ csv : { enabled : true } ,
122+ pdf : { enabled : false } ,
123+ png : { enabled : false } ,
124+ } ,
125+ } ) ;
126+
107127 initContext = coreMock . createPluginInitializerContext ( configSchema ) ;
108128 coreSetup = coreMock . createSetup ( configSchema ) ;
109129 coreStart = coreMock . createStart ( ) ;
110130 pluginSetup = createMockPluginSetup ( { } ) as unknown as ReportingSetupDeps ;
111131 pluginStart = await createMockPluginStart ( coreStart , configSchema ) ;
112-
113132 plugin = new ReportingPlugin ( initContext ) ;
114- // wait for the setup phase background work
115- plugin . setup ( coreSetup , pluginSetup ) ;
116- await new Promise ( setImmediate ) ;
117-
118- // create a way for an error to happen
119- const reportingCore = ( plugin as unknown as { reportingCore : ReportingCore } ) . reportingCore ;
120-
121- // wait for the startup phase background work
122- plugin . start ( coreStart , pluginStart ) ;
123- await new Promise ( setImmediate ) ;
124- const checkPdf = ( ) => reportingCore . getExportTypesRegistry ( ) . getById ( PDF_REPORT_TYPE_V2 ) ;
125- const checkPng = ( ) => reportingCore . getExportTypesRegistry ( ) . getById ( PNG_REPORT_TYPE_V2 ) ;
126- expect ( checkPdf ) . toThrowError ( `Unknown id ${ PDF_REPORT_TYPE_V2 } ` ) ;
127- expect ( checkPng ) . toThrowError ( `Unknown id ${ PNG_REPORT_TYPE_V2 } ` ) ;
133+
134+ // check the spy function was called with CSV
135+ expect ( registerSpy ) . toHaveBeenCalledTimes ( 2 ) ;
136+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : CSV_REPORT_TYPE } ) ) ;
137+ expect ( registerSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( { id : CSV_REPORT_TYPE_V2 } ) ) ;
138+
139+ // check the spy function was NOT called with anything else
140+ expect ( registerSpy ) . not . toHaveBeenCalledWith (
141+ expect . objectContaining ( { id : PDF_REPORT_TYPE } )
142+ ) ;
143+ expect ( registerSpy ) . not . toHaveBeenCalledWith (
144+ expect . objectContaining ( { id : PDF_REPORT_TYPE_V2 } )
145+ ) ;
146+ expect ( registerSpy ) . not . toHaveBeenCalledWith (
147+ expect . objectContaining ( { id : PNG_REPORT_TYPE_V2 } )
148+ ) ;
128149 } ) ;
129150 } ) ;
130151} ) ;
0 commit comments