Skip to content

Commit

Permalink
Switching from AsyncStorage to react-native-async-storage (based on #54
Browse files Browse the repository at this point in the history
…) (#55)

* Updated react native dependencies.

* Updated babel dependencies. Removed the caret from react and react-native beacause of incorrect peer dependencies.

* Changed from .babelrc to babel.config.js (based on facebook/react-native#21075).

* Add mock prefix to allow out of scope referencing (jestjs/jest#2567)

* Mock the async-storage module.

* Added explanation to README.

* Changed version to 2.0.0 as it is a breaking change.
  • Loading branch information
oas authored and Jason Merino committed May 22, 2019
1 parent c9013e9 commit fe17187
Show file tree
Hide file tree
Showing 8 changed files with 4,894 additions and 4,112 deletions.
3 changes: 0 additions & 3 deletions .babelrc

This file was deleted.

7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ A minimalistic wrapper around React Native's AsyncStorage.
_The `react-native-simple-store` is a good match for apps that are **not using redux**. If you have already found that your app needs to use redux and you need to persist data to the device it is recommended that you make use of [redux-persist](https://github.com/rt2zz/redux-persist) which provides a clean interface for storing data in your reducers to device._

## Installation

```bash
npm install react-native-simple-store
```

Since this wrapper uses [react-native-async-storage](https://github.com/react-native-community/react-native-async-storage), it needs to be linked to work properly:

```
react-native link @react-native-community/async-storage
```

## Use In Project
```
import store from 'react-native-simple-store';
Expand Down
120 changes: 51 additions & 69 deletions __tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

const returnValues = {
const mockReturnValues = {
arrayOne: JSON.stringify(['red', 'blue']),
objectOne: JSON.stringify({
isATest: true,
Expand All @@ -10,88 +9,73 @@ const returnValues = {
stringOne: JSON.stringify('testing string'),
};

function multiGetTestData() {
function mockMultiGetTestData() {
return [
['key1', JSON.stringify({ valor: 1 })],
['key2', JSON.stringify({ valor: 2 })],
];
};
}

function multiSaveTestData() {
function mockMultiSaveTestData() {
return [
['key1', { valor: 1 }],
['key2', { valor: 2 }],
];
};

const INDEX_PATH = '../src/index';
}

jest.unmock(INDEX_PATH);
jest.unmock('lodash.merge');

jest.mock('react-native', () => ({
AsyncStorage: {
setItem: jest.fn(() => {
return new Promise((resolve) => {
resolve(null);
});
}),
multiSet: jest.fn(() => {
return new Promise((resolve) => {
resolve(null);
});
}),
getItem: jest.fn((key) => {
return new Promise((resolve) => {
if (returnValues[key]) {
resolve(returnValues[key]);
} else {
resolve(null);
}
});
}),
multiGet: jest.fn(() => {
return new Promise((resolve) => {
resolve(multiGetTestData());
});
}),
removeItem: jest.fn(() => {
return new Promise((resolve) => {
import AsyncStorage from '@react-native-community/async-storage';
jest.mock('@react-native-community/async-storage', () => ({
setItem: jest.fn(() => {
return new Promise((resolve) => {
resolve(null);
});
}),
multiSet: jest.fn(() => {
return new Promise((resolve) => {
resolve(null);
});
}),
getItem: jest.fn((key) => {
return new Promise((resolve) => {
if (mockReturnValues[key]) {
resolve(mockReturnValues[key]);
} else {
resolve(null);
});
}),
getAllKeys: jest.fn(() => {
return new Promise((resolve) => {
resolve(['one', 'two', 'three']);
});
}),
multiRemove: jest.fn(() => ({
then: jest.fn(),
})),
}
}
});
}),
multiGet: jest.fn(() => {
return new Promise((resolve) => {
resolve(mockMultiGetTestData());
});
}),
removeItem: jest.fn(() => {
return new Promise((resolve) => {
resolve(null);
});
}),
getAllKeys: jest.fn(() => {
return new Promise((resolve) => {
resolve(['one', 'two', 'three']);
});
}),
multiRemove: jest.fn(() => ({
then: jest.fn(),
})),
}));

describe('index.js', () => {
import store from "../src/index";
jest.unmock("../src/index");
jest.unmock('lodash.merge');

const { AsyncStorage } = require('react-native');
const store = require(INDEX_PATH);

beforeEach(() => {
AsyncStorage.setItem.mockClear();
AsyncStorage.multiSet.mockClear();
AsyncStorage.getItem.mockClear();
AsyncStorage.multiGet.mockClear();
AsyncStorage.removeItem.mockClear();
AsyncStorage.getAllKeys.mockClear();
AsyncStorage.multiRemove.mockClear();
});
describe('index.js', () => {

describe('save', () => {

it('should return a promise with no errors', () => {
return store.save('objectOne', JSON.parse(returnValues.objectOne)).then((error) => {
return store.save('objectOne', JSON.parse(mockReturnValues.objectOne)).then((error) => {
expect(error).toEqual(null);
expect(AsyncStorage.setItem).toBeCalledWith('objectOne', returnValues.objectOne);
expect(AsyncStorage.setItem).toBeCalledWith('objectOne', mockReturnValues.objectOne);
});
});

Expand All @@ -100,7 +84,7 @@ describe('index.js', () => {
['key1', JSON.stringify({ valor: 1 })],
['key2', JSON.stringify({ valor: 2 })],
];
return store.save(multiSaveTestData()).then((error) => {
return store.save(mockMultiSaveTestData()).then((error) => {
expect(error).toEqual(null);
expect(AsyncStorage.multiSet).toBeCalledWith(result);
});
Expand All @@ -112,7 +96,7 @@ describe('index.js', () => {

it('should return a promise with saved data', () => {
return store.get('objectOne').then((error) => {
expect(error).toEqual(JSON.parse(returnValues.objectOne));
expect(error).toEqual(JSON.parse(mockReturnValues.objectOne));
expect(AsyncStorage.getItem).toBeCalledWith('objectOne');
});
});
Expand Down Expand Up @@ -148,8 +132,6 @@ describe('index.js', () => {
});

it('should handle a string and return a promise with no errors', () => {
const { AsyncStorage } = require('react-native');
const store = require(INDEX_PATH);
return store.update('stringOne', 'asdf').then((error) => {
expect(error).toEqual(null);
expect(AsyncStorage.setItem).toBeCalledWith('stringOne', JSON.stringify('asdf'));
Expand Down
15 changes: 15 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = (api) => {
api.cache(true);

return {
presets: [
"@babel/preset-env",
"@babel/preset-react",
"@babel/preset-flow"
],
plugins: [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-proposal-export-default-from"
]
}
};
Loading

0 comments on commit fe17187

Please # to comment.