ChartProps.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 { Behavior, ChartProps, supersetTheme } from '@superset-ui/core';
  20. const RAW_FORM_DATA = {
  21. some_field: 1,
  22. };
  23. const RAW_DATASOURCE = {
  24. column_formats: { test: '%s' },
  25. };
  26. const QUERY_DATA = { data: {} };
  27. const QUERIES_DATA = [QUERY_DATA];
  28. const BEHAVIORS = [Behavior.NativeFilter, Behavior.InteractiveChart];
  29. describe('ChartProps', () => {
  30. it('exists', () => {
  31. expect(ChartProps).toBeDefined();
  32. });
  33. describe('new ChartProps({})', () => {
  34. it('returns new instance', () => {
  35. const props = new ChartProps({
  36. width: 800,
  37. height: 600,
  38. formData: RAW_FORM_DATA,
  39. queriesData: QUERIES_DATA,
  40. theme: supersetTheme,
  41. });
  42. expect(props).toBeInstanceOf(ChartProps);
  43. });
  44. it('processes formData and datasource to convert field names to camelCase', () => {
  45. const props = new ChartProps({
  46. width: 800,
  47. height: 600,
  48. datasource: RAW_DATASOURCE,
  49. formData: RAW_FORM_DATA,
  50. queriesData: QUERIES_DATA,
  51. theme: supersetTheme,
  52. });
  53. expect(props.formData.someField as number).toEqual(1);
  54. expect(props.datasource.columnFormats).toEqual(
  55. RAW_DATASOURCE.column_formats,
  56. );
  57. expect(props.rawFormData).toEqual(RAW_FORM_DATA);
  58. expect(props.rawDatasource).toEqual(RAW_DATASOURCE);
  59. });
  60. });
  61. describe('ChartProps.createSelector()', () => {
  62. const selector = ChartProps.createSelector();
  63. it('returns a selector function', () => {
  64. expect(selector).toBeInstanceOf(Function);
  65. });
  66. it('selector returns previous chartProps if all input fields do not change', () => {
  67. const props1 = selector({
  68. width: 800,
  69. height: 600,
  70. datasource: RAW_DATASOURCE,
  71. formData: RAW_FORM_DATA,
  72. queriesData: QUERIES_DATA,
  73. behaviors: BEHAVIORS,
  74. isRefreshing: false,
  75. theme: supersetTheme,
  76. });
  77. const props2 = selector({
  78. width: 800,
  79. height: 600,
  80. datasource: RAW_DATASOURCE,
  81. formData: RAW_FORM_DATA,
  82. queriesData: QUERIES_DATA,
  83. behaviors: BEHAVIORS,
  84. isRefreshing: false,
  85. theme: supersetTheme,
  86. });
  87. expect(props1).toBe(props2);
  88. });
  89. it('selector returns a new chartProps if the 13th field changes', () => {
  90. /** this test is here to test for selectors that exceed 12 arguments (
  91. * isRefreshing is the 13th argument, which is missing TS declarations).
  92. * See: https://github.com/reduxjs/reselect/issues/378
  93. */
  94. const props1 = selector({
  95. width: 800,
  96. height: 600,
  97. datasource: RAW_DATASOURCE,
  98. formData: RAW_FORM_DATA,
  99. queriesData: QUERIES_DATA,
  100. behaviors: BEHAVIORS,
  101. isRefreshing: false,
  102. theme: supersetTheme,
  103. });
  104. const props2 = selector({
  105. width: 800,
  106. height: 600,
  107. datasource: RAW_DATASOURCE,
  108. formData: RAW_FORM_DATA,
  109. queriesData: QUERIES_DATA,
  110. behaviors: BEHAVIORS,
  111. isRefreshing: true,
  112. theme: supersetTheme,
  113. });
  114. expect(props1).not.toBe(props2);
  115. });
  116. it('selector returns a new chartProps if some input fields change and returns memoized chart props', () => {
  117. const props1 = selector({
  118. width: 800,
  119. height: 600,
  120. datasource: RAW_DATASOURCE,
  121. formData: RAW_FORM_DATA,
  122. queriesData: QUERIES_DATA,
  123. theme: supersetTheme,
  124. });
  125. const props2 = selector({
  126. width: 800,
  127. height: 600,
  128. datasource: RAW_DATASOURCE,
  129. formData: { new_field: 3 },
  130. queriesData: QUERIES_DATA,
  131. theme: supersetTheme,
  132. });
  133. const props3 = selector({
  134. width: 800,
  135. height: 600,
  136. datasource: RAW_DATASOURCE,
  137. formData: RAW_FORM_DATA,
  138. queriesData: QUERIES_DATA,
  139. theme: supersetTheme,
  140. });
  141. expect(props1).not.toBe(props2);
  142. expect(props1).toBe(props3);
  143. });
  144. });
  145. });