convertBuilderStateToWidget.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import {DisplayType, WidgetType} from 'sentry/views/dashboards/types';
  2. import type {WidgetBuilderState} from 'sentry/views/dashboards/widgetBuilder/hooks/useWidgetBuilderState';
  3. import {convertBuilderStateToWidget} from 'sentry/views/dashboards/widgetBuilder/utils/convertBuilderStateToWidget';
  4. import {FieldValueKind} from 'sentry/views/discover/table/types';
  5. describe('convertBuilderStateToWidget', function () {
  6. it('returns the widget with the provided widget queries state', function () {
  7. const mockState: WidgetBuilderState = {
  8. title: 'Test Widget',
  9. description: 'Test Description',
  10. dataset: WidgetType.ERRORS,
  11. displayType: DisplayType.TABLE,
  12. limit: 5,
  13. fields: [
  14. {kind: 'field', field: 'geo.country'},
  15. {
  16. function: ['count', '', undefined, undefined],
  17. kind: 'function',
  18. },
  19. {
  20. function: ['count_unique', 'user', undefined, undefined],
  21. kind: 'function',
  22. },
  23. ],
  24. yAxis: [{kind: 'field', field: 'count()'}],
  25. };
  26. const widget = convertBuilderStateToWidget(mockState);
  27. expect(widget).toEqual({
  28. title: 'Test Widget',
  29. description: 'Test Description',
  30. widgetType: WidgetType.ERRORS,
  31. displayType: DisplayType.TABLE,
  32. interval: '1h',
  33. limit: 5,
  34. queries: [
  35. {
  36. fields: ['geo.country', 'count()', 'count_unique(user)'],
  37. fieldAliases: ['', '', ''],
  38. aggregates: ['count()'],
  39. columns: ['geo.country'],
  40. conditions: '',
  41. name: '',
  42. orderby: 'geo.country',
  43. },
  44. ],
  45. });
  46. });
  47. it('injects the orderby from the sort state into the widget queries', function () {
  48. const mockState: WidgetBuilderState = {
  49. query: ['transaction.duration:>100', 'transaction.duration:>50'],
  50. sort: [{field: 'geo.country', kind: 'desc'}],
  51. };
  52. const widget = convertBuilderStateToWidget(mockState);
  53. expect(widget.queries[0]!.orderby).toBe('-geo.country');
  54. expect(widget.queries[1]!.orderby).toBe('-geo.country');
  55. });
  56. it('does not convert aggregates to aliased format', function () {
  57. const mockState: WidgetBuilderState = {
  58. query: ['transaction.duration:>100', 'transaction.duration:>50'],
  59. sort: [{field: 'count()', kind: 'desc'}],
  60. };
  61. const widget = convertBuilderStateToWidget(mockState);
  62. expect(widget.queries[0]!.orderby).toBe('-count()');
  63. expect(widget.queries[1]!.orderby).toBe('-count()');
  64. });
  65. it('adds aliases to the widget queries', function () {
  66. const mockState: WidgetBuilderState = {
  67. fields: [
  68. {field: 'geo.country', alias: 'test', kind: FieldValueKind.FIELD},
  69. {field: 'geo.country', alias: undefined, kind: FieldValueKind.FIELD},
  70. {field: 'geo.country', alias: 'another one', kind: FieldValueKind.FIELD},
  71. ],
  72. };
  73. const widget = convertBuilderStateToWidget(mockState);
  74. expect(widget.queries[0]!.fieldAliases).toEqual(['test', '', 'another one']);
  75. });
  76. it('adds legend aliases to the widget queries', function () {
  77. const mockState: WidgetBuilderState = {
  78. legendAlias: ['test', 'test2'],
  79. query: ['transaction.duration:>100', 'transaction.duration:>50'],
  80. };
  81. const widget = convertBuilderStateToWidget(mockState);
  82. expect(widget.queries[0]!.name).toBe('test');
  83. expect(widget.queries[0]!.conditions).toBe('transaction.duration:>100');
  84. expect(widget.queries[1]!.name).toBe('test2');
  85. expect(widget.queries[1]!.conditions).toBe('transaction.duration:>50');
  86. });
  87. });