diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/xpack_info.js b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/xpack_info.js index 2edfbbc27fc45..2e0d608e522d7 100644 --- a/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/xpack_info.js +++ b/x-pack/legacy/plugins/xpack_main/server/lib/__tests__/xpack_info.js @@ -312,6 +312,54 @@ describe('XPackInfo', () => { }); }); + it('onLicenseInfoChange() allows to subscribe to license update', async () => { + const license$ = new BehaviorSubject(createLicense()); + + const xPackInfo = new XPackInfo(mockServer, { + licensing: { + license$, + refresh: () => null, + }, + }); + + const watcherFeature = xPackInfo.feature('watcher'); + watcherFeature.registerLicenseCheckResultsGenerator(info => ({ + type: info.license.getType(), + })); + + const statuses = []; + xPackInfo.onLicenseInfoChange(() => statuses.push(watcherFeature.getLicenseCheckResults())); + + license$.next(createLicense({ type: 'basic' })); + expect(statuses).to.eql([{ type: 'basic' }]); + + license$.next(createLicense({ type: 'trial' })); + expect(statuses).to.eql([{ type: 'basic' }, { type: 'trial' }]); + }); + + it('refreshNow() leads to onLicenseInfoChange()', async () => { + const license$ = new BehaviorSubject(createLicense()); + + const xPackInfo = new XPackInfo(mockServer, { + licensing: { + license$, + refresh: () => license$.next({ type: 'basic' }), + }, + }); + + const watcherFeature = xPackInfo.feature('watcher'); + + watcherFeature.registerLicenseCheckResultsGenerator(info => ({ + type: info.license.getType(), + })); + + const statuses = []; + xPackInfo.onLicenseInfoChange(() => statuses.push(watcherFeature.getLicenseCheckResults())); + + await xPackInfo.refreshNow(); + expect(statuses).to.eql([{ type: 'basic' }]); + }); + it('getSignature() returns correct signature.', async () => { const license$ = new BehaviorSubject(createLicense()); const xPackInfo = new XPackInfo(mockServer, { diff --git a/x-pack/legacy/plugins/xpack_main/server/lib/xpack_info.ts b/x-pack/legacy/plugins/xpack_main/server/lib/xpack_info.ts index fbb8929154c36..9d5a8e64645ec 100644 --- a/x-pack/legacy/plugins/xpack_main/server/lib/xpack_info.ts +++ b/x-pack/legacy/plugins/xpack_main/server/lib/xpack_info.ts @@ -101,6 +101,8 @@ export class XPackInfo { error: license.error, }; } + + this._licenseInfoChangedListeners.forEach(fn => fn()); }); this._license = new XPackInfoLicense(() => this._cache.license);