-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests to cover exception handling in secure-storage; Fixed…
… a minor issue (#276)
- Loading branch information
Showing
3 changed files
with
107 additions
and
1 deletion.
There are no files selected for viewing
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,34 @@ | ||
import { mocked } from 'ts-jest/utils' | ||
|
||
import { setItem, getItem } from './storage' | ||
const { saveObject, getObject } = jest.requireActual('./index') | ||
|
||
jest.mock('./storage', () => ({ | ||
setItem: jest.fn(), | ||
getItem: jest.fn(), | ||
})) | ||
|
||
const getItemMocked = mocked(getItem) | ||
const setItemMocked = mocked(setItem) | ||
|
||
describe('storage', () => { | ||
afterEach(() => { | ||
getItemMocked.mockClear() | ||
setItemMocked.mockClear() | ||
}) | ||
|
||
it('saveObject to reject when exception occurs in setItem', async () => { | ||
setItemMocked.mockRejectedValueOnce('setItem error') | ||
await expect(saveObject('foo', 'bar')).rejects.toEqual('setItem error') | ||
}) | ||
|
||
it('getObject to return null when exception occurs in getItem', async () => { | ||
getItemMocked.mockRejectedValueOnce('getItem error') | ||
await expect(getObject('foo')).resolves.toBeNull() | ||
}) | ||
|
||
it('getObject to return null when exception occurs in JSON.parse', async () => { | ||
getItemMocked.mockResolvedValueOnce('bad json') | ||
await expect(getObject('foo')).resolves.toBeNull() | ||
}) | ||
}) |
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,72 @@ | ||
import { writeAsStringAsync, readAsStringAsync } from 'expo-file-system' | ||
import { getItemAsync, setItemAsync } from 'expo-secure-store' | ||
import { mocked } from 'ts-jest/utils' | ||
const { setItem, getItem } = jest.requireActual('./storage') | ||
|
||
jest.mock('expo-file-system', () => ({ | ||
writeAsStringAsync: jest.fn(), | ||
getInfoAsync: jest.fn().mockReturnValue({ exists: true }), | ||
readAsStringAsync: jest.fn(), | ||
})) | ||
|
||
jest.mock('expo-secure-store', () => ({ | ||
getItemAsync: jest.fn(), | ||
setItemAsync: jest.fn(), | ||
})) | ||
|
||
global.crypto = { | ||
getRandomValues: jest.fn().mockReturnValue(new Uint8Array(32)), | ||
} | ||
|
||
const writeAsStringAsyncMocked = mocked(writeAsStringAsync) | ||
const readAsStringAsyncMocked = mocked(readAsStringAsync) | ||
const getItemAsyncMocked = mocked(getItemAsync) | ||
const setItemAsyncMocked = mocked(setItemAsync) | ||
|
||
describe('storage', () => { | ||
afterEach(() => { | ||
writeAsStringAsyncMocked.mockClear() | ||
readAsStringAsyncMocked.mockClear() | ||
getItemAsyncMocked.mockClear() | ||
setItemAsyncMocked.mockClear() | ||
}) | ||
|
||
it('setItem to reject when exception occurs in readAsStringAsync', async () => { | ||
readAsStringAsyncMocked.mockRejectedValueOnce('readAsStringAsync error') | ||
await expect(setItem('foo', 'bar')).rejects.toEqual( | ||
'readAsStringAsync error' | ||
) | ||
}) | ||
|
||
it('setItem to reject when exception occurs in setItemAsync', async () => { | ||
setItemAsyncMocked.mockRejectedValueOnce('setItemAsync error') | ||
readAsStringAsyncMocked.mockReturnValueOnce(null) | ||
await expect(setItem('foo', 'bar')).rejects.toEqual('setItemAsync error') | ||
}) | ||
|
||
it('setItem to reject when exception occurs in writeAsStringAsync', async () => { | ||
setItemAsyncMocked.mockResolvedValueOnce() | ||
readAsStringAsyncMocked.mockReturnValueOnce(null) | ||
writeAsStringAsyncMocked.mockRejectedValueOnce('writeAsStringAsync error') | ||
await expect(setItem('foo', 'bar')).rejects.toEqual( | ||
'writeAsStringAsync error' | ||
) | ||
}) | ||
|
||
it('getItem to reject when exception occurs in readAsStringAsync', async () => { | ||
readAsStringAsyncMocked.mockRejectedValueOnce('readAsStringAsync error') | ||
await expect(getItem('foo')).rejects.toEqual('readAsStringAsync error') | ||
}) | ||
|
||
it('getItem to reject when getItemAsync returns null', async () => { | ||
readAsStringAsyncMocked.mockResolvedValueOnce('encrypted_storage_key') | ||
getItemAsyncMocked.mockResolvedValueOnce(null) | ||
await expect(getItem('foo')).rejects.toEqual('Storage key not found') | ||
}) | ||
|
||
it('getItem to reject when exception occurs in getItemAsyncMocked', async () => { | ||
readAsStringAsyncMocked.mockResolvedValueOnce('encrypted_storage_key') | ||
getItemAsyncMocked.mockRejectedValueOnce('getItemAsync error') | ||
await expect(getItem('foo')).rejects.toEqual('getItemAsync error') | ||
}) | ||
}) |
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