Files
grafana/public/app/features/plugins/tests/plugin_loader.test.ts
Tom Ratcliffe 4e10507c84 Chore: Remove global mock of plugin_loader (#97351)
* Remove global mock of `plugin_loader`

Removing this means we can get more accurate datasources behaviour in tests

* Fix circular dependency/undefined method when plugin_loader is unmocked

* Use optional chaining for trusted policies to stop tests failing when `bootData` is partially set

* Add plugin_loader mock back into single test that is still broken

* Revert trusted type policies changes

* Fix tests that break with trusted type policies
2024-12-11 14:50:49 +00:00

67 lines
2.1 KiB
TypeScript

jest.mock('app/core/core', () => {
return {
coreModule: {
directive: jest.fn(),
},
};
});
import { AppPluginMeta, PluginMetaInfo, PluginType, AppPlugin } from '@grafana/data';
// Loaded after the `unmock` above
import { addedComponentsRegistry, addedLinksRegistry, exposedComponentsRegistry } from '../extensions/registry/setup';
import { SystemJS } from '../loader/systemjs';
import { importAppPlugin } from '../plugin_loader';
jest.mock('../extensions/registry/setup');
describe('Load App', () => {
const app = new AppPlugin();
const modulePath = 'http://localhost:3000/public/plugins/my-app-plugin/module.js';
// Hook resolver for tests
const originalResolve = SystemJS.constructor.prototype.resolve;
SystemJS.constructor.prototype.resolve = (x: unknown) => x;
beforeAll(() => {
app.init = jest.fn();
addedComponentsRegistry.register = jest.fn();
addedLinksRegistry.register = jest.fn();
exposedComponentsRegistry.register = jest.fn();
SystemJS.set(modulePath, { plugin: app });
});
afterAll(() => {
SystemJS.delete(modulePath);
SystemJS.constructor.prototype.resolve = originalResolve;
});
it('should call init and set meta', async () => {
const meta: AppPluginMeta = {
id: 'test-app',
module: modulePath,
baseUrl: 'xxx',
info: {} as PluginMetaInfo,
type: PluginType.app,
name: 'test',
};
// Check that we mocked the import OK
const m = await SystemJS.import(modulePath);
expect(m.plugin).toBe(app);
// Importing the app should initialise the meta
const importedApp = await importAppPlugin(meta);
expect(importedApp).toBe(app);
expect(app.meta).toBe(meta);
// Importing the same app again doesn't initialise it twice
const importedAppAgain = await importAppPlugin(meta);
expect(importedAppAgain).toBe(app);
expect(app.init).toHaveBeenCalledTimes(1);
expect(addedComponentsRegistry.register).toHaveBeenCalledTimes(1);
expect(addedLinksRegistry.register).toHaveBeenCalledTimes(1);
expect(exposedComponentsRegistry.register).toHaveBeenCalledTimes(1);
});
});