annotation.test.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 {
  20. AnnotationLayer,
  21. AnnotationOpacity,
  22. AnnotationSourceType,
  23. AnnotationStyle,
  24. AnnotationType,
  25. AxisType,
  26. DataRecord,
  27. FormulaAnnotationLayer,
  28. TimeseriesDataRecord,
  29. } from '@superset-ui/core';
  30. import {
  31. evalFormula,
  32. extractAnnotationLabels,
  33. formatAnnotationLabel,
  34. parseAnnotationOpacity,
  35. } from '../../src/utils/annotation';
  36. describe('formatAnnotationLabel', () => {
  37. it('should handle default cases properly', () => {
  38. expect(formatAnnotationLabel('name')).toEqual('name');
  39. expect(formatAnnotationLabel('name', 'title')).toEqual('name - title');
  40. expect(formatAnnotationLabel('name', 'title', ['description'])).toEqual(
  41. 'name - title\n\ndescription',
  42. );
  43. });
  44. it('should handle missing cases properly', () => {
  45. expect(formatAnnotationLabel()).toEqual('');
  46. expect(formatAnnotationLabel(undefined, 'title')).toEqual('title');
  47. expect(formatAnnotationLabel('name', undefined, ['description'])).toEqual(
  48. 'name\n\ndescription',
  49. );
  50. expect(
  51. formatAnnotationLabel(undefined, undefined, ['description']),
  52. ).toEqual('description');
  53. });
  54. it('should handle multiple descriptions properly', () => {
  55. expect(
  56. formatAnnotationLabel('name', 'title', [
  57. 'description 1',
  58. 'description 2',
  59. ]),
  60. ).toEqual('name - title\n\ndescription 1\ndescription 2');
  61. expect(
  62. formatAnnotationLabel(undefined, undefined, [
  63. 'description 1',
  64. 'description 2',
  65. ]),
  66. ).toEqual('description 1\ndescription 2');
  67. });
  68. });
  69. describe('extractForecastSeriesContext', () => {
  70. it('should extract the correct series name and type', () => {
  71. expect(parseAnnotationOpacity(AnnotationOpacity.Low)).toEqual(0.2);
  72. expect(parseAnnotationOpacity(AnnotationOpacity.Medium)).toEqual(0.5);
  73. expect(parseAnnotationOpacity(AnnotationOpacity.High)).toEqual(0.8);
  74. expect(parseAnnotationOpacity(AnnotationOpacity.Undefined)).toEqual(1);
  75. expect(parseAnnotationOpacity(undefined)).toEqual(1);
  76. });
  77. });
  78. describe('extractAnnotationLabels', () => {
  79. it('should extract all annotations that can be added to the legend', () => {
  80. const layers: AnnotationLayer[] = [
  81. {
  82. annotationType: AnnotationType.Formula,
  83. name: 'My Formula',
  84. show: true,
  85. style: AnnotationStyle.Solid,
  86. value: 'sin(x)',
  87. showLabel: true,
  88. },
  89. {
  90. annotationType: AnnotationType.Formula,
  91. name: 'My Hidden Formula',
  92. show: false,
  93. style: AnnotationStyle.Solid,
  94. value: 'sin(2x)',
  95. showLabel: true,
  96. },
  97. {
  98. annotationType: AnnotationType.Interval,
  99. name: 'My Interval',
  100. sourceType: AnnotationSourceType.Table,
  101. show: true,
  102. style: AnnotationStyle.Solid,
  103. value: 1,
  104. showLabel: true,
  105. },
  106. {
  107. annotationType: AnnotationType.Timeseries,
  108. name: 'My Line',
  109. show: true,
  110. style: AnnotationStyle.Dashed,
  111. sourceType: AnnotationSourceType.Line,
  112. value: 1,
  113. showLabel: true,
  114. },
  115. {
  116. annotationType: AnnotationType.Timeseries,
  117. name: 'My Hidden Line',
  118. show: false,
  119. style: AnnotationStyle.Dashed,
  120. sourceType: AnnotationSourceType.Line,
  121. value: 1,
  122. showLabel: true,
  123. },
  124. ];
  125. expect(extractAnnotationLabels(layers)).toEqual(['My Formula', 'My Line']);
  126. });
  127. });
  128. describe('evalFormula', () => {
  129. const layer: FormulaAnnotationLayer = {
  130. annotationType: AnnotationType.Formula,
  131. name: 'My Formula',
  132. show: true,
  133. style: AnnotationStyle.Solid,
  134. value: 'x+1',
  135. showLabel: true,
  136. };
  137. it('Should evaluate a regular formula', () => {
  138. const data: TimeseriesDataRecord[] = [
  139. { __timestamp: 0 },
  140. { __timestamp: 10 },
  141. ];
  142. expect(evalFormula(layer, data, '__timestamp', AxisType.Time)).toEqual([
  143. [0, 1],
  144. [10, 11],
  145. ]);
  146. });
  147. it('Should evaluate a formula containing redundant characters', () => {
  148. const data: TimeseriesDataRecord[] = [
  149. { __timestamp: 0 },
  150. { __timestamp: 10 },
  151. ];
  152. expect(
  153. evalFormula(
  154. { ...layer, value: 'y = x* 2 -1' },
  155. data,
  156. '__timestamp',
  157. AxisType.Time,
  158. ),
  159. ).toEqual([
  160. [0, -1],
  161. [10, 19],
  162. ]);
  163. });
  164. it('Should evaluate a formula if axis type is category', () => {
  165. const data: DataRecord[] = [{ gender: 'boy' }, { gender: 'girl' }];
  166. expect(
  167. evalFormula(
  168. { ...layer, value: 'y = 1000' },
  169. data,
  170. 'gender',
  171. AxisType.Category,
  172. ),
  173. ).toEqual([
  174. ['boy', 1000],
  175. ['girl', 1000],
  176. ]);
  177. });
  178. });