plugin_loader.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 { SystemJS } from '@grafana/runtime';
  14. import { AppPluginMeta, PluginMetaInfo, PluginType, PluginIncludeType, AppPlugin } from '@grafana/ui';
  15. import { importAppPlugin } from './plugin_loader';
  16. class MyCustomApp extends AppPlugin {
  17. initWasCalled = false;
  18. calledTwice = false;
  19. init(meta: AppPluginMeta) {
  20. this.initWasCalled = true;
  21. this.calledTwice = this.meta === meta;
  22. }
  23. }
  24. describe('Load App', () => {
  25. const app = new MyCustomApp();
  26. const modulePath = 'my/custom/plugin/module';
  27. beforeAll(() => {
  28. SystemJS.set(modulePath, SystemJS.newModule({ plugin: app }));
  29. });
  30. afterAll(() => {
  31. SystemJS.delete(modulePath);
  32. });
  33. it('should call init and set meta', async () => {
  34. const meta: AppPluginMeta = {
  35. id: 'test-app',
  36. module: modulePath,
  37. baseUrl: 'xxx',
  38. info: {} as PluginMetaInfo,
  39. type: PluginType.app,
  40. name: 'test',
  41. };
  42. // Check that we mocked the import OK
  43. const m = await SystemJS.import(modulePath);
  44. expect(m.plugin).toBe(app);
  45. const loaded = await importAppPlugin(meta);
  46. expect(loaded).toBe(app);
  47. expect(app.meta).toBe(meta);
  48. expect(app.initWasCalled).toBeTruthy();
  49. expect(app.calledTwice).toBeFalsy();
  50. const again = await importAppPlugin(meta);
  51. expect(again).toBe(app);
  52. expect(app.calledTwice).toBeTruthy();
  53. });
  54. });
  55. import { ExampleConfigCtrl as ConfigCtrl } from 'app/plugins/app/example-app/legacy/config';
  56. import { AngularExamplePageCtrl } from 'app/plugins/app/example-app/legacy/angular_example_page';
  57. describe('Load Legacy App', () => {
  58. const app = {
  59. ConfigCtrl,
  60. AngularExamplePageCtrl, // Must match `pages.component` in plugin.json
  61. };
  62. const modulePath = 'my/custom/legacy/plugin/module';
  63. beforeAll(() => {
  64. SystemJS.set(modulePath, SystemJS.newModule(app));
  65. });
  66. afterAll(() => {
  67. SystemJS.delete(modulePath);
  68. });
  69. it('should call init and set meta for legacy app', async () => {
  70. const meta: AppPluginMeta = {
  71. id: 'test-app',
  72. module: modulePath,
  73. baseUrl: 'xxx',
  74. info: {} as PluginMetaInfo,
  75. type: PluginType.app,
  76. name: 'test',
  77. includes: [
  78. {
  79. type: PluginIncludeType.page,
  80. name: 'Example Page',
  81. component: 'AngularExamplePageCtrl',
  82. role: 'Viewer',
  83. addToNav: false,
  84. },
  85. ],
  86. };
  87. const loaded = await importAppPlugin(meta);
  88. expect(loaded).toHaveProperty('angularPages');
  89. expect(loaded.angularPages).toHaveProperty('AngularExamplePageCtrl', AngularExamplePageCtrl);
  90. });
  91. });