You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

theme-test.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import * as ThemeHelper from '../../helpers/theme';
  21. import { lightTheme } from '../../theme';
  22. const props = {
  23. color: 'rgb(0,0,0)',
  24. };
  25. describe('getProp', () => {
  26. it('should work', () => {
  27. expect(ThemeHelper.getProp('color')(props)).toEqual('rgb(0,0,0)');
  28. });
  29. });
  30. describe('themeColor', () => {
  31. it('should work for light theme', () => {
  32. expect(ThemeHelper.themeColor('backgroundPrimary')({ theme: lightTheme })).toEqual(
  33. 'rgb(252,252,253)',
  34. );
  35. });
  36. it('should work with a theme-defined opacity', () => {
  37. expect(ThemeHelper.themeColor('bannerIconHover')({ theme: lightTheme })).toEqual(
  38. 'rgba(217,45,32,0.2)',
  39. );
  40. });
  41. it('should work for all kind of color parameters', () => {
  42. expect(ThemeHelper.themeColor('transparent')({ theme: lightTheme })).toEqual('transparent');
  43. expect(ThemeHelper.themeColor('currentColor')({ theme: lightTheme })).toEqual('currentColor');
  44. expect(ThemeHelper.themeColor('var(--test)')({ theme: lightTheme })).toEqual('var(--test)');
  45. expect(ThemeHelper.themeColor('rgb(0,0,0)')({ theme: lightTheme })).toEqual('rgb(0,0,0)');
  46. expect(ThemeHelper.themeColor('rgba(0,0,0,1)')({ theme: lightTheme })).toEqual('rgba(0,0,0,1)');
  47. expect(
  48. ThemeHelper.themeColor(ThemeHelper.themeContrast('backgroundPrimary')({ theme: lightTheme }))(
  49. {
  50. theme: lightTheme,
  51. },
  52. ),
  53. ).toEqual('rgb(8,9,12)');
  54. expect(
  55. ThemeHelper.themeColor(ThemeHelper.themeAvatarColor('luke')({ theme: lightTheme }))({
  56. theme: lightTheme,
  57. }),
  58. ).toEqual('rgb(209,215,254)');
  59. });
  60. });
  61. describe('themeContrast', () => {
  62. it('should work for light theme', () => {
  63. expect(ThemeHelper.themeContrast('backgroundPrimary')({ theme: lightTheme })).toEqual(
  64. 'rgb(8,9,12)',
  65. );
  66. });
  67. it('should work for all kind of color parameters', () => {
  68. expect(ThemeHelper.themeContrast('var(--test)')({ theme: lightTheme })).toEqual('var(--test)');
  69. expect(ThemeHelper.themeContrast('rgb(0,0,0)')({ theme: lightTheme })).toEqual('rgb(0,0,0)');
  70. expect(ThemeHelper.themeContrast('rgba(0,0,0,1)')({ theme: lightTheme })).toEqual(
  71. 'rgba(0,0,0,1)',
  72. );
  73. expect(
  74. ThemeHelper.themeContrast(ThemeHelper.themeColor('backgroundPrimary')({ theme: lightTheme }))(
  75. {
  76. theme: lightTheme,
  77. },
  78. ),
  79. ).toEqual('rgb(252,252,253)');
  80. expect(
  81. ThemeHelper.themeContrast(ThemeHelper.themeAvatarColor('luke')({ theme: lightTheme }))({
  82. theme: lightTheme,
  83. }),
  84. ).toEqual('rgb(209,215,254)');
  85. expect(
  86. ThemeHelper.themeContrast('backgroundPrimary')({
  87. theme: {
  88. ...lightTheme,
  89. contrasts: { ...lightTheme.contrasts, backgroundPrimary: 'inherit' },
  90. },
  91. }),
  92. ).toEqual('inherit');
  93. });
  94. });
  95. describe('themeBorder', () => {
  96. it('should work for light theme', () => {
  97. expect(ThemeHelper.themeBorder()({ theme: lightTheme })).toEqual('1px solid rgb(235,235,235)');
  98. });
  99. it('should allow to override the color of the border', () => {
  100. expect(ThemeHelper.themeBorder('focus', 'primaryLight')({ theme: lightTheme })).toEqual(
  101. '4px solid rgba(123,135,217,0.2)',
  102. );
  103. });
  104. it('should allow to override the opacity of the border', () => {
  105. expect(ThemeHelper.themeBorder('focus', undefined, 0.5)({ theme: lightTheme })).toEqual(
  106. '4px solid rgba(197,205,223,0.5)',
  107. );
  108. });
  109. it('should allow to pass a CSS prop as color name', () => {
  110. expect(
  111. ThemeHelper.themeBorder('focus', 'var(--outlineColor)', 0.5)({ theme: lightTheme }),
  112. ).toEqual('4px solid var(--outlineColor)');
  113. });
  114. });
  115. describe('themeShadow', () => {
  116. it('should work for light theme', () => {
  117. expect(ThemeHelper.themeShadow('xs')({ theme: lightTheme })).toEqual(
  118. '0px 1px 2px 0px rgba(29,33,47,0.05)',
  119. );
  120. });
  121. it('should allow to override the color of the shadow', () => {
  122. expect(ThemeHelper.themeShadow('xs', 'backgroundPrimary')({ theme: lightTheme })).toEqual(
  123. '0px 1px 2px 0px rgba(252,252,253,0.05)',
  124. );
  125. expect(ThemeHelper.themeShadow('xs', 'transparent')({ theme: lightTheme })).toEqual(
  126. '0px 1px 2px 0px transparent',
  127. );
  128. });
  129. it('should allow to override the opacity of the shadow', () => {
  130. expect(ThemeHelper.themeShadow('xs', 'backgroundPrimary', 0.8)({ theme: lightTheme })).toEqual(
  131. '0px 1px 2px 0px rgba(252,252,253,0.8)',
  132. );
  133. });
  134. it('should allow to pass a CSS prop as color name', () => {
  135. expect(ThemeHelper.themeShadow('xs', 'var(--shadowColor)')({ theme: lightTheme })).toEqual(
  136. '0px 1px 2px 0px var(--shadowColor)',
  137. );
  138. });
  139. });