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.

utils.spec.ts 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { AbstractNode } from './utils'
  2. import { generate, normalizeAttrs } from './utils'
  3. import { render } from '@testing-library/react'
  4. import '@testing-library/jest-dom'
  5. describe('generate icon base utils', () => {
  6. describe('normalizeAttrs', () => {
  7. it('should normalize class to className', () => {
  8. const attrs = { class: 'test-class' }
  9. const result = normalizeAttrs(attrs)
  10. expect(result).toEqual({ className: 'test-class' })
  11. })
  12. it('should normalize style string to style object', () => {
  13. const attrs = { style: 'color:red;font-size:14px;' }
  14. const result = normalizeAttrs(attrs)
  15. expect(result).toEqual({ style: { color: 'red', fontSize: '14px' } })
  16. })
  17. it('should handle attributes with dashes and colons', () => {
  18. const attrs = { 'data-test': 'value', 'xlink:href': 'url' }
  19. const result = normalizeAttrs(attrs)
  20. expect(result).toEqual({ dataTest: 'value', xlinkHref: 'url' })
  21. })
  22. })
  23. describe('generate', () => {
  24. it('should generate React elements from AbstractNode', () => {
  25. const node: AbstractNode = {
  26. name: 'div',
  27. attributes: { class: 'container' },
  28. children: [
  29. {
  30. name: 'span',
  31. attributes: { style: 'color:blue;' },
  32. children: [],
  33. },
  34. ],
  35. }
  36. const { container } = render(generate(node, 'key'))
  37. // to svg element
  38. expect(container.firstChild).toHaveClass('container')
  39. expect(container.querySelector('span')).toHaveStyle({ color: 'blue' })
  40. })
  41. // add not has children
  42. it('should generate React elements without children', () => {
  43. const node: AbstractNode = {
  44. name: 'div',
  45. attributes: { class: 'container' },
  46. }
  47. const { container } = render(generate(node, 'key'))
  48. // to svg element
  49. expect(container.firstChild).toHaveClass('container')
  50. })
  51. it('should merge rootProps when provided', () => {
  52. const node: AbstractNode = {
  53. name: 'div',
  54. attributes: { class: 'container' },
  55. children: [],
  56. }
  57. const rootProps = { id: 'root' }
  58. const { container } = render(generate(node, 'key', rootProps))
  59. expect(container.querySelector('div')).toHaveAttribute('id', 'root')
  60. })
  61. })
  62. })