Skip to content

Commit 1b97abe

Browse files
committed
Added unit tests
1 parent 8d36c5b commit 1b97abe

File tree

3 files changed

+136
-13
lines changed

3 files changed

+136
-13
lines changed
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('asdfa }')
29+
break;
30+
}
31+
})
32+
}
33+
34+
static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
35+
return new Promise(resolve => resolve())
36+
}
37+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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(done) {
28+
cacheInstance.get('keyThatExists')
29+
.then(v => {
30+
expect(v).toEqual({ name: "Awesome Object" })
31+
done()
32+
})
33+
})
34+
35+
it('should return null if item is not found in cache', function(done) {
36+
cacheInstance.get('keyThatDoesNotExist')
37+
.then(v => {
38+
expect(v).toBeNull()
39+
done()
40+
})
41+
})
42+
43+
it('should reject promise error if string has an incorrect JSON format', function(done) {
44+
cacheInstance.get('keyWithInvalidJsonObject')
45+
.catch(e => {
46+
done()
47+
})
48+
})
49+
})
50+
51+
describe('set', function() {
52+
it('should resolve promise if item was successfully set in the cache', function(done) {
53+
const testObj = { name: "Awesome Object" }
54+
cacheInstance.set('testKey', testObj).then(() => done())
55+
})
56+
57+
it('should reject promise if item was not set in the cache because of json stringifying error', function(done) {
58+
const testObj: any = { name: "Awesome Object" }
59+
testObj.myOwnReference = testObj
60+
cacheInstance.set('testKey', testObj).catch(() => done())
61+
})
62+
})
63+
64+
describe('contains', function() {
65+
it('should return true if object with key exists', function(done) {
66+
cacheInstance.contains('keyThatExists').then(v => {
67+
expect(v).toBeTruthy()
68+
done()
69+
})
70+
})
71+
72+
it('should return false if object with key does not exist', function(done) {
73+
cacheInstance.contains('keyThatDoesNotExist').then(v => {
74+
expect(v).toBeFalsy()
75+
done()
76+
})
77+
})
78+
})
79+
})

packages/datafile-manager/src/reactNativeAsyncStorageCache.ts

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,43 +14,50 @@
1414
* limitations under the License.
1515
*/
1616

17-
import { getLogger } from '@optimizely/js-sdk-logging';
18-
import AsyncStorage from '@react-native-community/async-storage';
17+
import { getLogger } from '@optimizely/js-sdk-logging'
18+
import AsyncStorage from '@react-native-community/async-storage'
1919

20-
import PersistentKeyValueCache from './persistentKeyValueCache';
20+
import PersistentKeyValueCache from './persistentKeyValueCache'
2121

2222
const logger = getLogger('DatafileManager')
2323

2424
export default class ReactNativeAsyncStorageCache implements PersistentKeyValueCache {
2525

2626
get(key: string): Promise<Object | null> {
2727
return new Promise((resolve, reject) => {
28-
AsyncStorage.getItem(key).then((val: String | null) => {
28+
AsyncStorage.getItem(key).then((val: string | null) => {
2929
if (val) {
3030
try {
31-
resolve(JSON.parse(val.valueOf()));
31+
resolve(JSON.parse(val))
3232
} catch (ex) {
33-
logger.error('Error Parsing Object from cache');
34-
reject(ex);
33+
logger.error('Error Parsing Object from cache - %s', ex)
34+
reject(ex)
3535
}
3636
} else {
37-
resolve(null);
37+
resolve(null)
3838
}
39-
}).catch(reject);
40-
});
39+
}).catch(reject)
40+
})
4141
}
4242

4343
set(key: string, val: Object): Promise<void> {
44-
return AsyncStorage.setItem(key, typeof val === 'object' ? JSON.stringify(val) : val);
44+
return new Promise((resolve, reject) => {
45+
try {
46+
AsyncStorage.setItem(key, JSON.stringify(val)).then(resolve).catch(reject)
47+
} catch (e) {
48+
logger.error('Error stringifying Object to Json - %s', e)
49+
reject(e)
50+
}
51+
})
4552
}
4653

4754
contains(key: string): Promise<Boolean> {
4855
return new Promise((resolve, reject) =>
49-
AsyncStorage.getItem(key).then((val: String | null) => resolve(val !== null)).catch(reject)
56+
AsyncStorage.getItem(key).then((val: string | null) => resolve(val !== null)).catch(reject)
5057
)
5158
}
5259

5360
remove(key: string): Promise<void> {
54-
return AsyncStorage.removeItem(key);
61+
return AsyncStorage.removeItem(key)
5562
}
5663
}

0 commit comments

Comments
 (0)