computeMaxFontSize.test.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { computeMaxFontSize } from '@superset-ui/core';
  20. import { addDummyFill, removeDummyFill, SAMPLE_TEXT } from './getBBoxDummyFill';
  21. describe('computeMaxFontSize(input)', () => {
  22. describe('returns dimension of the given text', () => {
  23. beforeEach(addDummyFill);
  24. afterEach(removeDummyFill);
  25. it('requires either idealFontSize or maxHeight', () => {
  26. expect(() => {
  27. computeMaxFontSize({
  28. text: SAMPLE_TEXT[0],
  29. });
  30. }).toThrow();
  31. });
  32. it('computes maximum font size for given maxWidth and maxHeight', () => {
  33. expect(
  34. computeMaxFontSize({
  35. maxWidth: 400,
  36. maxHeight: 30,
  37. text: 'sample text',
  38. }),
  39. ).toEqual(30);
  40. });
  41. it('computes maximum font size for given idealFontSize and maxHeight', () => {
  42. expect(
  43. computeMaxFontSize({
  44. maxHeight: 20,
  45. idealFontSize: 40,
  46. text: SAMPLE_TEXT[0],
  47. }),
  48. ).toEqual(20);
  49. });
  50. it('computes maximum font size for given maxWidth and idealFontSize', () => {
  51. expect(
  52. computeMaxFontSize({
  53. maxWidth: 250,
  54. idealFontSize: 40,
  55. text: SAMPLE_TEXT[0],
  56. }),
  57. ).toEqual(25);
  58. });
  59. it('ensure idealFontSize is used if the maximum font size calculation goes below zero', () => {
  60. expect(
  61. computeMaxFontSize({
  62. maxWidth: 5,
  63. idealFontSize: 34,
  64. text: SAMPLE_TEXT[0],
  65. }),
  66. ).toEqual(34);
  67. });
  68. });
  69. });