UNPKG

3.33 kBJavaScriptView Raw
1import { NativeModules } from 'react-native';
2import * as SecureStore from '../SecureStore';
3
4import { mockPlatformIOS } from '../../test/mocking';
5
6it(`sets values`, async () => {
7 const testKey = 'key-test_0.0';
8 const testValue = 'value `~!@#$%^&*();:\'"-_.,<>';
9 const options = { keychainService: 'test' };
10 await SecureStore.setItemAsync(testKey, testValue, options);
11
12 expect(NativeModules.ExponentSecureStore.setValueWithKeyAsync).toHaveBeenCalledTimes(1);
13 expect(NativeModules.ExponentSecureStore.setValueWithKeyAsync).toHaveBeenCalledWith(
14 testValue,
15 testKey,
16 options
17 );
18});
19
20it(`provides default options when setting values`, async () => {
21 await SecureStore.setItemAsync('key', 'value');
22 expect(NativeModules.ExponentSecureStore.setValueWithKeyAsync).toHaveBeenCalledWith(
23 'value',
24 'key',
25 {}
26 );
27});
28
29it(`gets values`, async () => {
30 NativeModules.ExponentSecureStore.getValueWithKeyAsync.mockImplementation(async () => 'value');
31
32 const options = { keychainService: 'test' };
33 const result = await SecureStore.getItemAsync('key', options);
34 expect(result).toBe('value');
35 expect(NativeModules.ExponentSecureStore.getValueWithKeyAsync).toHaveBeenCalledWith(
36 'key',
37 options
38 );
39});
40
41it(`deletes values`, async () => {
42 const options = { keychainService: 'test' };
43 await SecureStore.deleteItemAsync('key', options);
44 expect(NativeModules.ExponentSecureStore.deleteValueWithKeyAsync).toHaveBeenCalledWith(
45 'key',
46 options
47 );
48});
49
50it(`checks for invalid keys`, async () => {
51 NativeModules.ExponentSecureStore.getValueWithKeyAsync.mockImplementation(
52 async () => `unexpected value`
53 );
54
55 await expect(SecureStore.getItemAsync(null)).rejects.toMatchSnapshot();
56 await expect(SecureStore.getItemAsync(true)).rejects.toMatchSnapshot();
57 await expect(SecureStore.getItemAsync({})).rejects.toMatchSnapshot();
58 await expect(SecureStore.getItemAsync(() => {})).rejects.toMatchSnapshot();
59 await expect(SecureStore.getItemAsync('@')).rejects.toMatchSnapshot();
60
61 expect(NativeModules.ExponentSecureStore.getValueWithKeyAsync).not.toHaveBeenCalled();
62});
63
64it(`checks for invalid values`, async () => {
65 await expect(SecureStore.setItemAsync('key', null)).rejects.toMatchSnapshot();
66 await expect(SecureStore.setItemAsync('key', true)).rejects.toMatchSnapshot();
67 await expect(SecureStore.setItemAsync('key', {})).rejects.toMatchSnapshot();
68 await expect(SecureStore.setItemAsync('key', () => {})).rejects.toMatchSnapshot();
69
70 expect(NativeModules.ExponentSecureStore.setValueWithKeyAsync).not.toHaveBeenCalled();
71});
72
73it(`exports accessibility options on iOS`, () => {
74 mockPlatformIOS();
75
76 expect(SecureStore.AFTER_FIRST_UNLOCK).toMatchSnapshot('AFTER_FIRST_UNLOCK');
77 expect(SecureStore.AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY).toMatchSnapshot(
78 'AFTER_FIRST_UNLOCK_THIS_DEVICE_ONLY'
79 );
80 expect(SecureStore.ALWAYS).toMatchSnapshot('ALWAYS');
81 expect(SecureStore.ALWAYS_THIS_DEVICE_ONLY).toMatchSnapshot('ALWAYS_THIS_DEVICE_ONLY');
82 expect(SecureStore.WHEN_UNLOCKED).toMatchSnapshot('WHEN_UNLOCKED');
83 expect(SecureStore.WHEN_UNLOCKED_THIS_DEVICE_ONLY).toMatchSnapshot(
84 'WHEN_UNLOCKED_THIS_DEVICE_ONLY'
85 );
86 expect(SecureStore.WHEN_PASSCODE_SET_THIS_DEVICE_ONLY).toMatchSnapshot(
87 'WHEN_PASSCODE_SET_THIS_DEVICE_ONLY'
88 );
89});