widgetLegendSelectionState.spec.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import type {Location} from 'history';
  2. import {DashboardFixture} from 'sentry-fixture/dashboard';
  3. import {LocationFixture} from 'sentry-fixture/locationFixture';
  4. import {OrganizationFixture} from 'sentry-fixture/organization';
  5. import {RouterFixture} from 'sentry-fixture/routerFixture';
  6. import type {InjectedRouter} from 'sentry/types/legacyReactRouter';
  7. import type {Organization} from 'sentry/types/organization';
  8. import type {DashboardDetails, Widget} from 'sentry/views/dashboards/types';
  9. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  10. import WidgetLegendSelectionState from './widgetLegendSelectionState';
  11. const WIDGET_ID_DELIMITER = ':';
  12. const SERIES_NAME_DELIMITER = ';';
  13. describe('WidgetLegend functions util', () => {
  14. let legendFunctions: WidgetLegendSelectionState;
  15. describe('legendChanges', function () {
  16. let widget: Widget;
  17. let location: Location;
  18. let organization: Organization;
  19. let dashboard: DashboardDetails;
  20. let router: InjectedRouter;
  21. beforeEach(() => {
  22. widget = {
  23. id: '12345',
  24. title: 'Test Query',
  25. displayType: DisplayType.AREA,
  26. widgetType: WidgetType.ERRORS,
  27. interval: '5m',
  28. queries: [
  29. {
  30. name: '',
  31. conditions: '',
  32. fields: ['count()', 'Releases'],
  33. aggregates: ['count()', 'Releases'],
  34. columns: [],
  35. orderby: '',
  36. },
  37. ],
  38. };
  39. location = {
  40. ...LocationFixture(),
  41. query: {
  42. unselectedSeries: [`12345${WIDGET_ID_DELIMITER}Releases`],
  43. },
  44. };
  45. organization = {
  46. ...OrganizationFixture(),
  47. };
  48. dashboard = {
  49. ...DashboardFixture([widget, {...widget, id: '23456'}]),
  50. };
  51. router = {
  52. ...RouterFixture({location}),
  53. };
  54. legendFunctions = new WidgetLegendSelectionState({
  55. dashboard,
  56. location,
  57. organization,
  58. router,
  59. });
  60. });
  61. it('set initial unselected legend options', () => {
  62. expect(legendFunctions.getWidgetSelectionState(widget)).toEqual({
  63. [`Releases${SERIES_NAME_DELIMITER}12345`]: false,
  64. });
  65. });
  66. it('updates legend query param when legend option toggled', () => {
  67. legendFunctions.setWidgetSelectionState({'Releases:12345': true}, widget);
  68. expect(router.replace).toHaveBeenCalledWith({
  69. query: {unselectedSeries: [`12345${WIDGET_ID_DELIMITER}`]},
  70. });
  71. });
  72. it('updates legend query param when legend option toggled but not in query params', () => {
  73. location = {...location, query: {...location.query, unselectedSeries: []}};
  74. legendFunctions.setWidgetSelectionState(
  75. {
  76. [`Releases${SERIES_NAME_DELIMITER}12345`]: false,
  77. [`count()${SERIES_NAME_DELIMITER}12345`]: true,
  78. },
  79. widget
  80. );
  81. expect(router.replace).toHaveBeenCalledWith({
  82. query: {unselectedSeries: [`12345${WIDGET_ID_DELIMITER}Releases`]},
  83. });
  84. });
  85. it('gives updated query param when widget change submitted', () => {
  86. expect(legendFunctions.setMultipleWidgetSelectionStateURL(dashboard)).toEqual([
  87. `12345${WIDGET_ID_DELIMITER}Releases`,
  88. `23456${WIDGET_ID_DELIMITER}Releases`,
  89. ]);
  90. });
  91. });
  92. describe('legend naming', function () {
  93. let widget: Widget;
  94. beforeEach(() => {
  95. widget = {
  96. id: '12345',
  97. title: 'Test Query',
  98. displayType: DisplayType.AREA,
  99. widgetType: WidgetType.ERRORS,
  100. interval: '5m',
  101. queries: [
  102. {
  103. name: '',
  104. conditions: '',
  105. fields: ['count()', 'Releases'],
  106. aggregates: ['count()', 'Releases'],
  107. columns: [],
  108. orderby: '',
  109. },
  110. ],
  111. };
  112. });
  113. it('formats to query param format from selected', () => {
  114. expect(
  115. legendFunctions.encodeLegendQueryParam(widget, {
  116. [`Releases${SERIES_NAME_DELIMITER}${widget.id}`]: false,
  117. })
  118. ).toEqual(`${widget.id}${WIDGET_ID_DELIMITER}Releases`);
  119. });
  120. it('formats to selected format from query param', () => {
  121. expect(legendFunctions.decodeLegendQueryParam(widget)).toEqual({
  122. [`Releases${SERIES_NAME_DELIMITER}${widget.id}`]: false,
  123. });
  124. });
  125. });
  126. });