isSortable.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 { ControlStateMapping } from '@superset-ui/chart-controls';
  20. import { GenericDataType } from '@apache-superset/core/api/core';
  21. import { isSortable } from '../../src/utils/isSortable';
  22. const controls: ControlStateMapping = {
  23. datasource: {
  24. datasource: {
  25. columns: [
  26. { column_name: 'a', type_generic: GenericDataType.String },
  27. { column_name: 'b', type_generic: GenericDataType.Numeric },
  28. { column_name: 'c', type_generic: GenericDataType.Boolean },
  29. ],
  30. },
  31. type: 'Select',
  32. },
  33. };
  34. test('should return true if the column is forced to be categorical', () => {
  35. const c: ControlStateMapping = {
  36. ...controls,
  37. x_axis: { value: 'b', type: 'Select' },
  38. xAxisForceCategorical: { value: true, type: 'Checkbox' },
  39. };
  40. expect(isSortable(c)).toBe(true);
  41. });
  42. test('should return true if the column is a custom SQL column', () => {
  43. const c: ControlStateMapping = {
  44. ...controls,
  45. x_axis: {
  46. value: { label: 'custom_sql', sqlExpression: 'MAX(ID)' },
  47. type: 'Select',
  48. },
  49. };
  50. expect(isSortable(c)).toBe(true);
  51. });
  52. test('should return true if the column type is String or Boolean', () => {
  53. const c: ControlStateMapping = {
  54. ...controls,
  55. x_axis: { value: 'c', type: 'Checkbox' },
  56. };
  57. expect(isSortable(c)).toBe(true);
  58. });
  59. test('should return false if none of the conditions are met', () => {
  60. const c: ControlStateMapping = {
  61. ...controls,
  62. x_axis: { value: 'b', type: 'Input' },
  63. };
  64. expect(isSortable(c)).toBe(false);
  65. });