widgetLegendSelectionState.spec.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. describe('WidgetLegend functions util', () => {
  13. let legendFunctions: WidgetLegendSelectionState;
  14. describe('legendChanges', function () {
  15. let widget: Widget;
  16. let location: Location;
  17. let organization: Organization;
  18. let dashboard: DashboardDetails;
  19. let router: InjectedRouter;
  20. beforeEach(() => {
  21. widget = {
  22. id: '12345',
  23. title: 'Test Query',
  24. displayType: DisplayType.AREA,
  25. widgetType: WidgetType.ERRORS,
  26. interval: '5m',
  27. queries: [
  28. {
  29. name: '',
  30. conditions: '',
  31. fields: ['count()', 'Releases'],
  32. aggregates: ['count()', 'Releases'],
  33. columns: [],
  34. orderby: '',
  35. },
  36. ],
  37. };
  38. location = {
  39. ...LocationFixture(),
  40. query: {
  41. unselectedSeries: [`12345${WIDGET_ID_DELIMITER}Releases`],
  42. },
  43. };
  44. organization = {
  45. ...OrganizationFixture(),
  46. features: ['dashboards-releases-on-charts'],
  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: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. {'Releases:12345': false, 'count():12345': true},
  76. widget
  77. );
  78. expect(router.replace).toHaveBeenCalledWith({
  79. query: {unselectedSeries: [`12345${WIDGET_ID_DELIMITER}Releases`]},
  80. });
  81. });
  82. it('gives updated query param when widget change submitted', () => {
  83. expect(legendFunctions.setMultipleWidgetSelectionStateURL(dashboard)).toEqual([
  84. `12345${WIDGET_ID_DELIMITER}Releases`,
  85. `23456${WIDGET_ID_DELIMITER}Releases`,
  86. ]);
  87. });
  88. });
  89. describe('legend naming', function () {
  90. let widget: Widget;
  91. beforeEach(() => {
  92. widget = {
  93. id: '12345',
  94. title: 'Test Query',
  95. displayType: DisplayType.AREA,
  96. widgetType: WidgetType.ERRORS,
  97. interval: '5m',
  98. queries: [
  99. {
  100. name: '',
  101. conditions: '',
  102. fields: ['count()', 'Releases'],
  103. aggregates: ['count()', 'Releases'],
  104. columns: [],
  105. orderby: '',
  106. },
  107. ],
  108. };
  109. });
  110. it('formats to query param format from selected', () => {
  111. expect(
  112. legendFunctions.encodeLegendQueryParam(widget, {[`Releases:${widget.id}`]: false})
  113. ).toEqual(`${widget.id}${WIDGET_ID_DELIMITER}Releases`);
  114. });
  115. it('formats to selected format from query param', () => {
  116. expect(legendFunctions.decodeLegendQueryParam(widget)).toEqual({
  117. [`Releases:${widget.id}`]: false,
  118. });
  119. });
  120. });
  121. });