plugin_loader.test.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* tslint:disable:import-blacklist */
  14. import System from 'systemjs/dist/system.js';
  15. import { AppPluginMeta, PluginMetaInfo, PluginType, PluginIncludeType, AppPlugin } from '@grafana/ui';
  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. System.set(modulePath, System.newModule({ plugin: app }));
  30. });
  31. afterAll(() => {
  32. System.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 System.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. });
  56. import { ExampleConfigCtrl as ConfigCtrl } from 'app/plugins/app/example-app/legacy/config';
  57. import { AngularExamplePageCtrl } from 'app/plugins/app/example-app/legacy/angular_example_page';
  58. describe('Load Legacy App', () => {
  59. const app = {
  60. ConfigCtrl,
  61. AngularExamplePageCtrl, // Must match `pages.component` in plugin.json
  62. };
  63. const modulePath = 'my/custom/legacy/plugin/module';
  64. beforeAll(() => {
  65. System.set(modulePath, System.newModule(app));
  66. });
  67. afterAll(() => {
  68. System.delete(modulePath);
  69. });
  70. it('should call init and set meta for legacy app', async () => {
  71. const meta: AppPluginMeta = {
  72. id: 'test-app',
  73. module: modulePath,
  74. baseUrl: 'xxx',
  75. info: {} as PluginMetaInfo,
  76. type: PluginType.app,
  77. name: 'test',
  78. includes: [
  79. {
  80. type: PluginIncludeType.page,
  81. name: 'Example Page',
  82. component: 'AngularExamplePageCtrl',
  83. role: 'Viewer',
  84. addToNav: false,
  85. },
  86. ],
  87. };
  88. const loaded = await importAppPlugin(meta);
  89. expect(loaded).toHaveProperty('angularPages');
  90. expect(loaded.angularPages).toHaveProperty('AngularExamplePageCtrl', AngularExamplePageCtrl);
  91. });
  92. });