From 888748f10b54984e2710020dd1f16a0fa32e48dc Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Tue, 5 Sep 2023 16:58:37 +0200 Subject: [PATCH] frontend-app-api: add discovery tests Signed-off-by: Patrik Oldsberg --- packages/app-next-example-plugin/src/index.ts | 11 +- .../src/wiring/discovery.test.ts | 115 ++++++++++++++++++ 2 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 packages/frontend-app-api/src/wiring/discovery.test.ts diff --git a/packages/app-next-example-plugin/src/index.ts b/packages/app-next-example-plugin/src/index.ts index 94858a71a5..dae1eea379 100644 --- a/packages/app-next-example-plugin/src/index.ts +++ b/packages/app-next-example-plugin/src/index.ts @@ -15,13 +15,6 @@ */ export { examplePlugin } from './plugin'; -// TODO: This should be an extension created & exported in the `plugin.tsx` -// YEAHHH most likely but there's no api(output) for it yet. -export { ExampleSidebarItem } from './ExampleSidebarItem'; -export const a1 = null; -export const a2 = false; -export const a3 = true; -export const a4 = Symbol('b'); -export const a5 = 3; -export const a6 = []; +// TODO: This should be an extension created & exported in the `plugin.tsx` +export { ExampleSidebarItem } from './ExampleSidebarItem'; diff --git a/packages/frontend-app-api/src/wiring/discovery.test.ts b/packages/frontend-app-api/src/wiring/discovery.test.ts new file mode 100644 index 0000000000..249f9dcea9 --- /dev/null +++ b/packages/frontend-app-api/src/wiring/discovery.test.ts @@ -0,0 +1,115 @@ +/* + * Copyright 2023 The Backstage Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { createPlugin } from '@backstage/frontend-plugin-api'; +import { getAvailablePlugins } from './discovery'; + +const globalSpy = jest.fn(); +Object.defineProperty(global, '__@backstage/discovered__', { + get: globalSpy, +}); + +describe('getAvailablePlugins', () => { + afterEach(jest.resetAllMocks); + + it('should discover nothing with undefined global', () => { + expect(getAvailablePlugins()).toEqual([]); + }); + + it('should discover nothing with empty global', () => { + globalSpy.mockReturnValue({ + modules: [], + }); + expect(getAvailablePlugins()).toEqual([]); + }); + + it('should discover a plugin', () => { + const testPlugin = createPlugin({ id: 'test' }); + globalSpy.mockReturnValue({ + modules: [ + { + module: { + testPlugin, + }, + }, + ], + }); + expect(getAvailablePlugins()).toEqual([testPlugin]); + }); + + it('should ignore garbage', () => { + const testPlugin = createPlugin({ id: 'test' }); + globalSpy.mockReturnValue({ + modules: [ + { + module: { + testPlugin, + a: 1, + b: null, + c: undefined, + d: Symbol('wat'), + e: () => {}, + f: [], + g: {}, + h: 'h', + i: NaN, + j: Infinity, + k: -Infinity, + l: new Date(), + m: new RegExp('wat'), + n: new Error('wat'), + o: new Map(), + p: new Set(), + q: new WeakMap(), + r: new WeakSet(), + s: new ArrayBuffer(1), + t: new DataView(new ArrayBuffer(1)), + u: false, + v: true, + w: 0, + }, + }, + ], + }); + expect(getAvailablePlugins()).toEqual([testPlugin]); + }); + + it('should discover multiple plugins', () => { + const test1Plugin = createPlugin({ id: 'test1' }); + const test2Plugin = createPlugin({ id: 'test2' }); + const test3Plugin = createPlugin({ id: 'test3' }); + globalSpy.mockReturnValue({ + modules: [ + { + module: { + test1Plugin, + test2Plugin, + }, + }, + { + module: { + test3Plugin, + }, + }, + ], + }); + expect(getAvailablePlugins()).toEqual([ + test1Plugin, + test2Plugin, + test3Plugin, + ]); + }); +});