actions.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import { mockStandardFieldConfigOptions } from 'test/helpers/fieldConfig';
  2. import { standardEditorsRegistry, standardFieldConfigEditorRegistry } from '@grafana/data';
  3. import { PanelModel } from 'app/features/dashboard/state';
  4. import { getPanelPlugin } from 'app/features/plugins/__mocks__/pluginMocks';
  5. import { panelPluginLoaded } from 'app/features/plugins/admin/state/actions';
  6. import { thunkTester } from '../../../../test/core/thunk/thunkTester';
  7. import { changePanelPlugin } from './actions';
  8. import { panelModelAndPluginReady } from './reducers';
  9. jest.mock('app/features/plugins/importPanelPlugin', () => {
  10. return {
  11. importPanelPlugin: function () {
  12. return Promise.resolve(
  13. getPanelPlugin({
  14. id: 'table',
  15. }).useFieldConfig()
  16. );
  17. },
  18. };
  19. });
  20. standardFieldConfigEditorRegistry.setInit(() => mockStandardFieldConfigOptions());
  21. standardEditorsRegistry.setInit(() => mockStandardFieldConfigOptions());
  22. describe('panel state actions', () => {
  23. describe('changePanelPlugin', () => {
  24. it('Should load plugin and call changePlugin', async () => {
  25. const sourcePanel = new PanelModel({ id: 12, type: 'graph' });
  26. const dispatchedActions = await thunkTester({
  27. plugins: {
  28. panels: {},
  29. },
  30. panels: {},
  31. })
  32. .givenThunk(changePanelPlugin)
  33. .whenThunkIsDispatched({
  34. panel: sourcePanel,
  35. pluginId: 'table',
  36. });
  37. expect(dispatchedActions.length).toBe(2);
  38. expect(dispatchedActions[0].type).toBe(panelPluginLoaded.type);
  39. expect(dispatchedActions[1].type).toBe(panelModelAndPluginReady.type);
  40. expect(sourcePanel.type).toBe('table');
  41. });
  42. it('Should apply options and fieldConfig', async () => {
  43. const sourcePanel = new PanelModel({ id: 12, type: 'graph' });
  44. await thunkTester({
  45. plugins: {
  46. panels: {},
  47. },
  48. panels: {},
  49. })
  50. .givenThunk(changePanelPlugin)
  51. .whenThunkIsDispatched({
  52. panel: sourcePanel,
  53. pluginId: 'table',
  54. options: {
  55. showHeader: true,
  56. },
  57. fieldConfig: {
  58. defaults: {
  59. unit: 'short',
  60. },
  61. overrides: [],
  62. },
  63. });
  64. expect(sourcePanel.options.showHeader).toBe(true);
  65. expect(sourcePanel.fieldConfig.defaults.unit).toBe('short');
  66. });
  67. });
  68. });