style.spec.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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: undefined })
  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. // JSDOM doesn't support custom properties on style object so we have to
  42. // mock it here.
  43. function mockElementWithStyle() {
  44. const store: any = {}
  45. return {
  46. style: {
  47. WebkitTransition: '',
  48. setProperty(key: string, val: string) {
  49. store[key] = val
  50. },
  51. getPropertyValue(key: string) {
  52. return store[key]
  53. }
  54. }
  55. }
  56. }
  57. it('CSS custom properties', () => {
  58. const el = mockElementWithStyle()
  59. patchStyle(el as any, {}, { '--theme': 'red' } as any)
  60. expect(el.style.getPropertyValue('--theme')).toBe('red')
  61. })
  62. it('auto vendor prefixing', () => {
  63. const el = mockElementWithStyle()
  64. patchStyle(el as any, {}, { transition: 'all 1s' })
  65. expect(el.style.WebkitTransition).toBe('all 1s')
  66. })
  67. })