plugin_loader.test.ts 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. // Use the real plugin_loader (stubbed by default)
  2. jest.unmock('app/features/plugins/plugin_loader');
  3. (global as any).ace = {
  4. define: jest.fn(),
  5. };
  6. jest.mock('app/core/core', () => {
  7. return {
  8. coreModule: {
  9. directive: jest.fn(),
  10. },
  11. };
  12. });
  13. import { AppPluginMeta, PluginMetaInfo, PluginType, AppPlugin } from '@grafana/data';
  14. import { SystemJS } from '@grafana/runtime';
  15. // Loaded after the `unmock` abve
  16. import { importAppPlugin } from '../plugin_loader';
  17. class MyCustomApp extends AppPlugin {
  18. initWasCalled = false;
  19. calledTwice = false;
  20. init(meta: AppPluginMeta) {
  21. this.initWasCalled = true;
  22. this.calledTwice = this.meta === meta;
  23. }
  24. }
  25. describe('Load App', () => {
  26. const app = new MyCustomApp();
  27. const modulePath = 'my/custom/plugin/module';
  28. beforeAll(() => {
  29. SystemJS.set(modulePath, SystemJS.newModule({ plugin: app }));
  30. });
  31. afterAll(() => {
  32. SystemJS.delete(modulePath);
  33. });
  34. it('should call init and set meta', async () => {
  35. const meta: AppPluginMeta = {
  36. id: 'test-app',
  37. module: modulePath,
  38. baseUrl: 'xxx',
  39. info: {} as PluginMetaInfo,
  40. type: PluginType.app,
  41. name: 'test',
  42. };
  43. // Check that we mocked the import OK
  44. const m = await SystemJS.import(modulePath);
  45. expect(m.plugin).toBe(app);
  46. const loaded = await importAppPlugin(meta);
  47. expect(loaded).toBe(app);
  48. expect(app.meta).toBe(meta);
  49. expect(app.initWasCalled).toBeTruthy();
  50. expect(app.calledTwice).toBeFalsy();
  51. const again = await importAppPlugin(meta);
  52. expect(again).toBe(app);
  53. expect(app.calledTwice).toBeTruthy();
  54. });
  55. });