-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
65 lines (56 loc) · 1.81 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import React from "react";
import { act } from 'react-dom/test-utils';
import { render, cleanup } from "react-testing-library";
import useTrackingIsLoaded from "./src/index.js";
function EffectfulComponent() {
const [status, error] = useTrackingIsLoaded();
return (
<>
<div>
{status ? "Loaded" : "Not Loaded"}
</div>
<div>
{error ? "Error" : null}
</div>
</>
);
}
window.requestAnimationFrame = fn => setTimeout(fn, 16);
window.cancelAnimationFrame = id => clearTimeout(id);
jest.useFakeTimers();
afterEach(() => {
cleanup();
window.ga = undefined;
});
test("it renders the Not loaded message", () => {
const { container } = render(<EffectfulComponent />);
const div = container.firstChild;
expect(div.textContent).toBe("Not Loaded");
});
test("it renders the loaded message if GA has been already loaded", () => {
window.ga = jest.fn();
const { container } = render(<EffectfulComponent />);
const div = container.firstChild;
expect(div.textContent).toBe("Loaded");
});
test("it renders the loaded message after GA has finished loading", () => {
const { container, rerender } = render(<EffectfulComponent />);
const div = container.firstChild;
expect(div.textContent).toBe("Not Loaded");
window.ga = jest.fn();
rerender(<EffectfulComponent />);
act(() => {
jest.runOnlyPendingTimers();
});
expect(div.textContent).toBe("Loaded");
});
test("it renders the error message if GA hasn't finished loading in time", () => {
const { container, rerender } = render(<EffectfulComponent />);
const div = container.firstChild;
const divError = container.lastChild;
expect(div.textContent).toBe("Not Loaded");
act(() => {
jest.runOnlyPendingTimers();
});
expect(divError.textContent).toBe("Error");
});