frontend-app-api: add discovery tests

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2023-09-05 16:58:37 +02:00
parent 89f03fa044
commit 888748f10b
2 changed files with 117 additions and 9 deletions
@@ -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';
@@ -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,
]);
});
});