Skip to content

Commit

Permalink
fix: added mock for useKeyboardContext (#361)
Browse files Browse the repository at this point in the history
## 📜 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
  • Loading branch information
kirillzyusko authored Feb 18, 2024
1 parent 7d2d0a2 commit 620f379
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 0 deletions.
36 changes: 36 additions & 0 deletions FabricExample/__tests__/custom-hook.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useFocusEffect } from "@react-navigation/native";
import { renderHook } from "@testing-library/react-native";
import { useCallback } from "react";
import {
AndroidSoftInputModes,
KeyboardController,
useKeyboardContext,
} from "react-native-keyboard-controller";

jest.mock("@react-navigation/native", () => ({
useFocusEffect: jest.fn().mockImplementation((cb) => cb()),
}));

function useKeyboardAnimation() {
useFocusEffect(
useCallback(() => {
KeyboardController.setInputMode(
AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE,
);

return () => KeyboardController.setDefaultMode();
}, []),
);

const context = useKeyboardContext();

return context.animated;
}

describe("custom hook creation", () => {
it("should render without errors", () => {
const { result } = renderHook(() => useKeyboardAnimation());

expect(result).toBeDefined();
});
});
1 change: 1 addition & 0 deletions FabricExample/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@react-navigation/native": "^6.1.7",
"@react-navigation/native-stack": "^6.9.13",
"@react-navigation/stack": "^6.3.17",
"@testing-library/react-hooks": "^8.0.1",
"lottie-react-native": "^6.1.2",
"react": "18.2.0",
"react-native": "0.72.4",
Expand Down
27 changes: 27 additions & 0 deletions FabricExample/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,13 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.12.5":
version "7.23.9"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.9.tgz#47791a15e4603bb5f905bc0753801cf21d6345f7"
integrity sha512-0CX6F+BI2s9dkUqr08KFrAIZgNFj75rdBU/DjCyYLIaV/quFjkk6T+EJ2LkZHyZTbEV4L5p97mNkUsHl2wLFAw==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.20.0":
version "7.20.13"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.13.tgz#7055ab8a7cff2b8f6058bf6ae45ff84ad2aded4b"
Expand Down Expand Up @@ -2054,6 +2061,14 @@
pretty-format "^29.0.3"
redent "^3.0.0"

"@testing-library/react-hooks@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
dependencies:
"@babel/runtime" "^7.12.5"
react-error-boundary "^3.1.0"

"@testing-library/react-native@^11.5.0":
version "11.5.4"
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-11.5.4.tgz#5c57c0c5afc3f1960ff491aba68f66adc899e1cc"
Expand Down Expand Up @@ -5176,6 +5191,13 @@ react-devtools-core@^4.27.2:
shell-quote "^1.6.1"
ws "^7"

react-error-boundary@^3.1.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
dependencies:
"@babel/runtime" "^7.12.5"

react-freeze@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/react-freeze/-/react-freeze-1.0.3.tgz#5e3ca90e682fed1d73a7cb50c2c7402b3e85618d"
Expand Down Expand Up @@ -5372,6 +5394,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.2:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regenerator-transform@^0.15.1:
version "0.15.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.15.1.tgz#f6c4e99fc1b4591f780db2586328e4d9a9d8dc56"
Expand Down
36 changes: 36 additions & 0 deletions example/__tests__/custom-hook.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { useFocusEffect } from "@react-navigation/native";
import { renderHook } from "@testing-library/react-native";
import { useCallback } from "react";
import {
AndroidSoftInputModes,
KeyboardController,
useKeyboardContext,
} from "react-native-keyboard-controller";

jest.mock("@react-navigation/native", () => ({
useFocusEffect: jest.fn().mockImplementation((cb) => cb()),
}));

function useKeyboardAnimation() {
useFocusEffect(
useCallback(() => {
KeyboardController.setInputMode(
AndroidSoftInputModes.SOFT_INPUT_ADJUST_RESIZE,
);

return () => KeyboardController.setDefaultMode();
}, []),
);

const context = useKeyboardContext();

return context.animated;
}

describe("custom hook creation", () => {
it("should render without errors", () => {
const { result } = renderHook(() => useKeyboardAnimation());

expect(result).toBeDefined();
});
});
1 change: 1 addition & 0 deletions example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"@react-navigation/native": "^6.1.7",
"@react-navigation/native-stack": "^6.9.13",
"@react-navigation/stack": "^6.3.17",
"@testing-library/react-hooks": "^8.0.1",
"lottie-react-native": "^6.1.2",
"react": "18.2.0",
"react-native": "0.72.4",
Expand Down
27 changes: 27 additions & 0 deletions example/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1164,6 +1164,13 @@
dependencies:
regenerator-runtime "^0.13.11"

