Skip to content

Commit

Permalink
fix: new Reanimated API mock (#709)
Browse files Browse the repository at this point in the history
## 📜 Description

Starting from REA 3.15 a new API with `get`/`set` methods was added. In
this PR I'm adding corresponding methods to mock.

## 💡 Motivation and Context

If devs are using `get`/`set` methods in their code (which is
recommended way right now), then tests will crash because `get`/`set`
methods are `undefined`. To fix this problem I reflect corresponding
changes from Reanimated and adding these methods.

## 📢 Changelog

<!-- High level overview of important changes -->
<!-- For example: fixed status bar manipulation; added new types
declarations; -->
<!-- If your changes don't affect one of platform/language below - then
remove this platform/language -->

### JS

- added `get`/`set` methods to mock;
- added new unit tests with new API methods usage;

## 🤔 How Has This Been Tested?

Tested via unit-tests.

## 📸 Screenshots (if appropriate):

<img width="409" alt="image"
src="https://github.com/user-attachments/assets/e9c6390d-be7d-464d-8130-2d5bfd1a80cc">

## 📝 Checklist

- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed
  • Loading branch information
kirillzyusko authored Nov 30, 2024
1 parent fb5c52e commit 737e452
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 14 deletions.
58 changes: 58 additions & 0 deletions FabricExample/__tests__/reanimated-new-api.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import "@testing-library/jest-native/extend-expect";
import { render } from "@testing-library/react-native";
import React from "react";
import {
useReanimatedFocusedInput,
useReanimatedKeyboardAnimation,
} from "react-native-keyboard-controller";
import Reanimated, { useAnimatedStyle } from "react-native-reanimated";

function TestKeyboardMovementComponent() {
const { height } = useReanimatedKeyboardAnimation();
const style = useAnimatedStyle(
() => ({
width: 20,
height: 20,
backgroundColor: "red",
transform: [{ translateY: height.get() }],
}),
[],
);

return <Reanimated.View style={style} testID="view" />;
}

function TestFocusedInputLayoutComponent() {
const { input } = useReanimatedFocusedInput();
const style = useAnimatedStyle(() => {
const layout = input.get()?.layout;

return {
top: layout?.y,
left: layout?.x,
height: layout?.height,
width: layout?.width,
};
}, []);

return <Reanimated.View style={style} testID="view" />;
}

describe("basic keyboard interaction", () => {
it("should have default style with new reanimated API", () => {
const { getByTestId } = render(<TestKeyboardMovementComponent />);

expect(getByTestId("view")).toHaveStyle({ transform: [{ translateY: 0 }] });
});

it("should have default style with `useReanimatedFocusedInput` using new reanimated API", () => {
const { getByTestId } = render(<TestFocusedInputLayoutComponent />);

expect(getByTestId("view")).toHaveStyle({
top: 0,
left: 0,
width: 200,
height: 40,
});
});
});
58 changes: 58 additions & 0 deletions example/__tests__/reanimated-new-api.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import "@testing-library/jest-native/extend-expect";
import { render } from "@testing-library/react-native";
import React from "react";
import {
useReanimatedFocusedInput,
useReanimatedKeyboardAnimation,
} from "react-native-keyboard-controller";
import Reanimated, { useAnimatedStyle } from "react-native-reanimated";

function TestKeyboardMovementComponent() {
const { height } = useReanimatedKeyboardAnimation();
const style = useAnimatedStyle(
() => ({
width: 20,
height: 20,
backgroundColor: "red",
transform: [{ translateY: height.get() }],
}),
[],
);

return <Reanimated.View style={style} testID="view" />;
}

function TestFocusedInputLayoutComponent() {
const { input } = useReanimatedFocusedInput();
const style = useAnimatedStyle(() => {
const layout = input.get()?.layout;

return {
top: layout?.y,
left: layout?.x,
height: layout?.height,
width: layout?.width,
};
}, []);

return <Reanimated.View style={style} testID="view" />;
}

describe("basic keyboard interaction", () => {
it("should have default style with new reanimated API", () => {
const { getByTestId } = render(<TestKeyboardMovementComponent />);

expect(getByTestId("view")).toHaveStyle({ transform: [{ translateY: 0 }] });
});

it("should have default style with `useReanimatedFocusedInput` using new reanimated API", () => {
const { getByTestId } = render(<TestFocusedInputLayoutComponent />);

expect(getByTestId("view")).toHaveStyle({
top: 0,
left: 0,
width: 200,
height: 40,
});
});
});
31 changes: 17 additions & 14 deletions jest/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ const values = {
height: new Animated.Value(0),
},
reanimated: {
progress: { value: 0 },
height: { value: 0 },
progress: { value: 0, get: jest.fn().mockReturnValue(0), set: jest.fn() },
height: { value: 0, get: jest.fn().mockReturnValue(0), set: jest.fn() },
},
};
const inputData = {
target: 1,
parentScrollViewTarget: -1,
layout: {
x: 0,
y: 0,
width: 200,
height: 40,
absoluteX: 0,
absoluteY: 100,
},
};
const focusedInput = {
input: {
value: {
target: 1,
parentScrollViewTarget: -1,
layout: {
x: 0,
y: 0,
width: 200,
height: 40,
absoluteX: 0,
absoluteY: 100,
},
},
value: inputData,
get: jest.fn().mockReturnValue(inputData),
set: jest.fn(),
},
};

Expand Down

0 comments on commit 737e452

Please sign in to comment.