Skip to content

Commit 620f379

Browse files
authored
fix: added mock for useKeyboardContext (#361)
## 📜 Description Added mock for `useKeyboardContext` + `AndroidSoftInputModes`. ## 💡 Motivation and Context `useKeyboardContext` and `AndroidSoftInputModes` is a part of official API of this library. However they are not exported from the library (so if you use them in unit-test -> this test will not pass and you will have to mock it manually). So in this PR I'm adding them to mock and also adding a new unit test which assures that new mocks are actually present. ## 📢 Changelog ### JS - added mock for `useKeyboardContext`; - added mock for `AndroidSoftInputModes`. ## 🤔 How Has This Been Tested? Tested manually via newly added test. ## 📸 Screenshots (if appropriate): <img width="670" alt="image" src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/b3942088-344b-4f81-b64c-ea7e90be5c77"> ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent 7d2d0a2 commit 620f379

7 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useFocusEffect } from "@react-navigation/native";
2+
import { renderHook } from "@testing-library/react-native";
3+
import { useCallback } from "react";
4+
import {
5+
AndroidSoftInputModes,
6+
KeyboardController,
7+
useKeyboardContext,
8+
} from "react-native-keyboard-controller";
9+
10+
jest.mock("@react-navigation/native", () => ({
11+
useFocusEffect: jest.fn().mockImplementation((cb) => cb()),
12+
}));
13+
14+
function useKeyboardAnimation() {
15+
useFocusEffect(
16+
useCallback(() => {
17+
KeyboardController.setInputMode(
18+
AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE,
19+
);
20+
21+
return () => KeyboardController.setDefaultMode();
22+
}, []),
23+
);
24+
25+
const context = useKeyboardContext();
26+
27+
return context.animated;
28+
}
29+
30+
describe("custom hook creation", () => {
31+
it("should render without errors", () => {
32+
const { result } = renderHook(() => useKeyboardAnimation());
33+
34+
expect(result).toBeDefined();
35+
});
36+
});

FabricExample/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"@react-navigation/native": "^6.1.7",
1717
"@react-navigation/native-stack": "^6.9.13",
1818
"@react-navigation/stack": "^6.3.17",
19+
"@testing-library/react-hooks": "^8.0.1",
1920
"lottie-react-native": "^6.1.2",
2021
"react": "18.2.0",
2122
"react-native": "0.72.4",

FabricExample/yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,13 @@
12581258
dependencies:
12591259
regenerator-runtime "^0.13.11"
12601260

1261+
"@babel/runtime@^7.12.5":
1262+
version "7.23.9"
1263+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.9.tgz#47791a15e4603bb5f905bc0753801cf21d6345f7"
1264+
integrity sha512-0CX6F+BI2s9dkUqr08KFrAIZgNFj75rdBU/DjCyYLIaV/quFjkk6T+EJ2LkZHyZTbEV4L5p97mNkUsHl2wLFAw==
1265+
dependencies:
1266+
regenerator-runtime "^0.14.0"
1267+
12611268
"@babel/runtime@^7.20.0":
12621269
version "7.20.13"
12631270
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.13.tgz#7055ab8a7cff2b8f6058bf6ae45ff84ad2aded4b"
@@ -2054,6 +2061,14 @@
20542061
pretty-format "^29.0.3"
20552062
redent "^3.0.0"
20562063

2064+
"@testing-library/react-hooks@^8.0.1":
2065+
version "8.0.1"
2066+
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
2067+
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
2068+
dependencies:
2069+
"@babel/runtime" "^7.12.5"
2070+
react-error-boundary "^3.1.0"
2071+
20572072
"@testing-library/react-native@^11.5.0":
20582073
version "11.5.4"
20592074
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-11.5.4.tgz#5c57c0c5afc3f1960ff491aba68f66adc899e1cc"
@@ -5176,6 +5191,13 @@ react-devtools-core@^4.27.2:
51765191
shell-quote "^1.6.1"
51775192
ws "^7"
51785193

5194+
react-error-boundary@^3.1.0:
5195+
version "3.1.4"
5196+
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
5197+
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
5198+
dependencies:
5199+
"@babel/runtime" "^7.12.5"
5200+
51795201
react-freeze@^1.0.0:
51805202
version "1.0.3"
51815203
resolved "https://registry.yarnpkg.com/react-freeze/-/react-freeze-1.0.3.tgz#5e3ca90e682fed1d73a7cb50c2c7402b3e85618d"
@@ -5372,6 +5394,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.2:
53725394
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
53735395
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
53745396

5397+
regenerator-runtime@^0.14.0:
5398+
version "0.14.1"
5399+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
5400+
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==
5401+
53755402
regenerator-transform@^0.15.1:
53765403
version "0.15.1"
53775404
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.15.1.tgz#f6c4e99fc1b4591f780db2586328e4d9a9d8dc56"
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { useFocusEffect } from "@react-navigation/native";
2+
import { renderHook } from "@testing-library/react-native";
3+
import { useCallback } from "react";
4+
import {
5+
AndroidSoftInputModes,
6+
KeyboardController,
7+
useKeyboardContext,
8+
} from "react-native-keyboard-controller";
9+
10+
jest.mock("@react-navigation/native", () => ({
11+
useFocusEffect: jest.fn().mockImplementation((cb) => cb()),
12+
}));
13+
14+
function useKeyboardAnimation() {
15+
useFocusEffect(
16+
useCallback(() => {
17+
KeyboardController.setInputMode(
18+
AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE,
19+
);
20+
21+
return () => KeyboardController.setDefaultMode();
22+
}, []),
23+
);
24+
25+
const context = useKeyboardContext();
26+
27+
return context.animated;
28+
}
29+
30+
describe("custom hook creation", () => {
31+
it("should render without errors", () => {
32+
const { result } = renderHook(() => useKeyboardAnimation());
33+
34+
expect(result).toBeDefined();
35+
});
36+
});

