-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathuse-script.test.tsx
More file actions
174 lines (133 loc) · 5.69 KB
/
use-script.test.tsx
File metadata and controls
174 lines (133 loc) · 5.69 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import { act, renderHook } from '@testing-library/react-hooks';
import useScript from '../use-script';
describe('useScript', () => {
beforeEach(() => {
const html = document.querySelector('html');
if (html) {
html.innerHTML = '';
}
});
it('should append a script tag', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const { result } = renderHook(() =>
useScript({ src: 'http://scriptsrc/' }),
);
const [loading, error] = result.current;
expect(loading).toBe(true);
expect(error).toBeNull();
const script = document.querySelector('script');
expect(script).not.toBeNull();
if (script) {
expect(script.getAttribute('src')).toEqual('http://scriptsrc/');
}
});
it('should append a script tag with attributes', async () => {
expect(document.querySelectorAll('script').length).toBe(0);
const { result } = renderHook(() =>
useScript({
src: 'http://scriptsrc/',
'data-test': 'test',
async: true,
}),
);
const [loading, error] = result.current;
expect(loading).toBe(true);
expect(error).toBeNull();
const script = document.querySelector('script');
expect(script).not.toBeNull();
if (script) {
expect(script.getAttribute('src')).toEqual('http://scriptsrc/');
expect(script.getAttribute('data-test')).toEqual('test');
expect(script.getAttribute('async')).toBe('true');
}
});
it('should render a script only once', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const props = { src: 'http://scriptsrc/' };
const handle = renderHook((p) => useScript(p), {
initialProps: props,
});
expect(document.querySelectorAll('script').length).toBe(1);
handle.rerender();
expect(document.querySelectorAll('script').length).toBe(1);
});
it('should set loading false on load', async () => {
const props = { src: 'http://scriptsrc/' };
const handle = renderHook((p) => useScript(p), {
initialProps: props,
});
const [loading, error] = handle.result.current;
expect(loading).toBe(true);
expect(error).toBe(null);
act(() => {
const el = document.querySelector('script');
if (el) {
el.dispatchEvent(new Event('load'));
}
});
const [loadingAfter, errorAfter] = handle.result.current;
expect(loadingAfter).toBe(false);
expect(errorAfter).toBe(null);
});
it('should not cause issues on unmount', async () => {
const props = { src: 'http://scriptsrc/' };
const handle = renderHook((p) => useScript(p), {
initialProps: props,
});
handle.unmount();
act(() => {
const el = document.querySelector('script');
if (el) {
el.dispatchEvent(new Event('load'));
}
});
});
it('should check for script existing on the page before rendering when checkForExisting is true', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const previousScript = document.createElement('script');
previousScript.src = 'http://scriptsrc/';
document.body.appendChild(previousScript);
expect(document.querySelectorAll('script').length).toBe(1);
const props = { src: 'http://scriptsrc/', checkForExisting: true };
const handle = renderHook((p) => useScript(p), {
initialProps: props,
});
expect(document.querySelectorAll('script').length).toBe(1);
handle.rerender();
expect(document.querySelectorAll('script').length).toBe(1);
});
it('should not check for script existing on the page before rendering when checkForExisting is not set', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const previousScript = document.createElement('script');
previousScript.src = 'http://scriptsrc/';
document.body.appendChild(previousScript);
expect(document.querySelectorAll('script').length).toBe(1);
const props = { src: 'http://scriptsrc/' };
const handle = renderHook((p) => useScript(p), {
initialProps: props,
});
expect(document.querySelectorAll('script').length).toBe(2);
handle.rerender();
expect(document.querySelectorAll('script').length).toBe(2);
});
it('should handle null src and not append a script tag', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const { result } = renderHook(() => useScript({ src: null }));
const [loading, error] = result.current;
expect(loading).toBe(false);
expect(error).toBeNull();
expect(document.querySelectorAll('script').length).toBe(0);
});
it('should NOT set loading to true if checkForExisting finds the script', () => {
expect(document.querySelectorAll('script').length).toBe(0);
const previousScript = document.createElement('script');
previousScript.src = 'http://scriptsrc/';
document.body.appendChild(previousScript);
expect(document.querySelectorAll('script').length).toBe(1);
const props = { src: 'http://scriptsrc/', checkForExisting: true };
const { result } = renderHook(() => useScript(props));
const [loading] = result.all[0] as Array<any>;
expect(loading).toEqual(false);
expect(document.querySelectorAll('script').length).toBe(1);
});
});