style.spec.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import { patchStyle } from '../../src/modules/style'
  2. describe(`module style`, () => {
  3. it('string', () => {
  4. const el = document.createElement('div')
  5. patchStyle(el, {}, 'color:red')
  6. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  7. })
  8. it('plain object', () => {
  9. const el = document.createElement('div')
  10. patchStyle(el, {}, { color: 'red' })
  11. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
  12. })
  13. it('camelCase', () => {
  14. const el = document.createElement('div')
  15. patchStyle(el, {}, { marginRight: '10px' })
  16. expect(el.style.cssText.replace(/\s/g, '')).toBe('margin-right:10px;')
  17. })
  18. it('remove if falsy value', () => {
  19. const el = document.createElement('div')
  20. patchStyle(el, { color: 'red' }, { color: null })
  21. expect(el.style.cssText.replace(/\s/g, '')).toBe('')
  22. })
  23. it('!important', () => {
  24. const el = document.createElement('div')
  25. patchStyle(el, {}, { color: 'red !important' })
  26. expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red!important;')
  27. })
  28. it('camelCase with !important', () => {
  29. const el = document.createElement('div')
  30. patchStyle(el, {}, { marginRight: '10px !important' })
  31. expect(el.style.cssText.replace(/\s/g, '')).toBe(
  32. 'margin-right:10px!important;'
  33. )
  34. })
  35. it('object with multiple entries', () => {
  36. const el = document.createElement('div')
  37. patchStyle(el, {}, { color: 'red', marginRight: '10px' })
  38. expect(el.style.getPropertyValue('color')).toBe('red')
  39. expect(el.style.getPropertyValue('margin-right')).toBe('10px')
  40. })
  41. })