Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add jest utils #110

Closed
IvanIhnatsiuk opened this issue Dec 25, 2022 · 2 comments
Closed

add jest utils #110

IvanIhnatsiuk opened this issue Dec 25, 2022 · 2 comments
Assignees

Comments

@IvanIhnatsiuk
Copy link
Contributor

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
Hi @kirillzyusko, Thanks a lot for the library. I would like to suggest you implementation of jestUtils, because It would be really great to have possibility to mock keyboard events in jest.

Describe the solution you'd like
A clear and concise description of what you want to happen.

create a mock implementation for opening/closing keyboard events. In jest it will look like this:

const { getByTestId } = render(<SomeComponentWithKeyboardHandler />);

expect(getByTestId("keyboardEventsView").toHaveAnimatedStyle({
 height: 0,
}) // or just simple style if you use Animated from RN core;

RNKeyboardHandler.openKeyboard(state);  // state - height, progress

expect(getByTestId("keyboardEventsView").toHaveAnimatedStyle({
 height: 100,
}) 

RNKeyboardHandler.hideKeyboard(state);  // state - height, progress

expect(getByTestId("keyboardEventsView").toHaveAnimatedStyle({
 height: 0,
}) 

Additional context
Add any other context or screenshots about the feature request here.
I have some ideas how to implement this logic and we can contact to discuss it :)

@kirillzyusko
Copy link
Owner

Hi @IvanIhnatsiuk

I wanted to add it, but with the API that you suggested it seems like you will need to change global object in mock:

RNKeyboardHandler.openKeyboard(state);  // state - height, progress <- seems like here you will change a global value that further is consumed by `useKeyboardAnimation` hook

And as far as I remember I experimented with it and it had some problems with tests concurrency. The mock will be created once per file, but since you are changing global object and tests are running in parallel there can be a race condition, where one test will change value and will expect to have that value in next expect statement, though it's not always truth, since another test also can change it), for example:

describe("keyboard unit tests", () => {
  it("should have keyboard height = 0", () => {
    const { getByTestId } = render(<SomeComponentWithKeyboardHandler />);

    RNKeyboardHandler.setState({height: 0, progress: 0});

    expect(getByTestId("keyboardEventsView").toHaveAnimatedStyle({
       height: 0,
    });
  });

  it("should have keyboard height = 100", () => {
    const { getByTestId } = render(<SomeComponentWithKeyboardHandler />);

    RNKeyboardHandler.setState({height: 100, progress: 0.5});

    expect(getByTestId("keyboardEventsView").toHaveAnimatedStyle({
       height: 100,
    });
  });
});

Here we have a situation, where two tests are running in parallel, and there might be chances, that RNKeyboardHandler.setState({height: 100, progress: 0.5}); will be executed straight after RNKeyboardHandler.setState({height: 0, progress: 0}); and as a result first test will fail.

If you have ideas how to implement such helper/utils that will be able to mock value per tests - let me know, I will be glad to hear that!


For now you can simply mock returned values for necessary hooks - you may find examples here and here.

@kirillzyusko
Copy link
Owner

Closed due to inactivity. Feel free to re-open this or open a new issue 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants