widgetLegendSelectionState.spec.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. features: ['dashboards-releases-on-charts'],
  48. };
  49. dashboard = {
  50. ...DashboardFixture([widget, {...widget, id: '23456'}]),
  51. };
  52. router = {
  53. ...RouterFixture({location}),
  54. };
  55. legendFunctions = new WidgetLegendSelectionState({
  56. dashboard,
  57. location,
  58. organization,
  59. router,
  60. });
  61. });
  62. it('set initial unselected legend options', () => {
  63. expect(legendFunctions.getWidgetSelectionState(widget)).toEqual({
  64. [`Releases${SERIES_NAME_DELIMITER}12345`]: false,
  65. });
  66. });
  67. it('updates legend query param when legend option toggled', () => {
  68. legendFunctions.setWidgetSelectionState({'Releases:12345': true}, widget);
  69. expect(router.replace).toHaveBeenCalledWith({
  70. query: {unselectedSeries: [`12345${WIDGET_ID_DELIMITER}`]},
  71. });
  72. });
  73. it('updates legend query param when legend option toggled but not in query params', () => {
  74. location = {...location, query: {...location.query, unselectedSeries: []}};
  75. legendFunctions.setWidgetSelectionState(
  76. {
  77. [`Releases${SERIES_NAME_DELIMITER}12345`]: false,
  78. [`count()${SERIES_NAME_DELIMITER}12345`]: true,
  79. },
  80. widget
  81. );
  82. expect(router.replace).toHaveBeenCalledWith({
  83. query: {unselectedSeries: [`12345${WIDGET_ID_DELIMITER}Releases`]},
  84. });
  85. });
  86. it('gives updated query param when widget change submitted', () => {
  87. expect(legendFunctions.setMultipleWidgetSelectionStateURL(dashboard)).toEqual([
  88. `12345${WIDGET_ID_DELIMITER}Releases`,
  89. `23456${WIDGET_ID_DELIMITER}Releases`,
  90. ]);
  91. });
  92. });
  93. describe('legend naming', function () {
  94. let widget: Widget;
  95. beforeEach(() => {
  96. widget = {
  97. id: '12345',
  98. title: 'Test Query',
  99. displayType: DisplayType.AREA,
  100. widgetType: WidgetType.ERRORS,
  101. interval: '5m',
  102. queries: [
  103. {
  104. name: '',
  105. conditions: '',
  106. fields: ['count()', 'Releases'],
  107. aggregates: ['count()', 'Releases'],
  108. columns: [],
  109. orderby: '',
  110. },
  111. ],
  112. };
  113. });
  114. it('formats to query param format from selected', () => {
  115. expect(
  116. legendFunctions.encodeLegendQueryParam(widget, {
  117. [`Releases${SERIES_NAME_DELIMITER}${widget.id}`]: false,
  118. })
  119. ).toEqual(`${widget.id}${WIDGET_ID_DELIMITER}Releases`);
  120. });
  121. it('formats to selected format from query param', () => {
  122. expect(legendFunctions.decodeLegendQueryParam(widget)).toEqual({
  123. [`Releases${SERIES_NAME_DELIMITER}${widget.id}`]: false,
  124. });
  125. });
  126. });
  127. });