"@babel/runtime@^7.12.5":
version "7.23.9"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.23.9.tgz#47791a15e4603bb5f905bc0753801cf21d6345f7"
integrity sha512-0CX6F+BI2s9dkUqr08KFrAIZgNFj75rdBU/DjCyYLIaV/quFjkk6T+EJ2LkZHyZTbEV4L5p97mNkUsHl2wLFAw==
dependencies:
regenerator-runtime "^0.14.0"

"@babel/runtime@^7.20.0":
version "7.20.13"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.20.13.tgz#7055ab8a7cff2b8f6058bf6ae45ff84ad2aded4b"
Expand Down Expand Up @@ -1924,6 +1931,14 @@
pretty-format "^29.0.3"
redent "^3.0.0"

"@testing-library/react-hooks@^8.0.1":
version "8.0.1"
resolved "https://registry.yarnpkg.com/@testing-library/react-hooks/-/react-hooks-8.0.1.tgz#0924bbd5b55e0c0c0502d1754657ada66947ca12"
integrity sha512-Aqhl2IVmLt8IovEVarNDFuJDVWVvhnr9/GCU6UUnrYXwgDFF9h2L2o2P9KBni1AST5sT6riAyoukFLyjQUgD/g==
dependencies:
"@babel/runtime" "^7.12.5"
react-error-boundary "^3.1.0"

"@testing-library/react-native@^11.5.0":
version "11.5.0"
resolved "https://registry.yarnpkg.com/@testing-library/react-native/-/react-native-11.5.0.tgz#b043c5db7b15eca42a65e95d3f3ae196fab9493b"
Expand Down Expand Up @@ -5108,6 +5123,13 @@ react-devtools-core@^4.27.2:
shell-quote "^1.6.1"
ws "^7"

react-error-boundary@^3.1.0:
version "3.1.4"
resolved "https://registry.yarnpkg.com/react-error-boundary/-/react-error-boundary-3.1.4.tgz#255db92b23197108757a888b01e5b729919abde0"
integrity sha512-uM9uPzZJTF6wRQORmSrvOIgt4lJ9MC1sNgEOj2XGsDTRE4kmpWxg7ENK9EWNKJRMAOY9z0MuF4yIfl6gp4sotA==
dependencies:
"@babel/runtime" "^7.12.5"

react-freeze@^1.0.0:
version "1.0.3"
resolved "https://registry.yarnpkg.com/react-freeze/-/react-freeze-1.0.3.tgz#5e3ca90e682fed1d73a7cb50c2c7402b3e85618d"
Expand Down Expand Up @@ -5309,6 +5331,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.2:
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regenerator-runtime@^0.14.0:
version "0.14.1"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==

regenerator-transform@^0.15.1:
version "0.15.1"
resolved "https://registry.yarnpkg.com/regenerator-transform/-/regenerator-transform-0.15.1.tgz#f6c4e99fc1b4591f780db2586328e4d9a9d8dc56"
Expand Down
17 changes: 17 additions & 0 deletions jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const mock = {
useResizeMode: jest.fn(),
useGenericKeyboardHandler: jest.fn(),
useKeyboardHandler: jest.fn(),
useKeyboardContext: jest.fn().mockReturnValue(values),
/// input
useReanimatedFocusedInput: jest.fn().mockReturnValue(focusedInput),
useFocusedInputHandler: jest.fn(),
Expand All @@ -47,6 +48,22 @@ const mock = {
setDefaultMode: jest.fn(),
dismiss: jest.fn(),
},
AndroidSoftInputModes: {
SOFT_INPUT_ADJUST_NOTHING: 48,
SOFT_INPUT_ADJUST_PAN: 32,
SOFT_INPUT_ADJUST_RESIZE: 16,
SOFT_INPUT_ADJUST_UNSPECIFIED: 0,
SOFT_INPUT_IS_FORWARD_NAVIGATION: 256,
SOFT_INPUT_MASK_ADJUST: 240,
SOFT_INPUT_MASK_STATE: 15,
SOFT_INPUT_MODE_CHANGED: 512,
SOFT_INPUT_STATE_ALWAYS_HIDDEN: 3,
SOFT_INPUT_STATE_ALWAYS_VISIBLE: 5,
SOFT_INPUT_STATE_HIDDEN: 2,
SOFT_INPUT_STATE_UNCHANGED: 1,
SOFT_INPUT_STATE_UNSPECIFIED: 0,
SOFT_INPUT_STATE_VISIBLE: 4,
},
KeyboardEvents: {
addListener: jest.fn(() => ({ remove: jest.fn() })),
},
Expand Down

0 comments on commit 620f379

Please sign in to comment.