Skip to content

Commit 0e1d075

Browse files
authored
refactor: Move Async Storage Implementation to utils package (#503)
Summary: This is first PR in an effort to implement support for Offline Events persistence for React Native Apps. Async Storage implementation was already done in datafile-manager package to support datafile caching. This PR moves the generic caching implementation to utils so that it can be used by both datafile-manager and event-processor. Test plan: All Existing tests should pass
1 parent e017d0d commit 0e1d075

File tree

8 files changed

+1155
-905
lines changed

8 files changed

+1155
-905
lines changed

packages/utils/CHANGELOG.MD

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
77
## [Unreleased]
88
Changes that have landed but are not yet released.
99

10+
### New Features
11+
- Added `PersistentKeyValueCache` interface and its implementation for React Native under `ReactNativeAsyncStorageCahe`.
12+
1013
## [0.2.0] - August 7, 2019
1114

1215
### New Features
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Copyright 2020, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
export default class AsyncStorage {
18+
static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
19+
return new Promise((resolve, reject) => {
20+
switch (key) {
21+
case 'keyThatExists':
22+
resolve('{ "name": "Awesome Object" }')
23+
break
24+
case 'keyThatDoesNotExist':
25+
resolve(null)
26+
break
27+
case 'keyWithInvalidJsonObject':
28+
resolve('bad json }')
29+
break
30+
}
31+
})
32+
}
33+
34+
static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
35+
return Promise.resolve()
36+
}
37+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/**
2+
* Copyright 2020, Optimizely
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
import ReactNativeAsyncStorageCache from '../src/reactNativeAsyncStorageCache';
18+
19+
describe('reactNativeAsyncStorageCache', () => {
20+
let cacheInstance: ReactNativeAsyncStorageCache;
21+
22+
beforeEach(() => {
23+
cacheInstance = new ReactNativeAsyncStorageCache();
24+
});
25+
26+
describe('get', function() {
27+
it('should return correct object when item is found in cache', function() {
28+
return cacheInstance.get('keyThatExists').then(v => expect(v).toEqual({ name: 'Awesome Object' }));
29+
});
30+
31+
it('should return null if item is not found in cache', function() {
32+
return cacheInstance.get('keyThatDoesNotExist').then(v => expect(v).toBeNull());
33+
});
34+
35+
it('should reject promise error if string has an incorrect JSON format', function() {
36+
return cacheInstance
37+
.get('keyWithInvalidJsonObject')
38+
.catch(() => 'exception caught')
39+
.then(v => {
40+
expect(v).toEqual('exception caught');
41+
});
42+
});
43+
});
44+
45+
describe('set', function() {
46+
it('should resolve promise if item was successfully set in the cache', function() {
47+
const testObj = { name: 'Awesome Object' };
48+
return cacheInstance.set('testKey', testObj);
49+
});
50+
51+
it('should reject promise if item was not set in the cache because of json stringifying error', function() {
52+
const testObj: any = { name: 'Awesome Object' };
53+
testObj.myOwnReference = testObj;
54+
return cacheInstance
55+
.set('testKey', testObj)
56+
.catch(() => 'exception caught')
57+
.then(v => expect(v).toEqual('exception caught'));
58+
});
59+
});
60+
61+
describe('contains', function() {
62+
it('should return true if object with key exists', function() {
63+
return cacheInstance.contains('keyThatExists').then(v => expect(v).toBeTruthy());
64+
});
65+
66+
it('should return false if object with key does not exist', function() {
67+
return cacheInstance.contains('keyThatDoesNotExist').then(v => expect(v).toBeFalsy());
68+
});
69+
});
70+
});

0 commit comments

Comments
 (0)