textPanelMigrationHandler.test.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import { FieldConfigSource, PanelModel } from '@grafana/data';
  2. import { TextMode, PanelOptions } from './models.gen';
  3. import { textPanelMigrationHandler } from './textPanelMigrationHandler';
  4. describe('textPanelMigrationHandler', () => {
  5. describe('when invoked and previous version was old Angular text panel', () => {
  6. it('then should migrate options', () => {
  7. const panel: any = {
  8. content: '<span>Hello World<span>',
  9. mode: 'html',
  10. options: {},
  11. };
  12. const result = textPanelMigrationHandler(panel);
  13. expect(result.content).toEqual('<span>Hello World<span>');
  14. expect(result.mode).toEqual('html');
  15. expect(panel.content).toBeUndefined();
  16. expect(panel.mode).toBeUndefined();
  17. });
  18. });
  19. describe('when invoked and previous version 7.1 or later', () => {
  20. it('then not migrate options', () => {
  21. const panel: any = {
  22. content: '<span>Hello World<span>',
  23. mode: 'html',
  24. options: { content: 'New content' },
  25. pluginVersion: '7.1.0',
  26. };
  27. const result = textPanelMigrationHandler(panel);
  28. expect(result.content).toEqual('New content');
  29. });
  30. });
  31. describe('when invoked and previous version was not old Angular text panel', () => {
  32. it('then should just pass options through', () => {
  33. const panel: PanelModel<PanelOptions> = {
  34. id: 1,
  35. fieldConfig: {} as unknown as FieldConfigSource,
  36. options: {
  37. content: `# Title
  38. For markdown syntax help: [commonmark.org/help](https://commonmark.org/help/)
  39. `,
  40. mode: TextMode.Markdown,
  41. },
  42. };
  43. const result = textPanelMigrationHandler(panel);
  44. expect(result.content).toEqual(`# Title
  45. For markdown syntax help: [commonmark.org/help](https://commonmark.org/help/)
  46. `);
  47. expect(result.mode).toEqual('markdown');
  48. });
  49. });
  50. describe('when invoked and previous version was using text mode', () => {
  51. it('then should switch to markdown', () => {
  52. const mode = 'text' as unknown as TextMode;
  53. const panel: PanelModel<PanelOptions> = {
  54. id: 1,
  55. fieldConfig: {} as unknown as FieldConfigSource,
  56. options: {
  57. content: `# Title
  58. For markdown syntax help: [commonmark.org/help](https://commonmark.org/help/)
  59. `,
  60. mode,
  61. },
  62. };
  63. const result = textPanelMigrationHandler(panel);
  64. expect(result.content).toEqual(`# Title
  65. For markdown syntax help: [commonmark.org/help](https://commonmark.org/help/)
  66. `);
  67. expect(result.mode).toEqual('markdown');
  68. });
  69. });
  70. });