-
Notifications
You must be signed in to change notification settings - Fork 478
[v2] Initial Core implementation #136
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
Conversation
| name: Type check | ||
| command: yarn test:types | ||
|
|
||
| "Test: unit": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI future notice: use the community orb for easier circle management: https://github.com/react-native-community/react-native-circleci-orb
babel.config.js
Outdated
| '@babel/preset-env', | ||
| { | ||
| targets: { | ||
| node: 'current', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| node: 'current', | |
| node: 8.3, |
RN requires Node 8.3+, we should be compatible
core/__tests__/AsyncStorage.test.ts
Outdated
| }); | ||
|
|
||
| describe('main API', () => { | ||
| it('handles basic set/read/remove calls', async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd use test.each for these tests for convenience
core/__tests__/AsyncStorage.test.ts
Outdated
| name: 'Jerry', | ||
| }; | ||
|
|
||
| // get |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
these comments are useless, please remove them all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops 🤦♂
core/__tests__/AsyncStorage.test.ts
Outdated
| }); | ||
|
|
||
| // instance | ||
| expect(asyncStorage.instance()).toBe(mockedStorage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this a part of your public API? asyncStorage is already an instance of a class, so this is quite unintuitive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it is. Some storages might come with other functionalities than AsyncStorage requires, so instance gives direct access to the storage (and to said functionalities). I guess I can rename it to getStorageInstance or smth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, this is dangerous. All functionalities should be behind a facade of your API, don't you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That'd be ideal, but we cannot predict all possibilities, right? Besides, I can imagine that'd be used only in advanced cases, which normally you wouldn't need.
Those extra methods would be expected to be documented in storage's docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be cautious about that, this is what Enzyme does that sooner or later made everyone using this API because guides and blog post were made. I'd remove this and think about expanding API when necessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Going to leave it like that for now, with a note to re-think it later.
core/__tests__/AsyncStorage.test.ts
Outdated
| }); | ||
|
|
||
| await as.get('key'); | ||
| expect(loggerFunc).toBeCalledTimes(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
called with what?
core/__tests__/AsyncStorage.test.ts
Outdated
| await as.get('key'); | ||
|
|
||
| expect(errorHandler).toBeCalledTimes(1); | ||
| expect(errorHandler.mock.calls[0][0]).toBe(error); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| expect(errorHandler.mock.calls[0][0]).toBe(error); | |
| expect(errorHandler).toHaveBeenCalledWith(error); |
core/__tests__/core.test.ts
Outdated
|
|
||
| describe('AsyncStorageFactory', () => { | ||
| it('Throws when tried to instantiate', () => { | ||
| expect(() => new Factory()).toThrow(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add some text that you expect to throw, it may have thrown for other reasons and test will pass, something like:
| expect(() => new Factory()).toThrow(); | |
| expect(() => new Factory()).toThrow('forbidden to instantiate Factory'); |
core/__tests__/core.test.ts
Outdated
| simpleErrorHandler(errorMessage); | ||
|
|
||
| expect(console.error).toBeCalledTimes(1); | ||
| expect(console.error.mock.calls[0][0]).toEqual(errorMessage); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| expect(console.error.mock.calls[0][0]).toEqual(errorMessage); | |
| expect(console.error).toHaveBeenCalledWith(errorMessage); |
| * Core Async Storage API | ||
| * | ||
| */ | ||
| declare class AsyncStorage< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't these types be output by tsc?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They were at first, but then I decided to remove it, as I needed more control over it
jest.config.js
Outdated
| @@ -0,0 +1,19 @@ | |||
| const commonSettings = { | |||
| transform: { | |||
| '^.+\\.tsx?$': 'babel-jest', | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the default transform, you should be good to remove it
|
Thanks a lot! |
| }; | ||
|
|
||
| switch (methodName) { | ||
| case 'get': { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yikes, that's not what I meant, this code is almost no different. You can use each like this:
it.each([
['get', 'myKey'],
['set', 'myKey', {name: 'Jerry'}],
['remove', 'myKey']
])('handles single %s api call', async (methodName: 'get' | 'set' | 'remove', ...args) => {
await as[methodName](...args);
expect(mockedStorage.getSingle).toBeCalledWith(...args.concat(null));
}There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yh, shame, this looks better. Will cover this in next PR.
Summary:
Todo: