isDerivedSeries.test.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 { isDerivedSeries } from '@superset-ui/chart-controls';
  20. import { SqlaFormData, ComparisonType, VizType } from '@superset-ui/core';
  21. const formData: SqlaFormData = {
  22. datasource: 'foo',
  23. viz_type: VizType.Table,
  24. };
  25. const series = {
  26. id: 'metric__1 month ago',
  27. name: 'metric__1 month ago',
  28. data: [100],
  29. };
  30. test('should be false if comparison type is not actual values', () => {
  31. expect(isDerivedSeries(series, formData)).toEqual(false);
  32. Object.keys(ComparisonType)
  33. .filter(type => type === ComparisonType.Values)
  34. .forEach(type => {
  35. const formDataWithComparisonType = {
  36. ...formData,
  37. comparison_type: type,
  38. time_compare: ['1 month ago'],
  39. };
  40. expect(isDerivedSeries(series, formDataWithComparisonType)).toEqual(
  41. false,
  42. );
  43. });
  44. });
  45. test('should be true if comparison type is values', () => {
  46. const formDataWithActualTypes = {
  47. ...formData,
  48. comparison_type: ComparisonType.Values,
  49. time_compare: ['1 month ago', '1 month later'],
  50. };
  51. expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(true);
  52. });
  53. test('should be false if series name does not match time_compare', () => {
  54. const arbitrary_series = {
  55. id: 'arbitrary column',
  56. name: 'arbitrary column',
  57. data: [100],
  58. };
  59. const formDataWithActualTypes = {
  60. ...formData,
  61. comparison_type: ComparisonType.Values,
  62. time_compare: ['1 month ago', '1 month later'],
  63. };
  64. expect(isDerivedSeries(arbitrary_series, formDataWithActualTypes)).toEqual(
  65. false,
  66. );
  67. });
  68. test('should be false if time compare is not suffix', () => {
  69. const series = {
  70. id: '1 month ago__metric',
  71. name: '1 month ago__metric',
  72. data: [100],
  73. };
  74. const formDataWithActualTypes = {
  75. ...formData,
  76. comparison_type: ComparisonType.Values,
  77. time_compare: ['1 month ago', '1 month later'],
  78. };
  79. expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(false);
  80. });
  81. test('should be false if series name invalid', () => {
  82. const series = {
  83. id: 123,
  84. name: 123,
  85. data: [100],
  86. };
  87. const formDataWithActualTypes = {
  88. ...formData,
  89. comparison_type: ComparisonType.Values,
  90. time_compare: ['1 month ago', '1 month later'],
  91. };
  92. expect(isDerivedSeries(series, formDataWithActualTypes)).toEqual(false);
  93. });