-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-native-bare] Use storage for faster calls to isLoggedIn
- Loading branch information
1 parent
c85f142
commit 1c48262
Showing
5 changed files
with
178 additions
and
1 deletion.
There are no files selected for viewing
65 changes: 64 additions & 1 deletion
65
packages/@magic-sdk/react-native-bare/src/react-native-sdk-base.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/@magic-sdk/react-native-bare/test/spec/react-native-sdk-base/constructor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import browserEnv from '@ikscodes/browser-env'; | ||
import AsyncStorage from '@react-native-async-storage/async-storage'; | ||
import { UserModule } from '@magic-sdk/provider'; | ||
import { createMagicSDK } from '../../factories'; | ||
import { reactNativeStyleSheetStub } from '../../mocks'; | ||
|
||
beforeEach(() => { | ||
browserEnv.restore(); | ||
reactNativeStyleSheetStub(); | ||
}); | ||
|
||
test('SDKBaseReactNative constructor sets up UserModule instance when useStorageCacheMobile is true', () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
// Ensure that the UserModule instance is created | ||
expect(magic.usr).toBeDefined(); | ||
expect(magic.usr).toBeInstanceOf(UserModule); | ||
}); | ||
|
||
test('Returns true if the cached value of isLoggedIn is true', async () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
AsyncStorage.getItem = jest.fn().mockResolvedValue('true'); | ||
const isLoggedInResult = await magic.user.isLoggedIn(); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(isLoggedInResult).toBe(true); | ||
}); | ||
|
||
test('Saves isLoggedIn=true in cache when user is logged in', async () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
AsyncStorage.getItem = jest.fn().mockResolvedValue('false'); | ||
jest.spyOn(magic.usr, 'isLoggedIn').mockResolvedValue(true); | ||
const isLoggedInResult = await magic.user.isLoggedIn(); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(AsyncStorage.setItem).toHaveBeenCalledWith('isLoggedIn', 'true'); | ||
expect(isLoggedInResult).toBe(true); | ||
}); | ||
|
||
test('Removes isLoggedIn from cache when user is not logged in', async () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
AsyncStorage.getItem = jest.fn().mockResolvedValue('true'); | ||
jest.spyOn(magic.usr, 'isLoggedIn').mockResolvedValue(false); | ||
const isLoggedInResult = await magic.user.isLoggedIn(); | ||
|
||
expect(AsyncStorage.getItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(AsyncStorage.removeItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(isLoggedInResult).toBe(true); | ||
}); | ||
|
||
test('Removes isLoggedIn from cache user logs out', async () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
const emitUserLoggedOutSpy = jest.spyOn(magic, 'emitUserLoggedOut'); | ||
AsyncStorage.getItem = jest.fn().mockResolvedValue('false'); | ||
jest.spyOn(magic.usr, 'logout').mockResolvedValue(true); | ||
const isLoggedOut = await magic.user.logout(); | ||
|
||
expect(AsyncStorage.removeItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(emitUserLoggedOutSpy).toHaveBeenCalledWith(isLoggedOut); | ||
expect(isLoggedOut).toBe(true); | ||
}); | ||
|
||
test('Rejects with error if an error is thrown', async () => { | ||
const magic = createMagicSDK({ useStorageCacheMobile: true }); | ||
|
||
const emitUserLoggedOutSpy = jest.spyOn(magic, 'emitUserLoggedOut'); | ||
AsyncStorage.getItem = jest.fn().mockResolvedValue('false'); | ||
jest.spyOn(magic.usr, 'logout').mockRejectedValue(new Error('something went wrong')); | ||
|
||
await expect(magic.user.logout()).rejects.toThrowError('something went wrong'); | ||
expect(AsyncStorage.removeItem).toHaveBeenCalledWith('isLoggedIn'); | ||
expect(emitUserLoggedOutSpy).not.toHaveBeenCalled(); // Since logout threw an error, emitUserLoggedOut should not be called | ||
}); |
16 changes: 16 additions & 0 deletions
16
...es/@magic-sdk/react-native-bare/test/spec/react-native-sdk-base/emitUserLoggedOut.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import browserEnv from '@ikscodes/browser-env'; | ||
import { createMagicSDK } from '../../factories'; | ||
import { reactNativeStyleSheetStub } from '../../mocks'; | ||
|
||
beforeEach(() => { | ||
browserEnv.restore(); | ||
reactNativeStyleSheetStub(); | ||
}); | ||
|
||
test('emitUserLoggedOut emits event', () => { | ||
const magic = createMagicSDK(); | ||
const callbackMock = jest.fn(); | ||
magic.onUserLoggedOut(callbackMock); | ||
magic.emitUserLoggedOut(true); | ||
expect(callbackMock).toHaveBeenCalledWith(true); | ||
}); |
17 changes: 17 additions & 0 deletions
17
...ages/@magic-sdk/react-native-bare/test/spec/react-native-sdk-base/onUserLoggedOut.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import browserEnv from '@ikscodes/browser-env'; | ||
import { createMagicSDK } from '../../factories'; | ||
import { reactNativeStyleSheetStub } from '../../mocks'; | ||
|
||
beforeEach(() => { | ||
browserEnv.restore(); | ||
reactNativeStyleSheetStub(); | ||
}); | ||
|
||
test('onUserLoggedOut adds callback', () => { | ||
const magic = createMagicSDK(); | ||
const callbackMock = jest.fn(); | ||
magic.onUserLoggedOut(callbackMock); | ||
const callbacks = magic.userLoggedOutCallbacks; | ||
expect(callbacks).toHaveLength(1); | ||
expect(callbacks[0]).toBe(callbackMock); | ||
}); |