2015-03-18 22:57:49 +00:00
|
|
|
/**
|
2015-03-23 22:07:33 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-11-18 16:25:30 +00:00
|
|
|
*
|
2018-05-10 22:44:55 +00:00
|
|
|
* @format
|
2015-11-18 16:25:30 +00:00
|
|
|
* @flow
|
2015-03-18 22:57:49 +00:00
|
|
|
*/
|
2018-05-10 22:44:55 +00:00
|
|
|
|
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');
|
2018-05-10 22:44:55 +00:00
|
|
|
var {AsyncStorage, Text, View} = 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';
|
2018-05-10 22:44:55 +00:00
|
|
|
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
|
2018-05-10 22:44:55 +00:00
|
|
|
var done = (result: ?boolean) => {};
|
|
|
|
var updateMessage = (message: string) => {};
|
2015-03-18 22:57:49 +00:00
|
|
|
|
2018-05-10 22:44:55 +00:00
|
|
|
function runTestCase(description: string, fn) {
|
2015-03-18 22:57:49 +00:00
|
|
|
updateMessage(description);
|
|
|
|
fn();
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:55 +00:00
|
|
|
function expectTrue(condition: boolean, message: string) {
|
2015-03-18 22:57:49 +00:00
|
|
|
if (!condition) {
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:44:55 +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),
|
2018-05-10 22:44:55 +00:00
|
|
|
'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;
|
|
|
|
}
|
2018-05-10 22:44:55 +00:00
|
|
|
expectTrue(
|
|
|
|
err === null,
|
|
|
|
'Unexpected error in ' + place + ': ' + JSON.stringify(err),
|
|
|
|
);
|
2015-03-18 22:57:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function testSetAndGet() {
|
2018-05-10 22:44:55 +00:00
|
|
|
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() {
|
2018-05-10 22:44:55 +00:00
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, () => {
|
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, () => {
|
2015-03-18 22:57:49 +00:00
|
|
|
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() {
|
2018-05-10 22:44:55 +00:00
|
|
|
AsyncStorage.setItem(KEY_1, VAL_1, () => {
|
|
|
|
AsyncStorage.setItem(KEY_2, VAL_2, () => {
|
2015-03-18 22:57:49 +00:00
|
|
|
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,
|
2018-05-10 22:44:55 +00:00
|
|
|
'Missing KEY_1 or KEY_2 in ' + '(' + result + ')',
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
|
|
|
updateMessage('testRemoveItem - add two items');
|
2018-05-10 22:44:55 +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,
|
2018-05-10 22:44:55 +00:00
|
|
|
'testRemoveItem: key_1 present after delete',
|
2015-03-18 22:57:49 +00:00
|
|
|
);
|
|
|
|
updateMessage('key properly removed ');
|
2015-06-03 23:57:08 +00:00
|
|
|
AsyncStorage.getAllKeys((err4, result3) => {
|
2018-05-10 22:44:55 +00:00
|
|
|
expectAsyncNoError('testRemoveItem/getAllKeys', err4);
|
|
|
|
expectTrue(
|
|
|
|
result3.indexOf(KEY_1) === -1,
|
|
|
|
'Unexpected: KEY_1 present in ' + result3,
|
|
|
|
);
|
|
|
|
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() {
|
2018-05-10 22:44:55 +00:00
|
|
|
AsyncStorage.setItem(KEY_MERGE, JSON.stringify(VAL_MERGE_1), err1 => {
|
2015-06-22 21:37:11 +00:00
|
|
|
expectAsyncNoError('testMerge/setItem', err1);
|
2018-05-10 22:44:55 +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);
|
2018-05-10 22:44:55 +00:00
|
|
|
AsyncStorage.multiSet(batch, err1 => {
|
2016-02-06 00:44:44 +00:00
|
|
|
// yes, twice on purpose
|
2018-05-10 22:44:55 +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`);
|
2018-05-10 22:44:55 +00:00
|
|
|
updateMessage(
|
|
|
|
'multiGet([key_1, key_2]) correctly returned ' +
|
|
|
|
JSON.stringify(result),
|
|
|
|
);
|
2015-06-03 23:57:08 +00:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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() {
|
2018-05-10 22:44:55 +00:00
|
|
|
done = () =>
|
|
|
|
this.setState({done: true}, () => {
|
|
|
|
TestModule.markTestCompleted();
|
|
|
|
});
|
|
|
|
updateMessage = msg => {
|
2015-03-18 22:57:49 +00:00
|
|
|
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>
|
2018-05-10 22:44:55 +00:00
|
|
|
{/* $FlowFixMe(>=0.54.0 site=react_native_fb,react_native_oss) This
|
2017-09-06 10:25:01 +00:00
|
|
|
* comment suppresses an error found when Flow v0.54 was deployed.
|
|
|
|
* To see the error delete this comment and run Flow. */
|
2018-05-10 22:44:55 +00:00
|
|
|
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;
|