normalizeProp.spec.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. import {
  2. normalizeClass,
  3. normalizeProps,
  4. normalizeStyle,
  5. parseStringStyle,
  6. stringifyStyle,
  7. } from '../src'
  8. describe('normalizeClass', () => {
  9. test('handles undefined correctly', () => {
  10. expect(normalizeClass(undefined)).toEqual('')
  11. })
  12. test('handles string correctly', () => {
  13. expect(normalizeClass('foo')).toEqual('foo')
  14. })
  15. test('handles array correctly', () => {
  16. expect(normalizeClass(['foo', undefined, true, false, 'bar'])).toEqual(
  17. 'foo bar',
  18. )
  19. })
  20. test('handles string containing spaces correctly', () => {
  21. expect(normalizeClass('foo1 ')).toEqual('foo1')
  22. expect(normalizeClass(['foo ', ' baz '])).toEqual('foo baz')
  23. })
  24. test('handles empty array correctly', () => {
  25. expect(normalizeClass([])).toEqual('')
  26. })
  27. test('handles nested array correctly', () => {
  28. expect(normalizeClass(['foo', ['bar'], [['baz']]])).toEqual('foo bar baz')
  29. })
  30. test('handles object correctly', () => {
  31. expect(normalizeClass({ foo: true, bar: false, baz: true })).toEqual(
  32. 'foo baz',
  33. )
  34. })
  35. test('handles empty object correctly', () => {
  36. expect(normalizeClass({})).toEqual('')
  37. })
  38. test('handles arrays and objects correctly', () => {
  39. expect(
  40. normalizeClass(['foo', ['bar'], { baz: true }, [{ qux: true }]]),
  41. ).toEqual('foo bar baz qux')
  42. })
  43. test('handles array of objects with falsy values', () => {
  44. expect(
  45. normalizeClass([
  46. { foo: false },
  47. { bar: 0 },
  48. { baz: -0 },
  49. { qux: '' },
  50. { quux: null },
  51. { corge: undefined },
  52. { grault: NaN },
  53. ]),
  54. ).toEqual('')
  55. })
  56. test('handles array of objects with truthy values', () => {
  57. expect(
  58. normalizeClass([
  59. { foo: true },
  60. { bar: 'not-empty' },
  61. { baz: 1 },
  62. { qux: {} },
  63. { quux: [] },
  64. ]),
  65. ).toEqual('foo bar baz qux quux')
  66. })
  67. // #6777
  68. test('parse multi-line inline style', () => {
  69. expect(
  70. parseStringStyle(`border: 1px solid transparent;
  71. background: linear-gradient(white, white) padding-box,
  72. repeating-linear-gradient(
  73. -45deg,
  74. #ccc 0,
  75. #ccc 0.5em,
  76. white 0,
  77. white 0.75em
  78. );`),
  79. ).toMatchInlineSnapshot(`
  80. {
  81. "background": "linear-gradient(white, white) padding-box,
  82. repeating-linear-gradient(
  83. -45deg,
  84. #ccc 0,
  85. #ccc 0.5em,
  86. white 0,
  87. white 0.75em
  88. )",
  89. "border": "1px solid transparent",
  90. }
  91. `)
  92. })
  93. })
  94. describe('normalizeStyle', () => {
  95. test('handles string correctly', () => {
  96. expect(normalizeStyle('foo')).toEqual('foo')
  97. })
  98. test('handles array correctly', () => {
  99. const style: any = normalizeStyle([
  100. `border: 1px solid transparent;
  101. background: linear-gradient(white, white) padding-box,
  102. repeating-linear-gradient(
  103. -45deg,
  104. #ccc 0,
  105. #ccc 0.5em,
  106. white 0,
  107. white 0.75em
  108. );`,
  109. ])
  110. expect(style.border).toEqual('1px solid transparent')
  111. expect(style.background).toEqual(`linear-gradient(white, white) padding-box,
  112. repeating-linear-gradient(
  113. -45deg,
  114. #ccc 0,
  115. #ccc 0.5em,
  116. white 0,
  117. white 0.75em
  118. )`)
  119. })
  120. test('handles object correctly', () => {
  121. const styleObj = {
  122. border: '1px solid transparent',
  123. background: `linear-gradient(white, white) padding-box,
  124. repeating-linear-gradient(
  125. -45deg,
  126. #ccc 0,
  127. #ccc 0.5em,
  128. white 0,
  129. white 0.75em
  130. )`,
  131. }
  132. const style: any = normalizeStyle(styleObj)
  133. expect(style.border).toEqual(styleObj.border)
  134. expect(style.background).toEqual(styleObj.background)
  135. })
  136. })
  137. describe('stringifyStyle', () => {
  138. test('should return empty string for undefined', () => {
  139. expect(stringifyStyle(undefined)).toBe('')
  140. expect(stringifyStyle('')).toBe('')
  141. expect(stringifyStyle('color: blue;')).toBe('color: blue;')
  142. })
  143. test('should return valid CSS string for normalized style object', () => {
  144. const style = {
  145. color: 'blue',
  146. fontSize: '14px',
  147. backgroundColor: 'white',
  148. opacity: 0.8,
  149. margin: 0,
  150. '--custom-color': 'red',
  151. }
  152. expect(stringifyStyle(style)).toBe(
  153. 'color:blue;font-size:14px;background-color:white;opacity:0.8;margin:0;--custom-color:red;',
  154. )
  155. })
  156. test('should ignore non-string or non-number values in style object', () => {
  157. const style: any = {
  158. color: 'blue',
  159. fontSize: '14px',
  160. lineHeight: true,
  161. padding: null,
  162. margin: undefined,
  163. }
  164. const expected = 'color:blue;font-size:14px;'
  165. expect(stringifyStyle(style)).toBe(expected)
  166. })
  167. })
  168. describe('normalizeProps', () => {
  169. test('should return null when props is null', () => {
  170. const props = null
  171. const result = normalizeProps(props)
  172. expect(result).toBeNull()
  173. })
  174. test('should normalize class prop when it is an array', () => {
  175. const props = {
  176. class: ['class1', 'class2'],
  177. }
  178. const result = normalizeProps(props)
  179. expect(result).toEqual({
  180. class: 'class1 class2',
  181. })
  182. })
  183. test('should normalize class prop when it is an object', () => {
  184. const props = {
  185. class: {
  186. class1: true,
  187. class2: false,
  188. class3: true,
  189. },
  190. }
  191. const result = normalizeProps(props)
  192. expect(result).toEqual({
  193. class: 'class1 class3',
  194. })
  195. })
  196. test('should normalize style prop', () => {
  197. const props = {
  198. style: ['color: blue', 'font-size: 14px'],
  199. }
  200. const result = normalizeProps(props)
  201. expect(result).toEqual({
  202. style: {
  203. color: 'blue',
  204. 'font-size': '14px',
  205. },
  206. })
  207. })
  208. })