Skip to content
This repository was archived by the owner on Oct 16, 2025. It is now read-only.

Conversation

@randycoulman
Copy link
Contributor

Add test coverage for the public API of the library.

The tests use an odd combination of Jest and testdouble.js mocking, but that's the only way I was able to make it work reasonably well.

Because react-native-sensitive-info is a native module, we have to mock/stub it out for testing. I use jest.mock to do the module replacement, because I couldn't get td.replace to work properly despite several different attempts.

Because babel-jest "hoists" the jest.mock call, I extracted the r-n-s-i mock into its own file so I could use testdouble's td.func() for the functions. testdouble gives a much better API for stubs and expectations as well as checking arguments by default, so I chose to use it for most of the stubbing/mocking.

However, to stub out the platform detection code, I found it easier to use jest.fn() because it allows you to specify a custom mock implementation where testdouble does not. That suggests that perhaps the platform dependency should be extracted into its own file, but I chose not to do that at this time since this is a single-file module at present.

Along the way, I found several bugs in the code. In getItem() we were trying to convert any undefined response into a null instead in order to avoid a downstream exception, but it was too aggressive and was turning any "falsy" result into null. Also, we weren't checking for the callback before calling it in some of the exception handlers. Those issues are now fixed.

@randycoulman randycoulman requested review from casto101 and lexun June 16, 2017 22:50
@randycoulman randycoulman merged commit 4031b2d into master Jun 16, 2017
@randycoulman randycoulman deleted the test-coverage branch June 16, 2017 23:25
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants