kirillzyusko / react-native-keyboard-controller

Keyboard manager which works in identical way on both iOS and Android
https://kirillzyusko.github.io/react-native-keyboard-controller/
MIT License
1.54k stars 61 forks source link

feat: iOS unit tests #366

Closed kirillzyusko closed 5 months ago

kirillzyusko commented 6 months ago

📜 Description

Added native unit tests on iOS.

💡 Motivation and Context

Where to write unit tests?

The problem with current integration of unit-tests on iOS is the fact that we depends on react dependency heavily which is available in example or FabricExample projects.

Of course I didn't want to duplicate tests (and there is no clear option on how to add them for KeyboardController.xcodeproj i. e. root) so I decided to create a new "HelloWorld" project and link files that I want to test.

Fortunately current codebase is designed quite well (thanks for supporting both architectures 😀), so I have a separated business-logic (keyboard frame tracking etc.) and consumers (react views/bridge bindings).

So I didn't make any modifications to the source code. The only one "suspicious" thing is adding extension for reactTag property (I rely on it in Extensions file and Extension file needs to be linked since ViewHierarchyNavigator uses UIResponder.current extension).

Unit tests approach

I decided to test everything as unit tests. Of course some of methods, such as becomeFirstResponder doesn't work (actually they work, but there is no way to check that method has been called as we can do in jest - and by default iOS doesn't provide any mocks).

So I've created TestableTextField/TestableTextView and for hierarchy creation used these classes.

Some of corresponding Android tests can not be ported to iOS:

But overall I'm happy to have at least something to have a better protection over accident changes that can break the library code.

CI integration

Also I added a job on CI to run these tests continuously.

📢 Changelog

CI

iOS

JS

🤔 How Has This Been Tested?

Tested locally and on CI.

📝 Checklist

github-actions[bot] commented 6 months ago

📊 Package size report

Current size Target Size Difference
130819 bytes 130798 bytes 21 bytes 📈