convertBuilderStateToWidget.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import {getDatasetConfig} from 'sentry/views/dashboards/datasetConfig/base';
  2. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  3. import type {WidgetBuilderState} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  4. import {convertBuilderStateToWidget} from 'sentry/views/dashboards/widgetBuilder/utils/convertBuilderStateToWidget';
  5. describe('convertBuilderStateToWidget', function () {
  6. it('returns the default of the dataset config when no widget queries state is provided', function () {
  7. const mockState: WidgetBuilderState = {
  8. title: 'Test Widget',
  9. description: 'Test Description',
  10. dataset: WidgetType.ERRORS,
  11. displayType: DisplayType.TABLE,
  12. fields: [],
  13. yAxis: [],
  14. };
  15. const widget = convertBuilderStateToWidget(mockState);
  16. expect(widget).toEqual({
  17. title: 'Test Widget',
  18. description: 'Test Description',
  19. widgetType: WidgetType.ERRORS,
  20. displayType: DisplayType.TABLE,
  21. interval: '1h',
  22. queries: [
  23. {
  24. ...getDatasetConfig(WidgetType.ERRORS).defaultWidgetQuery,
  25. },
  26. ],
  27. });
  28. });
  29. it('returns the default of the dataset config when no widget queries state is provided - issues', function () {
  30. const mockState: WidgetBuilderState = {
  31. title: 'Test Issues Widget',
  32. description: 'test description for an issues widget',
  33. dataset: WidgetType.ISSUE,
  34. displayType: DisplayType.TABLE,
  35. fields: [],
  36. yAxis: [],
  37. };
  38. const widget = convertBuilderStateToWidget(mockState);
  39. expect(widget).toEqual({
  40. title: 'Test Issues Widget',
  41. description: 'test description for an issues widget',
  42. widgetType: WidgetType.ISSUE,
  43. displayType: DisplayType.TABLE,
  44. interval: '1h',
  45. queries: [
  46. {
  47. ...getDatasetConfig(WidgetType.ISSUE).defaultWidgetQuery,
  48. },
  49. ],
  50. });
  51. });
  52. it('returns the widget with the provided widget queries state', function () {
  53. const mockState: WidgetBuilderState = {
  54. title: 'Test Widget',
  55. description: 'Test Description',
  56. dataset: WidgetType.ERRORS,
  57. displayType: DisplayType.TABLE,
  58. fields: [
  59. {kind: 'field', field: 'geo.country'},
  60. {
  61. function: ['count', '', undefined, undefined],
  62. kind: 'function',
  63. },
  64. {
  65. function: ['count_unique', 'user', undefined, undefined],
  66. kind: 'function',
  67. },
  68. ],
  69. yAxis: [{kind: 'field', field: 'count()'}],
  70. };
  71. const widget = convertBuilderStateToWidget(mockState);
  72. expect(widget).toEqual({
  73. title: 'Test Widget',
  74. description: 'Test Description',
  75. widgetType: WidgetType.ERRORS,
  76. displayType: DisplayType.TABLE,
  77. interval: '1h',
  78. queries: [
  79. {
  80. fields: ['geo.country', 'count()', 'count_unique(user)'],
  81. fieldAliases: [],
  82. aggregates: ['count()'],
  83. columns: ['geo.country'],
  84. conditions: '',
  85. name: '',
  86. orderby: 'geo.country',
  87. },
  88. ],
  89. });
  90. });
  91. it('injects the orderby from the sort state into the widget queries', function () {
  92. const mockState: WidgetBuilderState = {
  93. query: ['transaction.duration:>100', 'transaction.duration:>50'],
  94. sort: [{field: 'geo.country', kind: 'desc'}],
  95. };
  96. const widget = convertBuilderStateToWidget(mockState);
  97. expect(widget.queries[0].orderby).toEqual('-geo.country');
  98. expect(widget.queries[1].orderby).toEqual('-geo.country');
  99. });
  100. });