/
utils.js
118 lines (104 loc) · 2.82 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import {
deprecate,
checkHtmlElement,
HtmlElementTypeError,
parseJStoCSS,
} from '../utils'
import document from './helpers/document'
test('deprecate', () => {
const spy = jest.spyOn(console, 'warn').mockImplementation(() => {})
const name = 'test'
const replacement = 'test'
const message = `Warning: ${name} has been deprecated and will be removed in future updates.`
deprecate(name, replacement)
expect(spy).toHaveBeenCalledWith(message, replacement)
deprecate(name)
expect(spy).toHaveBeenCalledWith(message, undefined)
spy.mockRestore()
})
describe('checkHtmlElement', () => {
it('does not throw an error for correct html element', () => {
expect(() => {
const element = document.createElement('p')
checkHtmlElement(element, () => {}, {})
}).not.toThrow()
})
it('does not throw an error for correct svg element', () => {
expect(() => {
const element = document.createElementNS(
'http://www.w3.org/2000/svg',
'rect',
)
checkHtmlElement(element, () => {}, {})
}).not.toThrow()
})
it('does not throw for body', () => {
expect(() => {
checkHtmlElement(document.body, () => {}, {})
}).not.toThrow()
})
it('throws for undefined', () => {
expect(() => {
checkHtmlElement(undefined, () => {}, {})
}).toThrow(HtmlElementTypeError)
})
it('throws for document', () => {
expect(() => {
checkHtmlElement(document, () => {}, {})
}).toThrow(HtmlElementTypeError)
})
it('throws for function', () => {
expect(() => {
checkHtmlElement(
() => {},
() => {},
{},
)
}).toThrow(HtmlElementTypeError)
})
it('throws for almost element-like objects', () => {
class FakeObject {}
expect(() => {
checkHtmlElement(
{
ownerDocument: {
defaultView: {HTMLElement: FakeObject, SVGElement: FakeObject},
},
},
() => {},
{},
)
}).toThrow(HtmlElementTypeError)
})
})
describe('parseJStoCSS', () => {
describe('when all the styles are valid', () => {
it('returns the JS parsed as CSS text', () => {
expect(
parseJStoCSS(document, {
backgroundColor: 'blue',
height: '100%',
}),
).toBe('background-color: blue; height: 100%;')
})
})
describe('when some style is invalid', () => {
it('returns the JS parsed as CSS text without the invalid style', () => {
expect(
parseJStoCSS(document, {
backgroundColor: 'blue',
whatever: 'anything',
}),
).toBe('background-color: blue;')
})
})
describe('when all the styles are invalid', () => {
it('returns an empty string', () => {
expect(
parseJStoCSS(document, {
whatever: 'anything',
}),
).toBe('')
})
})
})