-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathlastOpenedVaultStorage.test.js
More file actions
99 lines (76 loc) · 2.53 KB
/
Copy pathlastOpenedVaultStorage.test.js
File metadata and controls
99 lines (76 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import AsyncStorage from '@react-native-async-storage/async-storage'
import {
getLastOpenedVaultId,
setLastOpenedVaultId
} from './lastOpenedVaultStorage'
import { logger } from './logger'
import { ASYNC_STORAGE_KEYS } from '../constants/asyncStorageKeys'
jest.mock('@react-native-async-storage/async-storage', () => ({
setItem: jest.fn(),
getItem: jest.fn()
}))
jest.mock('./logger', () => ({
logger: {
error: jest.fn()
}
}))
describe('lastOpenedVaultStorage', () => {
const { LAST_OPENED_VAULT_ID } = ASYNC_STORAGE_KEYS
beforeEach(() => {
jest.clearAllMocks()
})
describe('setLastOpenedVaultId', () => {
it('stores the provided vault id', async () => {
AsyncStorage.setItem.mockResolvedValue()
await setLastOpenedVaultId('vault-123')
expect(AsyncStorage.setItem).toHaveBeenCalledWith(
LAST_OPENED_VAULT_ID,
'vault-123'
)
})
it('coerces a non-string vault id to a string', async () => {
AsyncStorage.setItem.mockResolvedValue()
await setLastOpenedVaultId(42)
expect(AsyncStorage.setItem).toHaveBeenCalledWith(
LAST_OPENED_VAULT_ID,
'42'
)
})
it.each([undefined, null, '', 0])(
'does not store when vault id is falsy (%p)',
async (vaultId) => {
await setLastOpenedVaultId(vaultId)
expect(AsyncStorage.setItem).not.toHaveBeenCalled()
}
)
it('logs when saving fails', async () => {
const error = new Error('write failed')
AsyncStorage.setItem.mockRejectedValue(error)
await setLastOpenedVaultId('vault-123')
expect(logger.error).toHaveBeenCalledWith(
'Error saving last opened vault id:',
error
)
})
})
describe('getLastOpenedVaultId', () => {
it('returns the stored vault id', async () => {
AsyncStorage.getItem.mockResolvedValue('vault-123')
await expect(getLastOpenedVaultId()).resolves.toBe('vault-123')
expect(AsyncStorage.getItem).toHaveBeenCalledWith(LAST_OPENED_VAULT_ID)
})
it('returns null when no value is stored', async () => {
AsyncStorage.getItem.mockResolvedValue(null)
await expect(getLastOpenedVaultId()).resolves.toBeNull()
})
it('logs when loading fails and returns null', async () => {
const error = new Error('read failed')
AsyncStorage.getItem.mockRejectedValue(error)
await expect(getLastOpenedVaultId()).resolves.toBeNull()
expect(logger.error).toHaveBeenCalledWith(
'Error loading last opened vault id:',
error
)
})
})
})