2015-03-18 22:57:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
2015-11-18 16:25:30 +00:00
|
|
|
*
|
|
|
|
* @flow
|
2017-02-25 11:05:32 +00:00
|
|
|
* @providesModule AsyncStorageTest
|
2015-03-18 22:57:49 +00:00
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-04-09 03:36:40 +00:00
|
|
|
var React = require('react');
|
|
|
|
var ReactNative = require('react-native');
|
2015-03-18 22:57:49 +00:00
|
|
|
var {
|
|
|
|
AsyncStorage,
|
|
|
|
Text,
|
|
|
|
View,
|
2016-04-09 03:36:40 +00:00
|
|
|
} = ReactNative;
|
|
|
|
var { TestModule } = ReactNative.NativeModules;
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2015-06-03 23:57:08 +00:00
|
|
|
var deepDiffer = require('deepDiffer');
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
var DEBUG = false;
|
|
|
|
|
|
|
|
var KEY_1 = 'key_1';
|
|
|
|
var VAL_1 = 'val_1';
|
|
|
|
var KEY_2 = 'key_2';
|
|
|
|
var VAL_2 = 'val_2';
|
2015-06-03 23:57:08 +00:00
|
|
|
var KEY_MERGE = 'key_merge';
|
|
|
|
var VAL_MERGE_1 = {'foo': 1, 'bar': {'hoo': 1, 'boo': 1}, 'moo': {'a': 3}};
|
|
|
|
var VAL_MERGE_2 = {'bar': {'hoo': 2}, 'baz': 2, 'moo': {'a': 3}};
|
|
|
|
var VAL_MERGE_EXPECT =
|
|
|
|
{'foo': 1, 'bar': {'hoo': 2, 'boo': 1}, 'baz': 2, 'moo': {'a': 3}};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
|
|
|
// setup in componentDidMount
|
2015-11-18 16:25:30 +00:00
|
|
|
var done = (result : ?boolean) => {};
|
|
|
|
var updateMessage = (message : string ) => {};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2015-11-18 16:25:30 +00:00
|
|
|
function runTestCase(description : string, fn) {
|
2015-03-18 22:57:49 +00:00
|
|
|
updateMessage(description);
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:30 +00:00
|
|
|
function expectTrue(condition : boolean, message : string) {
|
2015-03-18 22:57:49 +00:00
|
|
|
if (!condition) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-18 16:25:30 +00:00
|
|
|
function expectEqual(lhs, rhs, testname : string) {
|
2015-03-18 22:57:49 +00:00
|
|
|
expectTrue(
|
2015-06-03 23:57:08 +00:00
|
|
|
!deepDiffer(lhs, rhs),
|
|
|
|
'Error in test ' + testname + ': expected\n' + JSON.stringify(rhs) +
|
|
|
|
'\ngot\n' + JSON.stringify(lhs)
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-06-22 21:37:11 +00:00
|
|
|
function expectAsyncNoError(place, err) {
|
2015-06-23 21:17:31 +00:00
|
|
|
if (err instanceof Error) {
|
|
|
|
err = err.message;
|
|
|
|
}
|
2015-06-22 21:37:11 +00:00
|
|
|
expectTrue(err === null, 'Unexpected error in ' + place + ': ' + JSON.stringify(err));
|
2015-03-18 22:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSetAndGet() {
|
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, (err1) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testSetAndGet/setItem', err1);
|
2015-03-18 22:57:49 +00:00
|
|
|
AsyncStorage.getItem(KEY_1, (err2, result) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testSetAndGet/getItem', err2);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectEqual(result, VAL_1, 'testSetAndGet setItem');
|
|
|
|
updateMessage('get(key_1) correctly returned ' + result);
|
|
|
|
runTestCase('should get null for missing key', testMissingGet);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testMissingGet() {
|
|
|
|
AsyncStorage.getItem(KEY_2, (err, result) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testMissingGet/setItem', err);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectEqual(result, null, 'testMissingGet');
|
|
|
|
updateMessage('missing get(key_2) correctly returned ' + result);
|
|
|
|
runTestCase('check set twice results in a single key', testSetTwice);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testSetTwice() {
|
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, ()=>{
|
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, ()=>{
|
|
|
|
AsyncStorage.getItem(KEY_1, (err, result) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testSetTwice/setItem', err);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectEqual(result, VAL_1, 'testSetTwice');
|
|
|
|
updateMessage('setTwice worked as expected');
|
|
|
|
runTestCase('test removeItem', testRemoveItem);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testRemoveItem() {
|
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, ()=>{
|
|
|
|
AsyncStorage.setItem(KEY_2, VAL_2, ()=>{
|
|
|
|
AsyncStorage.getAllKeys((err, result) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testRemoveItem/getAllKeys', err);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectTrue(
|
|
|
|
result.indexOf(KEY_1) >= 0 && result.indexOf(KEY_2) >= 0,
|
|
|
|
'Missing KEY_1 or KEY_2 in ' + '(' + result + ')'
|
|
|
|
);
|
|
|
|
updateMessage('testRemoveItem - add two items');
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.removeItem(KEY_1, (err2) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testRemoveItem/removeItem', err2);
|
2015-03-18 22:57:49 +00:00
|
|
|
updateMessage('delete successful ');
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.getItem(KEY_1, (err3, result2) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testRemoveItem/getItem', err3);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectEqual(
|
2015-06-03 23:57:08 +00:00
|
|
|
result2,
|
2015-03-18 22:57:49 +00:00
|
|
|
null,
|
|
|
|
'testRemoveItem: key_1 present after delete'
|
|
|
|
);
|
|
|
|
updateMessage('key properly removed ');
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.getAllKeys((err4, result3) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testRemoveItem/getAllKeys', err4);
|
2015-03-18 22:57:49 +00:00
|
|
|
expectTrue(
|
2015-06-03 23:57:08 +00:00
|
|
|
result3.indexOf(KEY_1) === -1,
|
|
|
|
'Unexpected: KEY_1 present in ' + result3
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
2015-06-03 23:57:08 +00:00
|
|
|
updateMessage('proper length returned.');
|
|
|
|
runTestCase('should merge values', testMerge);
|
2015-03-18 22:57:49 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-06-03 23:57:08 +00:00
|
|
|
function testMerge() {
|
|
|
|
AsyncStorage.setItem(KEY_MERGE, JSON.stringify(VAL_MERGE_1), (err1) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testMerge/setItem', err1);
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.mergeItem(KEY_MERGE, JSON.stringify(VAL_MERGE_2), (err2) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testMerge/mergeItem', err2);
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.getItem(KEY_MERGE, (err3, result) => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testMerge/setItem', err3);
|
2015-06-03 23:57:08 +00:00
|
|
|
expectEqual(JSON.parse(result), VAL_MERGE_EXPECT, 'testMerge');
|
|
|
|
updateMessage('objects deeply merged\nDone!');
|
2016-02-06 00:44:44 +00:00
|
|
|
runTestCase('multi set and get', testOptimizedMultiGet);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function testOptimizedMultiGet() {
|
|
|
|
let batch = [[KEY_1, VAL_1], [KEY_2, VAL_2]];
|
|
|
|
let keys = batch.map(([key, value]) => key);
|
|
|
|
AsyncStorage.multiSet(batch, (err1) => {
|
|
|
|
// yes, twice on purpose
|
2017-10-10 00:37:08 +00:00
|
|
|
[1, 2].forEach((i) => {
|
2016-02-06 00:44:44 +00:00
|
|
|
expectAsyncNoError(`${i} testOptimizedMultiGet/multiSet`, err1);
|
|
|
|
AsyncStorage.multiGet(keys, (err2, result) => {
|
|
|
|
expectAsyncNoError(`${i} testOptimizedMultiGet/multiGet`, err2);
|
|
|
|
expectEqual(result, batch, `${i} testOptimizedMultiGet multiGet`);
|
|
|
|
updateMessage('multiGet([key_1, key_2]) correctly returned ' + JSON.stringify(result));
|
2015-06-03 23:57:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-02-06 00:44:44 +00:00
|
|
|
|
2017-08-18 01:36:54 +00:00
|
|
|
class AsyncStorageTest extends React.Component<{}, $FlowFixMeState> {
|
2016-07-26 08:00:02 +00:00
|
|
|
state = {
|
|
|
|
messages: 'Initializing...',
|
|
|
|
done: false,
|
|
|
|
};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
|
|
|
componentDidMount() {
|
2016-11-04 12:40:26 +00:00
|
|
|
done = () => this.setState({done: true}, () => {
|
|
|
|
TestModule.markTestCompleted();
|
|
|
|
});
|
2015-03-18 22:57:49 +00:00
|
|
|
updateMessage = (msg) => {
|
|
|
|
this.setState({messages: this.state.messages.concat('\n' + msg)});
|
|
|
|
DEBUG && console.log(msg);
|
|
|
|
};
|
|
|
|
AsyncStorage.clear(testSetAndGet);
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-18 22:57:49 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<View style={{backgroundColor: 'white', padding: 40}}>
|
|
|
|
<Text>
|
2017-09-06 10:25:01 +00:00
|
|
|
{
|
|
|
|
/* $FlowFixMe(>=0.54.0 site=react_native_fb,react_native_oss) This
|
|
|
|
* comment suppresses an error found when Flow v0.54 was deployed.
|
|
|
|
* To see the error delete this comment and run Flow. */
|
|
|
|
this.constructor.displayName + ': '}
|
2015-03-18 22:57:49 +00:00
|
|
|
{this.state.done ? 'Done' : 'Testing...'}
|
|
|
|
{'\n\n' + this.state.messages}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2016-07-26 08:00:02 +00:00
|
|
|
}
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2015-06-24 02:12:51 +00:00
|
|
|
AsyncStorageTest.displayName = 'AsyncStorageTest';
|
|
|
|
|
2015-03-18 22:57:49 +00:00
|
|
|
module.exports = AsyncStorageTest;
|