diff --git a/FabricExample/__tests__/custom-hook.spec.tsx b/FabricExample/__tests__/custom-hook.spec.tsx new file mode 100644 index 0000000000..7a86298cc3 --- /dev/null +++ b/FabricExample/__tests__/custom-hook.spec.tsx @@ -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(); + }); +}); diff --git a/FabricExample/package.json b/FabricExample/package.json index 9eada4784b..f253b548f1 100644 --- a/FabricExample/package.json +++ b/FabricExample/package.json @@ -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", diff --git a/FabricExample/yarn.lock b/FabricExample/yarn.lock index 2d3b66b22d..2390e5aae4 100644 --- a/FabricExample/yarn.lock +++ b/FabricExample/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/example/__tests__/custom-hook.spec.tsx b/example/__tests__/custom-hook.spec.tsx new file mode 100644 index 0000000000..7a86298cc3 --- /dev/null +++ b/example/__tests__/custom-hook.spec.tsx @@ -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(); + }); +}); diff --git a/example/package.json b/example/package.json index 9c334b7d73..4367116e7e 100644 --- a/example/package.json +++ b/example/package.json @@ -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", diff --git a/example/yarn.lock b/example/yarn.lock index 363e7fb7f9..fcac23904e 100644 --- a/example/yarn.lock +++ b/example/yarn.lock @@ -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" @@ -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" @@ -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" @@ -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" diff --git a/jest/index.js b/jest/index.js index a4a462ed14..4acf38b79a 100644 --- a/jest/index.js +++ b/jest/index.js @@ -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(), @@ -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() })), },