example/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"@react-navigation/native": "^6.1.7",
1818
"@react-navigation/native-stack": "^6.9.13",
1919
"@react-navigation/stack": "^6.3.17",
20+
"@testing-library/react-hooks": "^8.0.1",
2021
"lottie-react-native": "^6.1.2",
2122
"react": "18.2.0",
2223
"react-native": "0.72.4",

example/yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,13 @@
11641164
dependencies:
11651165
regenerator-runtime "^0.13.11"
11661166

1167+
"@babel/runtime@^7.12.5":
1168+
version "7.23.9"
1169+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.9.tgz#47791a15e4603bb5f905bc0753801cf21d6345f7"
1170+
integrity sha512-0CX6F+BI2s9dkUqr08KFrAIZgNFj75rdBU/DjCyYLIaV/quFjkk6T+EJ2LkZHyZTbEV4L5p97mNkUsHl2wLFAw==
1171+
dependencies:
1172+
regenerator-runtime "^0.14.0"
1173+
11671174
"@babel/runtime@^7.20.0":
11681175
version "7.20.13"
11691176
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.13.tgz#7055ab8a7cff2b8f6058bf6ae45ff84ad2aded4b"
@@ -1924,6 +1931,14 @@
19241931
pretty-format "^29.0.3"
19251932
redent "^3.0.0"
19261933

1934+
"@testing-library/react-hooks@^8.0.1":
1935+
version "8.0.1"
1936+
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
1937+
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
1938+
dependencies:
1939+
"@babel/runtime" "^7.12.5"
1940+
react-error-boundary "^3.1.0"
1941+
19271942
"@testing-library/react-native@^11.5.0":
19281943
version "11.5.0"
19291944
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-11.5.0.tgz#b043c5db7b15eca42a65e95d3f3ae196fab9493b"
@@ -5108,6 +5123,13 @@ react-devtools-core@^4.27.2:
51085123
shell-quote "^1.6.1"
51095124
ws "^7"
51105125

5126+
react-error-boundary@^3.1.0:
5127+
version "3.1.4"
5128+
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
5129+
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
5130+
dependencies:
5131+
"@babel/runtime" "^7.12.5"
5132+
51115133
react-freeze@^1.0.0:
51125134
version "1.0.3"
51135135
resolved "https://registry.yarnpkg.com/react-freeze/-/react-freeze-1.0.3.tgz#5e3ca90e682fed1d73a7cb50c2c7402b3e85618d"
@@ -5309,6 +5331,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.2:
53095331
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
53105332
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==
53115333

5334+
regenerator-runtime@^0.14.0:
5335+
version "0.14.1"
5336+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
5337+
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==
5338+
53125339
regenerator-transform@^0.15.1:
53135340
version "0.15.1"
53145341
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.15.1.tgz#f6c4e99fc1b4591f780db2586328e4d9a9d8dc56"

jest/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ const mock = {
3434
useResizeMode: jest.fn(),
3535
useGenericKeyboardHandler: jest.fn(),
3636
useKeyboardHandler: jest.fn(),
37+
useKeyboardContext: jest.fn().mockReturnValue(values),
3738
/// input
3839
useReanimatedFocusedInput: jest.fn().mockReturnValue(focusedInput),
3940
useFocusedInputHandler: jest.fn(),
@@ -47,6 +48,22 @@ const mock = {
4748
setDefaultMode: jest.fn(),
4849
dismiss: jest.fn(),
4950
},
51+
AndroidSoftInputModes: {
52+
SOFT_INPUT_ADJUST_NOTHING: 48,
53+
SOFT_INPUT_ADJUST_PAN: 32,
54+
SOFT_INPUT_ADJUST_RESIZE: 16,
55+
SOFT_INPUT_ADJUST_UNSPECIFIED: 0,
56+
SOFT_INPUT_IS_FORWARD_NAVIGATION: 256,
57+
SOFT_INPUT_MASK_ADJUST: 240,
58+
SOFT_INPUT_MASK_STATE: 15,
59+
SOFT_INPUT_MODE_CHANGED: 512,
60+
SOFT_INPUT_STATE_ALWAYS_HIDDEN: 3,
61+
SOFT_INPUT_STATE_ALWAYS_VISIBLE: 5,
62+
SOFT_INPUT_STATE_HIDDEN: 2,
63+
SOFT_INPUT_STATE_UNCHANGED: 1,
64+
SOFT_INPUT_STATE_UNSPECIFIED: 0,
65+
SOFT_INPUT_STATE_VISIBLE: 4,
66+
},
5067
KeyboardEvents: {
5168
addListener: jest.fn(() => ({ remove: jest.fn() })),
5269
},

0 commit comments

Comments
 (0)