This repository has been archived by the owner on Nov 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
51 lines (47 loc) · 1.63 KB
/
index.test.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
const injectCssString = require('./index');
const ids = ['foo', 'bar'];
const icsIds = ids.map(id => 'ics-'+id);
const css = 'a{color:red}';
beforeEach(() => {
document.head.innerHTML = '';
});
describe('testing `injectCssString(css, id)`', () => {
test('style tag is added to the DOM', () => {
injectCssString(css, ids[0]);
expect(document.getElementById(icsIds[0])).toBeTruthy();
});
test('style tag contains the injected css', () => {
injectCssString(css, ids[0]);
expect(document.getElementById(icsIds[0]).innerHTML).toBe(css);
});
test('css string with same identifier cannot be injected twice', () => {
injectCssString(css, ids[0]);
injectCssString(css, ids[0]);
expect(document.getElementsByTagName('style').length).toBe(1);
});
test('css string with different identifier should be injected', () => {
injectCssString(css, ids[0]);
injectCssString(css, ids[1]);
expect(document.getElementsByTagName('style').length).toBe(2);
});
});
describe('testing `injectCssString(css)`', () => {
test('style tag is addded to the DOM', () => {
injectCssString(css);
expect(
Array.prototype.slice.call(
document.getElementsByTagName('style')
).find(el => el.innerHTML === css)
).not.toBe(null);
});
test('two calls with same css result in one style tag being addded to the DOM', () => {
injectCssString(css);
injectCssString(css);
expect(document.getElementsByTagName('style').length).toBe(1);
});
test('two calls with different css result in two style tags being addded to the DOM', () => {
injectCssString(css);
injectCssString(css+css);
expect(document.getElementsByTagName('style').length).toBe(2);
});
});