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

feat: iOS unit tests #366

Merged
merged 15 commits into from
Mar 29, 2024
Merged

feat: iOS unit tests #366

merged 15 commits into from
Mar 29, 2024

Conversation

kirillzyusko
Copy link
Owner

@kirillzyusko kirillzyusko commented Feb 22, 2024

📜 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:

  • get all inputs -> relies on window usage, and actual window doesn't contain any TextInputs -> most likely we need to pass params and pass a mock from unit tests but I don't want to change source code right now
  • testSetFocusToNextDoesNothingIfLastElement -> there is no way to check that view keeps focus
  • testSetFocusToPrevDoesNothingIfFirstElement -> there is no way to check that view keeps focus

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

  • added a job for running unit-tests;

iOS

  • added KeyboardControllerNative project;
  • excluded KeyboardControllerNative from Pods (to prevent compilation errors);

JS

  • excluded KeyboardControllerNative from being published to npm;

🤔 How Has This Been Tested?

Tested locally and on CI.

📝 Checklist

  • CI successfully passed
  • I added new mocks and corresponding unit-tests if library API was changed

@kirillzyusko kirillzyusko added 🍎 iOS iOS specific CI CI (github actions) labels Feb 22, 2024
@kirillzyusko kirillzyusko self-assigned this Feb 22, 2024
Copy link
Contributor

github-actions bot commented Feb 22, 2024

📊 Package size report

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

@kirillzyusko kirillzyusko added the tests You added or changed unit tests label Feb 22, 2024
@kirillzyusko kirillzyusko marked this pull request as ready for review March 29, 2024 18:51
@kirillzyusko kirillzyusko merged commit 1c3ae50 into main Mar 29, 2024
11 of 12 checks passed
@kirillzyusko kirillzyusko deleted the feat/ios-unit-tests branch March 29, 2024 20:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI CI (github actions) 🍎 iOS iOS specific tests You added or changed unit tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant