-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuse-network-connection.test.tsx
More file actions
59 lines (49 loc) · 1.87 KB
/
Copy pathuse-network-connection.test.tsx
File metadata and controls
59 lines (49 loc) · 1.87 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
import { render } from "@testing-library/react";
import { renderHook } from "@testing-library/react-hooks";
import {
NetworkConnection,
NetworkInformationUtils,
} from "andculturecode-javascript-core";
import React from "react";
import { NetworkConnectionProvider } from "../providers/network-connection-provider";
import { useNetworkConnection } from "./use-network-connection";
// -----------------------------------------------------------------------------------------
// #region Mocks
// -----------------------------------------------------------------------------------------
const getNetworkConnectionMock = jest.spyOn(
NetworkInformationUtils,
"getNetworkConnection"
);
// #endregion Mocks
describe("useNetworkConnection", () => {
describe("when used outside NetworkConnectionProvider", () => {
it("throws error", () => {
// Arrange & Act
const { result } = renderHook(() => useNetworkConnection());
// Assert
expect(result.error).toBeDefined();
});
});
describe("when used inside NetworkConnectionProvider", () => {
it("returns network connection information", () => {
// Arrange
let networkConnection: NetworkConnection;
const expectedNetworkConnection: NetworkConnection = {
isOnline: true,
};
getNetworkConnectionMock.mockReturnValue(expectedNetworkConnection);
const TestComponent = () => {
networkConnection = useNetworkConnection();
return <div></div>;
};
// Act
render(
<NetworkConnectionProvider>
<TestComponent />
</NetworkConnectionProvider>
);
// Assert
expect(networkConnection).toEqual(expectedNetworkConnection);
});
});
});