2020-04-11 03:37:30 +08:00
|
|
|
import { patchProp } from '../src/patchProp'
|
2019-11-09 11:06:53 +08:00
|
|
|
|
2020-04-11 03:23:01 +08:00
|
|
|
describe(`runtime-dom: style patching`, () => {
|
2019-11-09 11:06:53 +08:00
|
|
|
it('string', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, 'color:red')
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
|
|
|
|
})
|
|
|
|
|
2020-06-12 05:25:39 +08:00
|
|
|
// #1309
|
|
|
|
it('should not patch same string style', () => {
|
|
|
|
const el = document.createElement('div')
|
|
|
|
const fn = jest.fn()
|
|
|
|
const value = (el.style.cssText = 'color:red;')
|
|
|
|
Object.defineProperty(el.style, 'cssText', {
|
|
|
|
get(): any {
|
|
|
|
return value
|
|
|
|
},
|
|
|
|
set: fn
|
|
|
|
})
|
|
|
|
patchProp(el, 'style', value, value)
|
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
|
|
|
|
expect(fn).not.toBeCalled()
|
|
|
|
})
|
|
|
|
|
2019-11-09 11:06:53 +08:00
|
|
|
it('plain object', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, { color: 'red' })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red;')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('camelCase', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, { marginRight: '10px' })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('margin-right:10px;')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('remove if falsy value', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', { color: 'red' }, { color: undefined })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('!important', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, { color: 'red !important' })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe('color:red!important;')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('camelCase with !important', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, { marginRight: '10px !important' })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.cssText.replace(/\s/g, '')).toBe(
|
|
|
|
'margin-right:10px!important;'
|
|
|
|
)
|
|
|
|
})
|
|
|
|
|
|
|
|
it('object with multiple entries', () => {
|
|
|
|
const el = document.createElement('div')
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el, 'style', {}, { color: 'red', marginRight: '10px' })
|
2019-11-09 11:06:53 +08:00
|
|
|
expect(el.style.getPropertyValue('color')).toBe('red')
|
|
|
|
expect(el.style.getPropertyValue('margin-right')).toBe('10px')
|
|
|
|
})
|
2019-11-09 11:38:04 +08:00
|
|
|
|
|
|
|
// JSDOM doesn't support custom properties on style object so we have to
|
|
|
|
// mock it here.
|
|
|
|
function mockElementWithStyle() {
|
|
|
|
const store: any = {}
|
|
|
|
return {
|
|
|
|
style: {
|
|
|
|
WebkitTransition: '',
|
|
|
|
setProperty(key: string, val: string) {
|
|
|
|
store[key] = val
|
|
|
|
},
|
|
|
|
getPropertyValue(key: string) {
|
|
|
|
return store[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
it('CSS custom properties', () => {
|
|
|
|
const el = mockElementWithStyle()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el as any, 'style', {}, { '--theme': 'red' } as any)
|
2019-11-09 11:38:04 +08:00
|
|
|
expect(el.style.getPropertyValue('--theme')).toBe('red')
|
|
|
|
})
|
|
|
|
|
|
|
|
it('auto vendor prefixing', () => {
|
|
|
|
const el = mockElementWithStyle()
|
2020-04-10 23:57:07 +08:00
|
|
|
patchProp(el as any, 'style', {}, { transition: 'all 1s' })
|
2019-11-09 11:38:04 +08:00
|
|
|
expect(el.style.WebkitTransition).toBe('all 1s')
|
|
|
|
})
|
2019-11-09 11:06:53 +08:00
|
|
|
})
|