Skip to content

Conversation

@krizzu
Copy link
Member

@krizzu krizzu commented Jun 22, 2019

Summary:

Todo:

  • Initial implementation
  • Write tests
  • CI to run tests

@krizzu krizzu added v2 labels Jun 22, 2019
name: Type check
command: yarn test:types

"Test: unit":
Copy link

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',
Copy link

@thymikee thymikee Jul 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
node: 'current',
node: 8.3,

RN requires Node 8.3+, we should be compatible

});

describe('main API', () => {
it('handles basic set/read/remove calls', async () => {
Copy link

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

name: 'Jerry',
};

// get
Copy link

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops 🤦‍♂

});

// instance
expect(asyncStorage.instance()).toBe(mockedStorage);
Copy link

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

Copy link
Member Author

@krizzu krizzu Jul 1, 2019

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

Copy link

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?

Copy link
Member Author

@krizzu krizzu Jul 1, 2019

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.

Copy link

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

Copy link
Member Author

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.

});

await as.get('key');
expect(loggerFunc).toBeCalledTimes(1);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

called with what?

await as.get('key');

expect(errorHandler).toBeCalledTimes(1);
expect(errorHandler.mock.calls[0][0]).toBe(error);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(errorHandler.mock.calls[0][0]).toBe(error);
expect(errorHandler).toHaveBeenCalledWith(error);


describe('AsyncStorageFactory', () => {
it('Throws when tried to instantiate', () => {
expect(() => new Factory()).toThrow();
Copy link

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:

Suggested change
expect(() => new Factory()).toThrow();
expect(() => new Factory()).toThrow('forbidden to instantiate Factory');

simpleErrorHandler(errorMessage);

expect(console.error).toBeCalledTimes(1);
expect(console.error.mock.calls[0][0]).toEqual(errorMessage);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
expect(console.error.mock.calls[0][0]).toEqual(errorMessage);
expect(console.error).toHaveBeenCalledWith(errorMessage);

* Core Async Storage API
*
*/
declare class AsyncStorage<
Copy link

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?

Copy link
Member Author

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',
Copy link

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

@krizzu krizzu merged commit eeb7560 into master Jul 2, 2019
@krizzu krizzu deleted the core-impl branch July 2, 2019 15:36
@krizzu
Copy link
Member Author

krizzu commented Jul 2, 2019

Thanks a lot!

@krizzu krizzu removed the WIP label Jul 2, 2019
};

switch (methodName) {
case 'get': {
Copy link

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));
}

Copy link
Member Author

@krizzu krizzu Jul 2, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants