Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/utils/CHANGELOG.MD
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]
Changes that have landed but are not yet released.

### New Features
- Added `PersistentKeyValueCache` interface and its implementation for React Native under `ReactNativeAsyncStorageCahe`.

## [0.2.0] - August 7, 2019

### New Features
Expand Down
37 changes: 37 additions & 0 deletions packages/utils/__mocks__/@react-native-community/async-storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export default class AsyncStorage {
static getItem(key: string, callback?: (error?: Error, result?: string) => void): Promise<string | null> {
return new Promise((resolve, reject) => {
switch (key) {
case 'keyThatExists':
resolve('{ "name": "Awesome Object" }')
break
case 'keyThatDoesNotExist':
resolve(null)
break
case 'keyWithInvalidJsonObject':
resolve('bad json }')
break
}
})
}

static setItem(key: string, value: string, callback?: (error?: Error) => void): Promise<void> {
return Promise.resolve()
}
}
70 changes: 70 additions & 0 deletions packages/utils/__tests__/reactNativeAsyncStorageCache.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/**
* Copyright 2020, Optimizely
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import ReactNativeAsyncStorageCache from '../src/reactNativeAsyncStorageCache';

describe('reactNativeAsyncStorageCache', () => {
let cacheInstance: ReactNativeAsyncStorageCache;

beforeEach(() => {
cacheInstance = new ReactNativeAsyncStorageCache();
});

describe('get', function() {
it('should return correct object when item is found in cache', function() {
return cacheInstance.get('keyThatExists').then(v => expect(v).toEqual({ name: 'Awesome Object' }));
});

it('should return null if item is not found in cache', function() {
return cacheInstance.get('keyThatDoesNotExist').then(v => expect(v).toBeNull());
});

it('should reject promise error if string has an incorrect JSON format', function() {
return cacheInstance
.get('keyWithInvalidJsonObject')
.catch(() => 'exception caught')
.then(v => {
expect(v).toEqual('exception caught');
});
});
});

describe('set', function() {
it('should resolve promise if item was successfully set in the cache', function() {
const testObj = { name: 'Awesome Object' };
return cacheInstance.set('testKey', testObj);
});

it('should reject promise if item was not set in the cache because of json stringifying error', function() {
const testObj: any = { name: 'Awesome Object' };
testObj.myOwnReference = testObj;
return cacheInstance
.set('testKey', testObj)
.catch(() => 'exception caught')
.then(v => expect(v).toEqual('exception caught'));
});
});

describe('contains', function() {
it('should return true if object with key exists', function() {
return cacheInstance.contains('keyThatExists').then(v => expect(v).toBeTruthy());
});

it('should return false if object with key does not exist', function() {
return cacheInstance.contains('keyThatDoesNotExist').then(v => expect(v).toBeFalsy());
});
});
});
Loading