getTextDimension.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 { promiseTimeout, getTextDimension } from '@superset-ui/core';
  20. import { addDummyFill, removeDummyFill, SAMPLE_TEXT } from './getBBoxDummyFill';
  21. describe('getTextDimension(input)', () => {
  22. describe('returns default dimension if getBBox() is not available', () => {
  23. it('returns default value for default dimension', () => {
  24. expect(
  25. getTextDimension({
  26. text: SAMPLE_TEXT[0],
  27. }),
  28. ).toEqual({
  29. height: 20,
  30. width: 100,
  31. });
  32. });
  33. it('return specified value if specified', () => {
  34. expect(
  35. getTextDimension(
  36. {
  37. text: SAMPLE_TEXT[0],
  38. },
  39. {
  40. height: 30,
  41. width: 400,
  42. },
  43. ),
  44. ).toEqual({
  45. height: 30,
  46. width: 400,
  47. });
  48. });
  49. });
  50. describe('returns dimension of the given text', () => {
  51. beforeEach(addDummyFill);
  52. afterEach(removeDummyFill);
  53. it('takes text as argument', () => {
  54. expect(
  55. getTextDimension({
  56. text: SAMPLE_TEXT[0],
  57. }),
  58. ).toEqual({
  59. height: 20,
  60. width: 200,
  61. });
  62. });
  63. it('accepts provided class via className', () => {
  64. expect(
  65. getTextDimension({
  66. text: SAMPLE_TEXT[0],
  67. className: 'test-class',
  68. }),
  69. ).toEqual({
  70. height: 20,
  71. width: 100, // width is 100 after adding class
  72. });
  73. });
  74. it('accepts provided style.font', () => {
  75. expect(
  76. getTextDimension({
  77. text: SAMPLE_TEXT[0],
  78. style: {
  79. font: 'italic 700 30px Lobster',
  80. },
  81. }),
  82. ).toEqual({
  83. height: 30, // 20 * (30/20) [fontSize=30]
  84. width: 1125, // 250 [fontFamily=Lobster] * (30/20) [fontSize=30] * 1.5 [fontStyle=italic] * 2 [fontWeight=700]
  85. });
  86. });
  87. it('accepts provided style.fontFamily', () => {
  88. expect(
  89. getTextDimension({
  90. text: SAMPLE_TEXT[0],
  91. style: {
  92. fontFamily: 'Lobster',
  93. },
  94. }),
  95. ).toEqual({
  96. height: 20,
  97. width: 250, // (250 [fontFamily=Lobster]
  98. });
  99. });
  100. it('accepts provided style.fontSize', () => {
  101. expect(
  102. getTextDimension({
  103. text: SAMPLE_TEXT[0],
  104. style: {
  105. fontSize: '40px',
  106. },
  107. }),
  108. ).toEqual({
  109. height: 40, // 20 [baseHeight] * (40/20) [fontSize=40]
  110. width: 400, // 200 [baseWidth] * (40/20) [fontSize=40]
  111. });
  112. });
  113. it('accepts provided style.fontStyle', () => {
  114. expect(
  115. getTextDimension({
  116. text: SAMPLE_TEXT[0],
  117. style: {
  118. fontStyle: 'italic',
  119. },
  120. }),
  121. ).toEqual({
  122. height: 20,
  123. width: 300, // 200 [baseWidth] * 1.5 [fontStyle=italic]
  124. });
  125. });
  126. it('accepts provided style.fontWeight', () => {
  127. expect(
  128. getTextDimension({
  129. text: SAMPLE_TEXT[0],
  130. style: {
  131. fontWeight: 700,
  132. },
  133. }),
  134. ).toEqual({
  135. height: 20,
  136. width: 400, // 200 [baseWidth] * 2 [fontWeight=700]
  137. });
  138. });
  139. it('accepts provided style.letterSpacing', () => {
  140. expect(
  141. getTextDimension({
  142. text: SAMPLE_TEXT[0],
  143. style: {
  144. letterSpacing: '1.1',
  145. },
  146. }),
  147. ).toEqual({
  148. height: 20,
  149. width: 221, // Ceiling(200 [baseWidth] * 1.1 [letterSpacing=1.1])
  150. });
  151. });
  152. it('handle empty text', () => {
  153. expect(
  154. getTextDimension({
  155. text: '',
  156. }),
  157. ).toEqual({
  158. height: 0,
  159. width: 0,
  160. });
  161. });
  162. });
  163. it('cleans up DOM', async () => {
  164. getTextDimension({
  165. text: SAMPLE_TEXT[0],
  166. });
  167. expect(document.querySelectorAll('svg')).toHaveLength(1);
  168. await promiseTimeout(() => {}, 501);
  169. expect(document.querySelector('svg')).toBeNull();
  170. });
  171. });