Skip to content

Commit 4fefbe4

Browse files
committed
chore: refactor CSS support logic and write tests for it
1 parent 3dc9499 commit 4fefbe4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/test/jest-setup.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react'
2-
// whatever else you need in here
32

43
global.React = React
54

@@ -8,3 +7,13 @@ global.ResizeObserver = jest.fn().mockImplementation(() => ({
87
unobserve: jest.fn(),
98
disconnect: jest.fn(),
109
}))
10+
11+
global.CSS = {
12+
supports: (key, value) => {
13+
if (key === 'opacity') {
14+
return value
15+
}
16+
17+
return false
18+
},
19+
}

src/test/utils.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { debounce, deepEqual, computeTooltipPosition, cssTimeToMs } from 'utils'
1+
import { debounce, deepEqual, computeTooltipPosition, cssTimeToMs, cssSupports } from 'utils'
22
import { injectStyle } from 'utils/handle-style.ts'
33

44
describe('compute positions', () => {
@@ -283,3 +283,18 @@ describe('handleStyle', () => {
283283
expect(styleElement.innerHTML).toBe(`body { background: 'red' }`)
284284
})
285285
})
286+
287+
describe('check for CSS attribute support on current page', () => {
288+
test('CSS attribute should be supported in global Window', () => {
289+
const hasSupportToOpacity = cssSupports('opacity', '1')
290+
291+
expect(hasSupportToOpacity).toBe('1')
292+
})
293+
294+
test('CSS attribute should not be supported in global Window', () => {
295+
// not a valid css attribute
296+
const hasSupportToOpacity = cssSupports('lorem', '100')
297+
298+
expect(hasSupportToOpacity).toBe(false)
299+
})
300+
})

src/utils/css-supports.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1+
/**
2+
*
3+
* @param property CSS attribute
4+
* @param value Value of the CSS Attribute
5+
* @returns The value of the CSS Attribute is returned if it is supported, otherwise false
6+
*/
17
const cssSupports = (property: string, value: string): boolean => {
28
const hasCssSupports = 'CSS' in window && 'supports' in window.CSS
3-
return hasCssSupports ? window.CSS.supports(property, value) : true
9+
return hasCssSupports ? window.CSS.supports(property, value) : false
410
}
511

612
export default cssSupports

0 commit comments

Comments
 (0)