diff --git a/example/e2e/asyncstorage.e2e.js b/example/e2e/asyncstorage.e2e.js index 1280c3b7..b4da8c64 100644 --- a/example/e2e/asyncstorage.e2e.js +++ b/example/e2e/asyncstorage.e2e.js @@ -9,18 +9,25 @@ describe('Async Storage', () => { let restartButton; + let changeStorageButton; let closeKeyboard; let test_getSetClear; let test_mergeItem; + let isAndroid; beforeAll(async () => { await device.reloadReactNative(); restartButton = await element(by.id('restart_button')); + changeStorageButton = await element(by.id('change_storage')); closeKeyboard = await element(by.id('closeKeyboard')); test_getSetClear = await element(by.id('testType_getSetClear')); test_mergeItem = await element(by.id('testType_mergeItem')); + isAndroid = device.getPlatform() === 'android'; }); + const testRunOnlyOnIos = test => (isAndroid ? test.skip : test); + const testRunOnlyOnAndroid = test => (isAndroid ? test : test.skip); + it('should load default screen', async () => { await expect(restartButton).toExist(); await expect(closeKeyboard).toExist(); @@ -28,249 +35,311 @@ describe('Async Storage', () => { await expect(test_mergeItem).toExist(); }); - describe('get / set / clear item test', () => { - beforeAll(async () => { - if (device.getPlatform() === 'ios') { - await device.openURL({url: 'rnc-asyncstorage://unset-delegate'}); + const beforeAllgetSetClearTests = async () => { + if (!isAndroid) { + await device.openURL({url: 'rnc-asyncstorage://unset-delegate'}); + } + await test_getSetClear.tap(); + }; + const getSetClearTestsShouldBeVisibleTest = async () => { + await expect(element(by.id('clear_button'))).toExist(); + await expect(element(by.id('increaseByTen_button'))).toExist(); + await expect(element(by.id('storedNumber_text'))).toExist(); + }; + const getSetClearTestsShouldStoreValueInAsyncStorageTest = async () => { + const storedNumberText = await element(by.id('storedNumber_text')); + const increaseByTenButton = await element(by.id('increaseByTen_button')); + + await expect(storedNumberText).toHaveText(''); + + const tapTimes = Math.round(Math.random() * 9) + 1; + + for (let i = 0; i < tapTimes; i++) { + await increaseByTenButton.tap(); + } + + await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); + await restartButton.tap(); + await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); + }; + const getSetClearTestsShouldClearItemTest = async () => { + const storedNumberText = await element(by.id('storedNumber_text')); + const increaseByTenButton = await element(by.id('increaseByTen_button')); + const clearButton = await element(by.id('clear_button')); + + await increaseByTenButton.tap(); + await clearButton.tap(); + await restartButton.tap(); + await expect(storedNumberText).toHaveText(''); + }; + + const beforeAllmergeItemTests = async () => { + if (!isAndroid) { + await device.openURL({url: 'rnc-asyncstorage://unset-delegate'}); + } + await test_mergeItem.tap(); + }; + const mergeItemTestsShouldBeVisibleTest = async () => { + await expect(element(by.id('saveItem_button'))).toExist(); + await expect(element(by.id('mergeItem_button'))).toExist(); + await expect(element(by.id('restoreItem_button'))).toExist(); + await expect(element(by.id('testInput-name'))).toExist(); + await expect(element(by.id('testInput-age'))).toExist(); + await expect(element(by.id('testInput-eyes'))).toExist(); + await expect(element(by.id('testInput-shoe'))).toExist(); + }; + const mergeItemTestsShouldMergeItemsInAsyncStorage = async () => { + const buttonSaveItem = await element(by.id('saveItem_button')); + const buttonMergeItem = await element(by.id('mergeItem_button')); + const buttonRestoreItem = await element(by.id('restoreItem_button')); + + const nameInput = await element(by.id('testInput-name')); + const ageInput = await element(by.id('testInput-age')); + const eyesInput = await element(by.id('testInput-eyes')); + const shoeInput = await element(by.id('testInput-shoe')); + const storyText = await element(by.id('storyTextView')); + + async function performInput() { + const name = Math.random() > 0.5 ? 'Jerry' : 'Sarah'; + const age = Math.random() > 0.5 ? '21' : '23'; + const eyesColor = Math.random() > 0.5 ? 'blue' : 'green'; + const shoeSize = Math.random() > 0.5 ? '9' : '10'; + + if (!isAndroid) { + await eyesInput.tap(); } - await test_getSetClear.tap(); - }); + await nameInput.typeText(name); + await closeKeyboard.tap(); - it('should be visible', async () => { - await expect(element(by.id('clear_button'))).toExist(); - await expect(element(by.id('increaseByTen_button'))).toExist(); - await expect(element(by.id('storedNumber_text'))).toExist(); - }); + if (!isAndroid) { + await eyesInput.tap(); + } + await ageInput.typeText(age); + await closeKeyboard.tap(); - it('should store value in async storage', async () => { - const storedNumberText = await element(by.id('storedNumber_text')); - const increaseByTenButton = await element(by.id('increaseByTen_button')); + if (!isAndroid) { + await eyesInput.tap(); + } + await eyesInput.typeText(eyesColor); + await closeKeyboard.tap(); - await expect(storedNumberText).toHaveText(''); + if (!isAndroid) { + await eyesInput.tap(); + } + await shoeInput.typeText(shoeSize); + await closeKeyboard.tap(); - const tapTimes = Math.round(Math.random() * 9) + 1; + return `${name} is ${age}, has ${eyesColor} eyes and shoe size of ${shoeSize}.`; + } - for (let i = 0; i < tapTimes; i++) { - await increaseByTenButton.tap(); - } + const story = await performInput(); + await buttonSaveItem.tap(); + await restartButton.tap(); + await buttonRestoreItem.tap(); + expect(storyText).toHaveText(story); + await restartButton.tap(); - await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); - await restartButton.tap(); - await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); - }); + // merging here - it('should clear item', async () => { - const storedNumberText = await element(by.id('storedNumber_text')); - const increaseByTenButton = await element(by.id('increaseByTen_button')); - const clearButton = await element(by.id('clear_button')); + const newStory = await performInput(); + + await buttonMergeItem.tap(); + await restartButton.tap(); + await buttonRestoreItem.tap(); + expect(storyText).toHaveText(newStory); + }; + + const beforeAllgetSetClearItemDelegateTests = async () => { + await test_getSetClear.tap(); + if (isAndroid) { + // Not yet supported. + return; + } + + await device.openURL({url: 'rnc-asyncstorage://set-delegate'}); + }; + const getSetClearItemDelegateShouldStoreValueWithDelegateTest = async () => { + const storedNumberText = await element(by.id('storedNumber_text')); + const increaseByTenButton = await element(by.id('increaseByTen_button')); + await expect(storedNumberText).toHaveText(''); + + const tapTimes = Math.round(Math.random() * 9) + 1; + + for (let i = 0; i < tapTimes; i++) { await increaseByTenButton.tap(); - await clearButton.tap(); - await restartButton.tap(); - await expect(storedNumberText).toHaveText(''); - }); - }); + } + + await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); + await restartButton.tap(); + + // The delegate will distinguish itself by always returning the stored value + 1000000 + await expect(storedNumberText).toHaveText(`${tapTimes * 10 + 1000000}`); + }; + const getSetClearItemDelegateShouldClearItemWithDelegateTest = async () => { + const storedNumberText = await element(by.id('storedNumber_text')); + const increaseByTenButton = await element(by.id('increaseByTen_button')); + const clearButton = await element(by.id('clear_button')); + + await increaseByTenButton.tap(); + await clearButton.tap(); + await restartButton.tap(); + + // The delegate will distinguish itself by actually setting storing 1000000 + // instead of clearing. It will also always return the stored value + 1000000. + await expect(storedNumberText).toHaveText('2000000'); + }; + + const beforeAllMergeItemDelegateTest = async () => { + if (!isAndroid) { + await device.openURL({url: 'rnc-asyncstorage://set-delegate'}); + } + await test_mergeItem.tap(); + }; + const mergeItemDelegateShouldMergeItemsWithDelegateTest = async () => { + const buttonMergeItem = await element(by.id('mergeItem_button')); + const buttonRestoreItem = await element(by.id('restoreItem_button')); + + const nameInput = await element(by.id('testInput-name')); + const ageInput = await element(by.id('testInput-age')); + const eyesInput = await element(by.id('testInput-eyes')); + const shoeInput = await element(by.id('testInput-shoe')); + const storyText = await element(by.id('storyTextView')); + + async function performInput() { + const name = Math.random() > 0.5 ? 'Jerry' : 'Sarah'; + const age = Math.random() > 0.5 ? '21' : '23'; + const eyesColor = Math.random() > 0.5 ? 'blue' : 'green'; + const shoeSize = Math.random() > 0.5 ? '9' : '10'; + + if (!isAndroid) { + await eyesInput.tap(); + } + await nameInput.typeText(name); + await closeKeyboard.tap(); - describe('merge item test', () => { - beforeAll(async () => { - if (device.getPlatform() === 'ios') { - await device.openURL({url: 'rnc-asyncstorage://unset-delegate'}); + if (!isAndroid) { + await eyesInput.tap(); } - await test_mergeItem.tap(); - }); + await ageInput.typeText(age); + await closeKeyboard.tap(); - it('should be visible', async () => { - await expect(element(by.id('saveItem_button'))).toExist(); - await expect(element(by.id('mergeItem_button'))).toExist(); - await expect(element(by.id('restoreItem_button'))).toExist(); - await expect(element(by.id('testInput-name'))).toExist(); - await expect(element(by.id('testInput-age'))).toExist(); - await expect(element(by.id('testInput-eyes'))).toExist(); - await expect(element(by.id('testInput-shoe'))).toExist(); - }); + if (!isAndroid) { + await eyesInput.tap(); + } + await eyesInput.typeText(eyesColor); + await closeKeyboard.tap(); - it('should merge items in async storage', async () => { - const buttonSaveItem = await element(by.id('saveItem_button')); - const buttonMergeItem = await element(by.id('mergeItem_button')); - const buttonRestoreItem = await element(by.id('restoreItem_button')); - - const nameInput = await element(by.id('testInput-name')); - const ageInput = await element(by.id('testInput-age')); - const eyesInput = await element(by.id('testInput-eyes')); - const shoeInput = await element(by.id('testInput-shoe')); - const storyText = await element(by.id('storyTextView')); - - const isAndroid = device.getPlatform() === 'android'; - - async function performInput() { - const name = Math.random() > 0.5 ? 'Jerry' : 'Sarah'; - const age = Math.random() > 0.5 ? '21' : '23'; - const eyesColor = Math.random() > 0.5 ? 'blue' : 'green'; - const shoeSize = Math.random() > 0.5 ? '9' : '10'; - - if (!isAndroid) { - await eyesInput.tap(); - } - await nameInput.typeText(name); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await ageInput.typeText(age); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await eyesInput.typeText(eyesColor); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await shoeInput.typeText(shoeSize); - await closeKeyboard.tap(); - - return `${name} is ${age}, has ${eyesColor} eyes and shoe size of ${shoeSize}.`; + if (!isAndroid) { + await eyesInput.tap(); } + await shoeInput.typeText(shoeSize); + await closeKeyboard.tap(); + + return `${name} from delegate is ${age} from delegate, has ${eyesColor} eyes and shoe size of ${shoeSize}.`; + } - const story = await performInput(); - await buttonSaveItem.tap(); - await restartButton.tap(); - await buttonRestoreItem.tap(); - expect(storyText).toHaveText(story); - await restartButton.tap(); + const story = await performInput(); + await buttonMergeItem.tap(); + await restartButton.tap(); + await buttonRestoreItem.tap(); + expect(storyText).toHaveText(story); + }; - // merging here + describe('storage location: default - get / set / clear item test', () => { + beforeAll(beforeAllgetSetClearTests); + + it('should be visible', async () => { + await getSetClearTestsShouldBeVisibleTest(); + }); - const newStory = await performInput(); + it('should store value in async storage', async () => { + await getSetClearTestsShouldStoreValueInAsyncStorageTest(); + }); - await buttonMergeItem.tap(); - await restartButton.tap(); - await buttonRestoreItem.tap(); - expect(storyText).toHaveText(newStory); + it('should clear item', async () => { + await getSetClearTestsShouldClearItemTest(); }); }); - describe('get / set / clear item delegate test', () => { - beforeAll(async () => { - await test_getSetClear.tap(); - if (device.getPlatform() === 'android') { - // Not yet supported. - return; - } + describe('storage location: default - merge item test', () => { + beforeAll(beforeAllmergeItemTests); - await device.openURL({url: 'rnc-asyncstorage://set-delegate'}); + it('should be visible', async () => { + await mergeItemTestsShouldBeVisibleTest(); }); - it('should store value with delegate', async () => { - if (device.getPlatform() === 'android') { - // Not yet supported. - return; - } + it('should merge items in async storage', async () => { + await mergeItemTestsShouldMergeItemsInAsyncStorage(); + }); + }); - const storedNumberText = await element(by.id('storedNumber_text')); - const increaseByTenButton = await element(by.id('increaseByTen_button')); + describe('storage location: default - get / set / clear item delegate test', () => { + beforeAll(beforeAllgetSetClearItemDelegateTests); - await expect(storedNumberText).toHaveText(''); + testRunOnlyOnAndroid('should store value with delegate', async () => { + await getSetClearItemDelegateShouldStoreValueWithDelegateTest(); + }); - const tapTimes = Math.round(Math.random() * 9) + 1; + testRunOnlyOnAndroid('should clear item with delegate', async () => { + await getSetClearItemDelegateShouldClearItemWithDelegateTest(); + }); + }); - for (let i = 0; i < tapTimes; i++) { - await increaseByTenButton.tap(); - } + describe('storage location: default - merge item delegate test', () => { + beforeAll(beforeAllMergeItemDelegateTest); + + testRunOnlyOnAndroid('should merge items with delegate', async () => { + await mergeItemDelegateShouldMergeItemsWithDelegateTest(); + }); + }); - await expect(storedNumberText).toHaveText(`${tapTimes * 10}`); - await restartButton.tap(); + describe('storage location: applicationSupport - get / set / clear item test', () => { + beforeAll(beforeAllgetSetClearTests); - // The delegate will distinguish itself by always returning the stored value + 1000000 - await expect(storedNumberText).toHaveText(`${tapTimes * 10 + 1000000}`); + testRunOnlyOnIos(isAndroid, 'should be visible', async () => { + await changeStorageButton.tap(); + await getSetClearTestsShouldBeVisibleTest(); }); - it('should clear item with delegate', async () => { - if (device.getPlatform() === 'android') { - // Not yet supported. - return; - } + testRunOnlyOnIos('should store value in async storage', async () => { + await getSetClearTestsShouldStoreValueInAsyncStorageTest(); + }); - const storedNumberText = await element(by.id('storedNumber_text')); - const increaseByTenButton = await element(by.id('increaseByTen_button')); - const clearButton = await element(by.id('clear_button')); + testRunOnlyOnIos('should clear item', async () => { + await getSetClearTestsShouldClearItemTest(); + }); + }); - await increaseByTenButton.tap(); - await clearButton.tap(); - await restartButton.tap(); + describe('storage location: applicationSupport - merge item test', () => { + beforeAll(beforeAllmergeItemTests); - // The delegate will distinguish itself by actually setting storing 1000000 - // instead of clearing. It will also always return the stored value + 1000000. - await expect(storedNumberText).toHaveText('2000000'); + testRunOnlyOnIos('should be visible', async () => { + await mergeItemTestsShouldBeVisibleTest(); + }); + + testRunOnlyOnIos('should merge items in async storage', async () => { + await mergeItemTestsShouldMergeItemsInAsyncStorage(); }); }); - describe('merge item delegate test', () => { - beforeAll(async () => { - if (device.getPlatform() === 'ios') { - await device.openURL({url: 'rnc-asyncstorage://set-delegate'}); - } - await test_mergeItem.tap(); + describe('storage location: applicationSupport - get / set / clear item delegate test', () => { + beforeAll(beforeAllgetSetClearItemDelegateTests); + + testRunOnlyOnAndroid('should store value with delegate', async () => { + await getSetClearItemDelegateShouldStoreValueWithDelegateTest(); }); - it('should merge items with delegate', async () => { - if (device.getPlatform() === 'android') { - // Not yet supported. - return; - } + testRunOnlyOnAndroid('should clear item with delegate', async () => { + await getSetClearItemDelegateShouldClearItemWithDelegateTest(); + }); + }); - const buttonMergeItem = await element(by.id('mergeItem_button')); - const buttonRestoreItem = await element(by.id('restoreItem_button')); - - const nameInput = await element(by.id('testInput-name')); - const ageInput = await element(by.id('testInput-age')); - const eyesInput = await element(by.id('testInput-eyes')); - const shoeInput = await element(by.id('testInput-shoe')); - const storyText = await element(by.id('storyTextView')); - - const isAndroid = device.getPlatform() === 'android'; - - async function performInput() { - const name = Math.random() > 0.5 ? 'Jerry' : 'Sarah'; - const age = Math.random() > 0.5 ? '21' : '23'; - const eyesColor = Math.random() > 0.5 ? 'blue' : 'green'; - const shoeSize = Math.random() > 0.5 ? '9' : '10'; - - if (!isAndroid) { - await eyesInput.tap(); - } - await nameInput.typeText(name); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await ageInput.typeText(age); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await eyesInput.typeText(eyesColor); - await closeKeyboard.tap(); - - if (!isAndroid) { - await eyesInput.tap(); - } - await shoeInput.typeText(shoeSize); - await closeKeyboard.tap(); - - return `${name} from delegate is ${age} from delegate, has ${eyesColor} eyes and shoe size of ${shoeSize}.`; - } + describe('storage location: applicationSupport - merge item delegate test', () => { + beforeAll(beforeAllMergeItemDelegateTest); - const story = await performInput(); - await buttonMergeItem.tap(); - await restartButton.tap(); - await buttonRestoreItem.tap(); - expect(storyText).toHaveText(story); + testRunOnlyOnAndroid('should merge items with delegate', async () => { + await mergeItemDelegateShouldMergeItemsWithDelegateTest(); }); }); }); diff --git a/example/src/App.js b/example/src/App.js index a4464835..a3a5b6c2 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -18,6 +18,7 @@ import { Keyboard, Button, } from 'react-native'; +import AsyncStorage from '@react-native-community/async-storage'; import GetSetClear from './examples/GetSetClear'; import MergeItem from './examples/MergeItem'; @@ -41,13 +42,37 @@ const TESTS = { }, }; +const STORAGE_OPTIONS = { + documents: 'Documents', + applicationSupport: 'Application Support', +}; + type Props = {}; -type State = {restarting: boolean, currentTest: Object}; +type State = { + restarting: boolean, + currentTest: Object, + storageLocation: string, +}; export default class App extends Component { state = { restarting: false, currentTest: TESTS.GetSetClear, + storageLocation: STORAGE_OPTIONS.documents, + }; + + _changeStorage = () => { + if (this.state.storageLocation === STORAGE_OPTIONS.documents) { + AsyncStorage.setStorageLocationIOS( + AsyncStorage.StorageLocationIOS.applicationSupport, + ); + this.setState({storageLocation: STORAGE_OPTIONS.applicationSupport}); + } else { + AsyncStorage.setStorageLocationIOS( + AsyncStorage.StorageLocationIOS.documents, + ); + this.setState({storageLocation: STORAGE_OPTIONS.documents}); + } }; _simulateRestart = () => { @@ -68,13 +93,23 @@ export default class App extends Component { testID="closeKeyboard" /> - - Simulate Restart - + + + Storage location: {this.state.storageLocation} + + + + Simulate Restart + +