displayTimeRelatedControls.test.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. import { VizType } from '@superset-ui/core';
  20. import { displayTimeRelatedControls } from '../../src';
  21. const mockData = {
  22. actions: {
  23. setDatasource: jest.fn(),
  24. },
  25. controls: {
  26. x_axis: {
  27. type: 'SelectControl' as const,
  28. value: 'not_temporal',
  29. options: [
  30. { column_name: 'not_temporal', is_dttm: false },
  31. { column_name: 'ds', is_dttm: true },
  32. ],
  33. },
  34. },
  35. exportState: {},
  36. form_data: {
  37. datasource: '22__table',
  38. viz_type: VizType.Table,
  39. },
  40. };
  41. test('returns true when no x-axis exists', () => {
  42. expect(
  43. displayTimeRelatedControls({
  44. ...mockData,
  45. controls: {
  46. control_options: {
  47. type: 'SelectControl',
  48. value: 'not_temporal',
  49. options: [],
  50. },
  51. },
  52. }),
  53. ).toBeTruthy();
  54. });
  55. test('returns false when x-axis value is not temporal', () => {
  56. expect(displayTimeRelatedControls(mockData)).toBeFalsy();
  57. });
  58. test('returns true when x-axis value is temporal', () => {
  59. expect(
  60. displayTimeRelatedControls({
  61. ...mockData,
  62. controls: {
  63. x_axis: {
  64. ...mockData.controls.x_axis,
  65. value: 'ds',
  66. },
  67. },
  68. }),
  69. ).toBeTruthy();
  70. });
  71. test('returns false when x-axis value without options', () => {
  72. expect(
  73. displayTimeRelatedControls({
  74. ...mockData,
  75. controls: {
  76. x_axis: {
  77. type: 'SelectControl' as const,
  78. value: 'not_temporal',
  79. },
  80. },
  81. }),
  82. ).toBeFalsy();
  83. });
  84. test('returns true when x-axis is ad-hoc column', () => {
  85. expect(
  86. displayTimeRelatedControls({
  87. ...mockData,
  88. controls: {
  89. x_axis: {
  90. ...mockData.controls.x_axis,
  91. value: {
  92. sqlExpression: 'ds',
  93. label: 'ds',
  94. expressionType: 'SQL',
  95. },
  96. },
  97. },
  98. }),
  99. ).toBeTruthy();
  100. });
  101. test('returns false when the x-axis is neither an ad-hoc column nor a physical column', () => {
  102. expect(
  103. displayTimeRelatedControls({
  104. ...mockData,
  105. controls: {
  106. x_axis: {
  107. ...mockData.controls.x_axis,
  108. value: {},
  109. },
  110. },
  111. }),
  112. ).toBeFalsy();
  113. });