Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking & monitoring Keyboard events with jest in react native

Given the following code:

import { Keyboard } from 'react-native';

// ....

componentDidMount() {
  this.keyboardShowListener = Keyboard.addListener(
    'keyboardWillShow',
    () => this.setState({ visible: true }),
  );
  this.keyboardHideListener = Keyboard.addListener(
    'keyboardWillHide',
    () => this.setState({ visible: false }),
  );
}

// ....

onCancel() {
  const { clearActiveInput } = this.props;
  clearActiveInput();
  Keyboard.dismiss();
}

Is there a correct way to mock the imported Keyboard component to both verify the listener subscription took place, and also to verify the dismiss() event was triggered?

like image 581
Cameron Hotchkies Avatar asked Oct 11 '18 18:10

Cameron Hotchkies


People also ask

What do mocking someone mean?

1 : to treat with contempt or ridicule : deride he has been mocked as a mama's boy— C. P. Pierce. 2 : to disappoint the hopes of for any government to mock men's hopes with mere words and promises and gestures— D. D. Eisenhower.

Is mocking a form of insult?

Mockery or mocking is the act of insulting or making light of a person or other thing, sometimes merely by taunting, but often by making a caricature, purporting to engage in imitation in a way that highlights unflattering characteristics.

What does mocking attitude mean?

When you talk to or about someone in a mocking tone, you're making fun of them in a nasty, mean way. But if you're a comedy writer or political satirist, a mocking attitude is a tool of your trade.

What does going mocking mean?

transitive verb. If someone mocks you, they show or pretend that they think you are foolish or inferior, for example by saying something funny about you, or by imitating your behavior. I thought you were mocking me. Synonyms: laugh at, insult, tease, ridicule More Synonyms of mock.


1 Answers

So this problem was way more complex that I could imagine to solve at start. Since what you want to test here is dismiss() and show() basically for a keyboard right?

So the events and listeners by documentation for 0.63 and 0.62 is https://reactnative.dev/docs/keyboard#docsNav

useEffect(() => {
   Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
   Keyboard.addListener("keyboardDidHide", _keyboardDidHide);

   // cleanup function
   return () => {
       Keyboard.removeListener("keyboardDidShow", _keyboardDidShow);
       Keyboard.removeListener("keyboardDidHide", _keyboardDidHide);
   };
}, []);

const _keyboardDidShow = () => {
    alert("Keyboard Shown");
};

const _keyboardDidHide = () => {
    alert("Keyboard Hidden");
};

To get Jest to call the 2 functions _keyboardDidShow and _keyboardDidHide you will need to use Keyboard.emit('keyboardDidShow')

Example:

it('Test Keyboards keyboardDidShow is called', () => {
    const { getByTestId } = render(<Container />);
    act(() => {
        Keyboard.emit('keyboardDidShow', {});
    });
    const box = getByTestId('TEST');
    //Do here your expect clauses to check if something changed in your container
});

Not fully sure if this will ever help anyone. But that's how I solved this puzzle to figure out how to coverage the _keyboardDidShow and Hide

EDIT: For version 0.66 > now you need to do the following:

useEffect(() => {
  const kds = Keyboard.addListener("keyboardDidShow", _keyboardDidShow);
  const kdh = Keyboard.addListener("keyboardDidHide", _keyboardDidHide);

  // cleanup function
  return () => {
    kdh.remove();
    kds.remove();
  };
}, []);

const _keyboardDidShow = () => {
  alert("Keyboard Shown");
};

const _keyboardDidHide = () => {
  alert("Keyboard Hidden");
};

Since they changed the way to make it subscription events from now on

And for the tests change the Keyboard.emit to:

Keyboard._emitter.emit('keyboardDidShow', {});
Keyboard._emitter.emit('keyboardDidHide', {});

And you should have everything you need to make it work in jest :)

like image 82
Thiago de Oliveira Cruz Avatar answered Sep 24 '22 14:09

Thiago de Oliveira Cruz