Skip to content

Commit 566aa47

Browse files
Issue #24 Backfill tests for Hooks/useWindow
Issue #24 - Add useWindow tests
2 parents 049276f + 447f00d commit 566aa47

4 files changed

Lines changed: 99 additions & 3 deletions

File tree

jest.config.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ module.exports = {
99
modulePathIgnorePatterns: ["<rootDir>/dist"],
1010
preset: "ts-jest",
1111
roots: ["<rootDir>/src"],
12-
setupFilesAfterEnv: ["<rootDir>/src/setupTests.ts"],
12+
setupFilesAfterEnv: [
13+
"<rootDir>/src/setupTests.ts",
14+
// polyfill window.resizeTo
15+
"window-resizeto/polyfill",
16+
],
1317
testEnvironment: "jsdom",
1418
};

package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"devDependencies": {
2525
"@testing-library/jest-dom": "5.5.0",
2626
"@testing-library/react": "10.0.4",
27+
"@testing-library/react-hooks": "3.4.2",
2728
"@types/faker": "4.1.12",
2829
"@types/jest": "25.1.5",
2930
"@types/node": "13.11.0",
@@ -35,14 +36,16 @@
3536
"jest-extended": "0.11.5",
3637
"jest-fetch-mock": "3.0.3",
3738
"prettier": "1.19.1",
39+
"react-test-renderer": "16.9.0",
3840
"rimraf": "2.6.2",
3941
"rosie": "2.0.1",
4042
"ts-jest": "25.5.1",
4143
"tslint": "6.1.2",
4244
"tslint-config-prettier": "1.18.0",
4345
"typedoc": "0.17.6",
4446
"typedoc-plugin-markdown": "2.2.17",
45-
"typescript": "3.8.3"
47+
"typescript": "3.8.3",
48+
"window-resizeto": "0.0.2"
4649
},
4750
"files": [
4851
"dist"

src/hooks/use-window.test.tsx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1+
import { renderHook } from "@testing-library/react-hooks";
2+
import { useWindow } from "./use-window";
3+
4+
const DEFAULT_WIDTH: number = 1024;
5+
const DEFAULT_HEIGHT: number = 768;
6+
17
describe("useWindow", () => {
2-
test.skip("TODO - https://github.com/AndcultureCode/AndcultureCode.JavaScript.React/issues/24", () => {});
8+
beforeEach(() => {
9+
window.resizeTo(DEFAULT_WIDTH, DEFAULT_HEIGHT);
10+
});
11+
12+
test("returns width and height of window", async () => {
13+
// Arrange & Act
14+
const { result } = renderHook(() => useWindow());
15+
16+
// Assert
17+
expect(result.current.width).toBe(DEFAULT_WIDTH);
18+
expect(result.current.height).toBe(DEFAULT_HEIGHT);
19+
});
20+
21+
test("when window resize event is triggered, returns new width and height of window", async () => {
22+
// Arrange
23+
const { result } = renderHook(() => useWindow());
24+
const windowWidth = DEFAULT_WIDTH + 1;
25+
const windowHeight = DEFAULT_HEIGHT + 1;
26+
27+
// Act
28+
window.resizeTo(windowWidth, windowHeight);
29+
30+
// Assert
31+
expect(result.current.width).toBe(windowWidth);
32+
expect(result.current.height).toBe(windowHeight);
33+
});
334
});

0 commit comments

Comments
 (0)