-
Notifications
You must be signed in to change notification settings - Fork 477
[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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,160 @@ | ||
| import AsyncStorage from '../src/AsyncStorage'; | ||
|
|
||
| class StorageMock implements IStorageBackend<any> { | ||
| getSingle = jest.fn(); | ||
| setSingle = jest.fn(); | ||
| getMany = jest.fn(); | ||
| setMany = jest.fn(); | ||
| removeSingle = jest.fn(); | ||
| removeMany = jest.fn(); | ||
| getKeys = jest.fn(); | ||
| dropStorage = jest.fn(); | ||
| } | ||
|
|
||
| describe('AsyncStorage', () => { | ||
| const mockedStorage = new StorageMock(); | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| describe('main API', () => { | ||
| it.each(['get', 'set', 'remove'])( | ||
| 'handles single %s api call', | ||
| async (methodName: string) => { | ||
| const as = new AsyncStorage(mockedStorage, { | ||
| logger: false, | ||
| errorHandler: false, | ||
| }); | ||
|
|
||
| const key = 'myKey'; | ||
| const value = { | ||
| name: 'Jerry', | ||
| }; | ||
|
|
||
| switch (methodName) { | ||
| case 'get': { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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));
}
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yh, shame, this looks better. Will cover this in next PR. |
||
| await as.get(key); | ||
| expect(mockedStorage.getSingle).toBeCalledWith(key, null); | ||
| break; | ||
| } | ||
|
|
||
| case 'set': { | ||
| await as.set(key, value); | ||
| expect(mockedStorage.setSingle).toBeCalledWith(key, value, null); | ||
| break; | ||
| } | ||
|
|
||
| case 'remove': { | ||
| await as.remove(key); | ||
| expect(mockedStorage.removeSingle).toBeCalledWith(key, null); | ||
| break; | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it.each(['set', 'read', 'remove'])( | ||
| 'handles basic multi %s api call', | ||
| async (methodName: string) => { | ||
| const keys = ['key1', 'key2', 'key3']; | ||
| const keyValues = [ | ||
| {key1: 'value1'}, | ||
| {key2: 'value2'}, | ||
| {key3: 'value3'}, | ||
| ]; | ||
|
|
||
| const as = new AsyncStorage(mockedStorage, { | ||
| logger: false, | ||
| errorHandler: false, | ||
| }); | ||
|
|
||
| switch (methodName) { | ||
| case 'get': { | ||
| await as.getMultiple(keys); | ||
| expect(mockedStorage.getMany).toBeCalledWith(keys, null); | ||
| break; | ||
| } | ||
|
|
||
| case 'set': { | ||
| await as.setMultiple(keyValues); | ||
| expect(mockedStorage.setMany).toBeCalledWith(keyValues, null); | ||
| break; | ||
| } | ||
|
|
||
| case 'remove': { | ||
| await as.removeMultiple(keys); | ||
| expect(mockedStorage.removeMany).toBeCalledWith(keys, null); | ||
| break; | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it.each(['instance', 'getKeys', 'clearStorage'])( | ||
| 'handles %s api call', | ||
| async (methodName: string) => { | ||
| const asyncStorage = new AsyncStorage(mockedStorage, { | ||
| logger: false, | ||
| errorHandler: false, | ||
| }); | ||
|
|
||
| switch (methodName) { | ||
| case 'instance': { | ||
| expect(asyncStorage.instance()).toBe(mockedStorage); | ||
| break; | ||
| } | ||
|
|
||
| case 'getKeys': { | ||
| mockedStorage.getKeys.mockImplementationOnce(() => [ | ||
| 'key1', | ||
| 'key2', | ||
| ]); | ||
| const keys = await asyncStorage.getKeys(); | ||
| expect(keys).toEqual(['key1', 'key2']); | ||
| break; | ||
| } | ||
|
|
||
| case 'dropStorage': { | ||
| await asyncStorage.clearStorage(); | ||
| expect(mockedStorage.dropStorage).toBeCalledTimes(1); | ||
| break; | ||
| } | ||
| } | ||
| }, | ||
| ); | ||
| }); | ||
| describe('utils', () => { | ||
| it('uses logger when provided', async () => { | ||
| const loggerFunc = jest.fn(); | ||
|
|
||
| const as = new AsyncStorage(mockedStorage, { | ||
| logger: loggerFunc, | ||
| errorHandler: false, | ||
| }); | ||
|
|
||
| await as.get('key'); | ||
| expect(loggerFunc).toBeCalledTimes(1); | ||
| expect(loggerFunc).toBeCalledWith({action: 'read-single', key: 'key'}); | ||
| }); | ||
|
|
||
| it('uses error handler when provided', async () => { | ||
| const errorHandler = jest.fn(); | ||
|
|
||
| const error = new Error('Fatal!'); | ||
| mockedStorage.getSingle.mockImplementationOnce(async () => { | ||
| throw error; | ||
| }); | ||
|
|
||
| const as = new AsyncStorage(mockedStorage, { | ||
| errorHandler, | ||
| logger: false, | ||
| }); | ||
|
|
||
| await as.get('key'); | ||
|
|
||
| expect(errorHandler).toBeCalledTimes(1); | ||
| expect(errorHandler).toBeCalledWith(error); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| import Factory from '../src/'; | ||
| import {simpleLogger, simpleErrorHandler} from '../src/defaults'; | ||
|
|
||
| describe('AsyncStorageFactory', () => { | ||
| it('Throws when tried to instantiate', () => { | ||
| expect(() => new Factory()).toThrow( | ||
| "[AsyncStorage] AsyncStorageFactory must not be instantiated.\nInstead, use static functions, like 'create' to get AsyncStorage instance.", | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SimpleLogger', () => { | ||
| beforeAll(() => { | ||
| jest.spyOn(console, 'log').mockImplementation(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| console.log.mockReset(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| console.log.mockRestore(); | ||
| }); | ||
|
|
||
| it('logs basic info about action', () => { | ||
| const actionInfo: LoggerAction = { | ||
| action: 'save-single', | ||
| key: 'MyKey', | ||
| value: 'MyValue', | ||
| }; | ||
|
|
||
| simpleLogger(actionInfo); | ||
|
|
||
| expect(console.log).toBeCalledTimes(1); | ||
|
|
||
| const callArgs = console.log.mock.calls[0][0]; | ||
| expect(callArgs).toContain('[AsyncStorage]'); | ||
| expect(callArgs).toContain(actionInfo.key); | ||
| expect(callArgs).toContain(actionInfo.value); | ||
| }); | ||
|
|
||
| it('handles unknown action by logging it', () => { | ||
| const actionInfo: LoggerAction = { | ||
| // @ts-ignore need to handle unknown | ||
| action: 'my-action', | ||
| }; | ||
|
|
||
| simpleLogger(actionInfo); | ||
|
|
||
| expect(console.log).toBeCalledTimes(1); | ||
| expect(console.log).toBeCalledWith( | ||
| '[AsyncStorage] Unknown action: my-action', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('SimpleErrorHandler', () => { | ||
| beforeAll(() => { | ||
| jest.spyOn(console, 'error').mockImplementation(); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| console.error.mockReset(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| console.error.mockRestore(); | ||
| }); | ||
| it('logs error when it is a string', () => { | ||
| const errorMessage = 'Fatal!'; | ||
|
|
||
| simpleErrorHandler(errorMessage); | ||
|
|
||
| expect(console.error).toBeCalledTimes(1); | ||
| expect(console.error).toBeCalledWith(errorMessage); | ||
| }); | ||
|
|
||
| it('logs error when it is an Error', () => { | ||
| const error = new Error('Fatal!'); | ||
|
|
||
| simpleErrorHandler(error); | ||
|
|
||
| expect(console.error).toBeCalledTimes(1); | ||
| expect(console.error).toBeCalledWith('Fatal!'); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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