From 4102102e117c69ccd3bb7d006d4471ecaf220e71 Mon Sep 17 00:00:00 2001 From: Ashish Nanda Date: Wed, 11 Nov 2020 14:29:52 -0800 Subject: [PATCH 1/7] Migrate to AsyncStorage from community (#7118) --- packages/amazon-cognito-identity-js/package.json | 6 +++++- .../amazon-cognito-identity-js/src/StorageHelper-rn.js | 2 +- packages/cache/__mocks__/AsyncStorage.js | 4 +++- packages/cache/src/AsyncStorageCache.ts | 2 +- packages/core/package.json | 4 ++++ packages/core/src/RNComponents/reactnative.ts | 4 +++- packages/core/src/StorageHelper/reactnative.ts | 2 +- .../src/storage/adapter/InMemoryStore.native.ts | 4 ++-- .../pushnotification/__tests__/PushNotification-test.ts | 9 +++++---- packages/pushnotification/src/PushNotification.ts | 2 +- 10 files changed, 26 insertions(+), 13 deletions(-) diff --git a/packages/amazon-cognito-identity-js/package.json b/packages/amazon-cognito-identity-js/package.json index e4d02082133..466aca855b8 100644 --- a/packages/amazon-cognito-identity-js/package.json +++ b/packages/amazon-cognito-identity-js/package.json @@ -74,6 +74,7 @@ "@babel/cli": "^7.7.4", "@babel/core": "^7.7.4", "@babel/preset-env": "^7.7.4", + "@react-native-async-storage/async-storage": "^1.13.0", "babel-loader": "^8.0.6", "cross-env": "^3.1.4", "eslint": "^3.19.0", @@ -86,10 +87,13 @@ "eslint-plugin-standard": "^3.0.1", "jsdoc": "^3.4.0", "react": "^16.0.0", - "react-native": "^0.44.0", + "react-native": "^0.59.0", "rimraf": "^2.5.4", "webpack": "^3.5.5" }, + "peerDependencies": { + "@react-native-async-storage/async-storage": "^1.13.0" + }, "jest": { "globals": { "ts-jest": { diff --git a/packages/amazon-cognito-identity-js/src/StorageHelper-rn.js b/packages/amazon-cognito-identity-js/src/StorageHelper-rn.js index 221b92a1f4c..02c35391cd1 100644 --- a/packages/amazon-cognito-identity-js/src/StorageHelper-rn.js +++ b/packages/amazon-cognito-identity-js/src/StorageHelper-rn.js @@ -15,7 +15,7 @@ * limitations under the License. */ -import { AsyncStorage } from 'react-native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; const MEMORY_KEY_PREFIX = '@MemoryStorage:'; let dataMemory = {}; diff --git a/packages/cache/__mocks__/AsyncStorage.js b/packages/cache/__mocks__/AsyncStorage.js index 40dbbfe5565..484929fe26a 100644 --- a/packages/cache/__mocks__/AsyncStorage.js +++ b/packages/cache/__mocks__/AsyncStorage.js @@ -1,4 +1,6 @@ -const AsyncStorage = jest.genMockFromModule('react-native'); +const AsyncStorage = jest.genMockFromModule( + '@react-native-async-storage/async-storage' +); var store = {}; var curSize = 0; diff --git a/packages/cache/src/AsyncStorageCache.ts b/packages/cache/src/AsyncStorageCache.ts index 0b417454658..c872038e4eb 100644 --- a/packages/cache/src/AsyncStorageCache.ts +++ b/packages/cache/src/AsyncStorageCache.ts @@ -13,7 +13,7 @@ import { StorageCache } from './StorageCache'; import { defaultConfig, getCurrTime } from './Utils'; -import { AsyncStorage } from 'react-native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; import { ICache } from './types'; import { ConsoleLogger as Logger } from '@aws-amplify/core'; diff --git a/packages/core/package.json b/packages/core/package.json index a0560d02a15..a01da057f52 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -48,6 +48,7 @@ }, "homepage": "https://aws-amplify.github.io/", "devDependencies": { + "@react-native-async-storage/async-storage": "^1.13.0", "find": "^0.2.7", "genversion": "^2.2.0", "prepend-file": "^1.3.1", @@ -65,6 +66,9 @@ "url": "^0.11.0", "zen-observable-ts": "0.8.19" }, + "peerDependencies": { + "@react-native-async-storage/async-storage": "^1.13.0" + }, "jest": { "globals": { "ts-jest": { diff --git a/packages/core/src/RNComponents/reactnative.ts b/packages/core/src/RNComponents/reactnative.ts index 9d926b57a6d..54756a8afa1 100644 --- a/packages/core/src/RNComponents/reactnative.ts +++ b/packages/core/src/RNComponents/reactnative.ts @@ -11,4 +11,6 @@ * and limitations under the License. */ -export { Linking, AppState, AsyncStorage } from 'react-native'; +import { Linking, AppState } from 'react-native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; +export { Linking, AppState, AsyncStorage }; diff --git a/packages/core/src/StorageHelper/reactnative.ts b/packages/core/src/StorageHelper/reactnative.ts index 37841baa4c5..5c8af297aee 100644 --- a/packages/core/src/StorageHelper/reactnative.ts +++ b/packages/core/src/StorageHelper/reactnative.ts @@ -10,7 +10,7 @@ * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions * and limitations under the License. */ -import { AsyncStorage } from 'react-native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; const MEMORY_KEY_PREFIX = '@MemoryStorage:'; let dataMemory = {}; diff --git a/packages/datastore/src/storage/adapter/InMemoryStore.native.ts b/packages/datastore/src/storage/adapter/InMemoryStore.native.ts index 799f5a6786a..7a8a506b727 100644 --- a/packages/datastore/src/storage/adapter/InMemoryStore.native.ts +++ b/packages/datastore/src/storage/adapter/InMemoryStore.native.ts @@ -1,6 +1,6 @@ -import { AsyncStorage } from 'react-native'; +import AsyncStorage from '@react-native-async-storage/async-storage'; -// See: https://reactnative.dev/docs/asyncstorage +// See: https://react-native-async-storage.github.io/async-storage/ export function createInMemoryStore() { return AsyncStorage; } diff --git a/packages/pushnotification/__tests__/PushNotification-test.ts b/packages/pushnotification/__tests__/PushNotification-test.ts index dee1f893218..57ff5debdfb 100644 --- a/packages/pushnotification/__tests__/PushNotification-test.ts +++ b/packages/pushnotification/__tests__/PushNotification-test.ts @@ -11,10 +11,6 @@ jest.mock('react-native', () => ({ currentState: 'active', addEventListener: (event, callback) => callback('active'), }, - AsyncStorage: { - getItem: () => new Promise(res => res('item')), - setItem: jest.fn(), - }, DeviceEventEmitter: { addListener: jest.fn(), }, @@ -29,6 +25,11 @@ jest.mock('react-native', () => ({ }, })); +jest.mock('@react-native-async-storage/async-storage', () => ({ + getItem: () => new Promise(res => res('item')), + setItem: jest.fn(), +})); + jest.mock('@react-native-community/push-notification-ios', () => ({ requestPermissions: () => {}, getInitialNotification: new Promise(res => res('notification')), diff --git a/packages/pushnotification/src/PushNotification.ts b/packages/pushnotification/src/PushNotification.ts index e34e85c75cd..ebdb0dd3477 100644 --- a/packages/pushnotification/src/PushNotification.ts +++ b/packages/pushnotification/src/PushNotification.ts @@ -14,11 +14,11 @@ import { NativeModules, DeviceEventEmitter, - AsyncStorage, Platform, AppState, } from 'react-native'; import PushNotificationIOS from '@react-native-community/push-notification-ios'; +import AsyncStorage from '@react-native-async-storage/async-storage'; import { Amplify, ConsoleLogger as Logger, JS } from '@aws-amplify/core'; const logger = new Logger('Notification'); From d8cfa80ba34b9b8c961706fb4538ea9b6de307bb Mon Sep 17 00:00:00 2001 From: Ivan Artemiev <29709626+iartemiev@users.noreply.github.com> Date: Tue, 2 Mar 2021 14:31:56 -0500 Subject: [PATCH 2/7] chore: temporarily disable storga multipart e2e tests --- .circleci/config.yml | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 2e138a68f2f..37710165da8 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -844,12 +844,15 @@ workflows: - build filters: <<: *releasable_branches - - integ_rn_ios_storage_multipart_progress: - requires: - - integ_setup - - build - filters: - <<: *releasable_branches + # TODO: re-enable these when merging native -> main + # Disabling because this test would require us to manually install + # @react-native-async-storage/async-storage as a dependency + # - integ_rn_ios_storage_multipart_progress: + # requires: + # - integ_setup + # - build + # filters: + # <<: *releasable_branches - integ_rn_ios_push_notifications: requires: - integ_setup @@ -862,12 +865,13 @@ workflows: - build filters: <<: *releasable_branches - - integ_rn_android_storage_multipart_progress: - requires: - - integ_setup - - build - filters: - <<: *releasable_branches + # Same callout as with the ios multipart test + # - integ_rn_android_storage_multipart_progress: + # requires: + # - integ_setup + # - build + # filters: + # <<: *releasable_branches - integ_datastore_auth: requires: - integ_setup @@ -895,10 +899,10 @@ workflows: - integ_angular_auth - integ_vue_auth - integ_rn_ios_storage - - integ_rn_ios_storage_multipart_progress + # - integ_rn_ios_storage_multipart_progress + # - integ_rn_android_storage_multipart_progress - integ_rn_ios_push_notifications - integ_rn_android_storage - - integ_rn_android_storage_multipart_progress - integ_datastore_auth - post_release: filters: From b05d109ed983aac0d952ce02570fa22d8211fa44 Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Mon, 10 May 2021 11:10:04 -0700 Subject: [PATCH 3/7] chore: re-enable skipped RN tests (#8247) --- .circleci/config.yml | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 37710165da8..cb8cbd80454 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -844,15 +844,12 @@ workflows: - build filters: <<: *releasable_branches - # TODO: re-enable these when merging native -> main - # Disabling because this test would require us to manually install - # @react-native-async-storage/async-storage as a dependency - # - integ_rn_ios_storage_multipart_progress: - # requires: - # - integ_setup - # - build - # filters: - # <<: *releasable_branches + - integ_rn_ios_storage_multipart_progress: + requires: + - integ_setup + - build + filters: + <<: *releasable_branches - integ_rn_ios_push_notifications: requires: - integ_setup @@ -865,13 +862,12 @@ workflows: - build filters: <<: *releasable_branches - # Same callout as with the ios multipart test - # - integ_rn_android_storage_multipart_progress: - # requires: - # - integ_setup - # - build - # filters: - # <<: *releasable_branches + - integ_rn_android_storage_multipart_progress: + requires: + - integ_setup + - build + filters: + <<: *releasable_branches - integ_datastore_auth: requires: - integ_setup @@ -899,8 +895,8 @@ workflows: - integ_angular_auth - integ_vue_auth - integ_rn_ios_storage - # - integ_rn_ios_storage_multipart_progress - # - integ_rn_android_storage_multipart_progress + - integ_rn_ios_storage_multipart_progress + - integ_rn_android_storage_multipart_progress - integ_rn_ios_push_notifications - integ_rn_android_storage - integ_datastore_auth From 566fd16f27428d184d4730e268aa21edd12a5650 Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Mon, 10 May 2021 12:04:32 -0700 Subject: [PATCH 4/7] update README --- README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 1d58bda0b82..79145c2224d 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,15 @@ Our default implementation works with Amazon Web Services (AWS), but AWS Amplify ### Amplify 4.x.x has breaking changes for React Native. Please see the breaking changes below: -- +- If you are using React Native or Expo, you will need add `@react-native-async-storage/async-storage` as a dependency to your application, in addition to the other React Naive dependencies: + +``` +// React Native +yarn add aws-amplify amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage + +// Expo +yarn add aws-amplify @react-native-community/netinfo @react-native-async-storage/async-storage +``` ### Amplify 3.x.x has breaking changes. Please see the breaking changes below: From e49fff6bb756cd5b1e5e556a120d0c2f19361493 Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Mon, 10 May 2021 12:15:17 -0700 Subject: [PATCH 5/7] update identity-js mocks --- .../__tests__/StorageHelperRN.test.js | 102 ++++++++---------- .../amazon-cognito-identity-js/package.json | 48 +-------- 2 files changed, 48 insertions(+), 102 deletions(-) diff --git a/packages/amazon-cognito-identity-js/__tests__/StorageHelperRN.test.js b/packages/amazon-cognito-identity-js/__tests__/StorageHelperRN.test.js index b8fc6da0c89..79f7ebc1dd2 100644 --- a/packages/amazon-cognito-identity-js/__tests__/StorageHelperRN.test.js +++ b/packages/amazon-cognito-identity-js/__tests__/StorageHelperRN.test.js @@ -5,25 +5,23 @@ describe('React native storage helper unit tests', () => { }); var items = {}; - jest.mock('react-native', () => ({ - AsyncStorage: { - setItem: jest.fn((item, value) => { - return new Promise((resolve, reject) => { - items[item] = value; - resolve(value); - }); - }), - getItem: jest.fn((item, value) => { - return new Promise((resolve, reject) => { - resolve(items[item]); - }); - }), - removeItem: jest.fn(item => { - return new Promise((resolve, reject) => { - resolve(delete items[item]); - }); - }), - }, + jest.mock('@react-native-async-storage/async-storage', () => ({ + setItem: jest.fn((item, value) => { + return new Promise((resolve, reject) => { + items[item] = value; + resolve(value); + }); + }), + getItem: jest.fn((item, value) => { + return new Promise((resolve, reject) => { + resolve(items[item]); + }); + }), + removeItem: jest.fn(item => { + return new Promise((resolve, reject) => { + resolve(delete items[item]); + }); + }), })); const StorageHelper = require('../src/StorageHelper-rn.js').default; @@ -55,24 +53,22 @@ describe('React native storage helper unit tests', () => { const items = { '@MemoryStorage:key': 'value1', }; - jest.mock('react-native', () => ({ - AsyncStorage: { - setItem: jest.fn((item, value) => { - return new Promise((resolve, reject) => { - items[item] = value; - resolve(value); - }); - }), - getAllKeys: jest.fn(callback => { - callback(null, Object.keys(items)); - }), - multiGet: jest.fn((keys, callback) => { - const values = keys.map(key => [key, items[key] || null]); - callback && callback(null, values); + jest.mock('@react-native-async-storage/async-storage', () => ({ + setItem: jest.fn((item, value) => { + return new Promise((resolve, reject) => { + items[item] = value; + resolve(value); + }); + }), + getAllKeys: jest.fn(callback => { + callback(null, Object.keys(items)); + }), + multiGet: jest.fn((keys, callback) => { + const values = keys.map(key => [key, items[key] || null]); + callback && callback(null, values); - return values; - }), - }, + return values; + }), })); const StorageHelper = require('../src/StorageHelper-rn.js').default; @@ -86,13 +82,11 @@ describe('React native storage helper unit tests', () => { }); test('Get all keys throws errors', () => { - jest.mock('react-native', () => ({ - AsyncStorage: { - getAllKeys: jest.fn(callback => { - const err = ['errKey']; - callback(err, null); - }), - }, + jest.mock('@react-native-async-storage/async-storage', () => ({ + getAllKeys: jest.fn(callback => { + const err = ['errKey']; + callback(err, null); + }), })); const StorageHelper = require('../src/StorageHelper-rn.js').default; @@ -103,19 +97,17 @@ describe('React native storage helper unit tests', () => { }); test('Multiget throws errors', () => { var items = {}; - jest.mock('react-native', () => ({ - AsyncStorage: { - getAllKeys: jest.fn(callback => { - callback(null, Object.keys(items)); - }), - multiGet: jest.fn((keys, callback) => { - const values = keys.map(key => [key, items[key] || null]); - const err = new Error('Storage Error'); - callback && callback(err, null); + jest.mock('@react-native-async-storage/async-storage', () => ({ + getAllKeys: jest.fn(callback => { + callback(null, Object.keys(items)); + }), + multiGet: jest.fn((keys, callback) => { + const values = keys.map(key => [key, items[key] || null]); + const err = new Error('Storage Error'); + callback && callback(err, null); - return values; - }), - }, + return values; + }), })); const StorageHelper = require('../src/StorageHelper-rn.js').default; diff --git a/packages/amazon-cognito-identity-js/package.json b/packages/amazon-cognito-identity-js/package.json index 23d1dd8a2c7..ff9e6ee3ec1 100644 --- a/packages/amazon-cognito-identity-js/package.json +++ b/packages/amazon-cognito-identity-js/package.json @@ -97,51 +97,5 @@ }, "peerDependencies": { "@react-native-async-storage/async-storage": "^1.13.0" - }, - "jest": { - "globals": { - "ts-jest": { - "diagnostics": false, - "tsConfig": { - "lib": [ - "es5", - "es2015", - "dom", - "esnext.asynciterable", - "es2017.object" - ], - "allowJs": true, - "esModuleInterop": true - } - } - }, - "transform": { - "^.+\\.(js|jsx|ts|tsx)$": "ts-jest" - }, - "preset": "ts-jest", - "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$", - "moduleFileExtensions": [ - "ts", - "tsx", - "js", - "json", - "jsx" - ], - "testEnvironment": "jsdom", - "testURL": "http://localhost/", - "coverageThreshold": { - "global": { - "branches": 0, - "functions": 0, - "lines": 0, - "statements": 0 - } - }, - "coveragePathIgnorePatterns": [ - "/node_modules/", - "dist", - "es", - "lib" - ] - } + } } From 01efcede3caf1c89572fd7061c8e943462068753 Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Mon, 10 May 2021 12:16:27 -0700 Subject: [PATCH 6/7] update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 79145c2224d..b06e14164e2 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ Our default implementation works with Amazon Web Services (AWS), but AWS Amplify ### Amplify 4.x.x has breaking changes for React Native. Please see the breaking changes below: -- If you are using React Native or Expo, you will need add `@react-native-async-storage/async-storage` as a dependency to your application, in addition to the other React Naive dependencies: +- If you are using React Native or Expo, you will need add `@react-native-async-storage/async-storage` as a dependency to your application, in addition to the other React Native dependencies: ``` // React Native From 23c9eec796b49a76277df979c0d6c8affe7f3480 Mon Sep 17 00:00:00 2001 From: Alex Hinson Date: Mon, 10 May 2021 13:05:39 -0700 Subject: [PATCH 7/7] add pod-install --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b06e14164e2..66ccdd182e3 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,7 @@ Our default implementation works with Amazon Web Services (AWS), but AWS Amplify ``` // React Native yarn add aws-amplify amazon-cognito-identity-js @react-native-community/netinfo @react-native-async-storage/async-storage +npx pod-install // Expo yarn add aws-amplify @react-native-community/netinfo @react-native-async-storage/async-storage