Skip to content

Commit d8b2444

Browse files
danielbariongabrieljablonski
authored andcommitted
chore: refactor CSS support logic and write tests for it
1 parent 0e7324d commit d8b2444

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-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: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { debounce, deepEqual, computeTooltipPosition, cssTimeToMs, clearTimeoutRef } from 'utils'
1+
import {
2+
debounce,
3+
deepEqual,
4+
computeTooltipPosition,
5+
cssTimeToMs,
6+
cssSupports,
7+
clearTimeoutRef,
8+
} from 'utils'
29
import { injectStyle } from 'utils/handle-style.ts'
310

411
describe('compute positions', () => {
@@ -299,3 +306,18 @@ describe('handleStyle', () => {
299306
expect(styleElement.innerHTML).toBe(`body { background: 'red' }`)
300307
})
301308
})
309+
310+
describe('check for CSS attribute support on current page', () => {
311+
test('CSS attribute should be supported in global Window', () => {
312+
const hasSupportToOpacity = cssSupports('opacity', '1')
313+
314+
expect(hasSupportToOpacity).toBe('1')
315+
})
316+
317+
test('CSS attribute should not be supported in global Window', () => {
318+
// not a valid css attribute
319+
const hasSupportToOpacity = cssSupports('lorem', '100')
320+
321+
expect(hasSupportToOpacity).toBe(false)
322+
})
323+
})

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)