1import { NativeModulesProxy } from 'expo-modules-core';
2
3import * as SecureStore from '../SecureStore';
4
5it(`sets values`, async () => {
6  const testKey = 'key-test_0.0';
7  const testValue = 'value `~!@#$%^&*();:\'"-_.,<>';
8  const options = { keychainService: 'test' };
9  await SecureStore.setItemAsync(testKey, testValue, options);
10
11  expect(NativeModulesProxy.ExpoSecureStore.setValueWithKeyAsync).toHaveBeenCalledTimes(1);
12  expect(NativeModulesProxy.ExpoSecureStore.setValueWithKeyAsync).toHaveBeenCalledWith(
13    testValue,
14    testKey,
15    options
16  );
17});
18
19it(`provides default options when setting values`, async () => {
20  await SecureStore.setItemAsync('key', 'value');
21  expect(NativeModulesProxy.ExpoSecureStore.setValueWithKeyAsync).toHaveBeenCalledWith(
22    'value',
23    'key',
24    {}
25  );
26});
27
28it(`gets values`, async () => {
29  NativeModulesProxy.ExpoSecureStore.getValueWithKeyAsync.mockImplementation(async () => 'value');
30
31  const options = { keychainService: 'test' };
32  const result = await SecureStore.getItemAsync('key', options);
33  expect(result).toBe('value');
34  expect(NativeModulesProxy.ExpoSecureStore.getValueWithKeyAsync).toHaveBeenCalledWith(
35    'key',
36    options
37  );
38});
39
40it(`deletes values`, async () => {
41  const options = { keychainService: 'test' };
42  await SecureStore.deleteItemAsync('key', options);
43  expect(NativeModulesProxy.ExpoSecureStore.deleteValueWithKeyAsync).toHaveBeenCalledWith(
44    'key',
45    options
46  );
47});
48
49it(`checks for invalid keys`, async () => {
50  NativeModulesProxy.ExpoSecureStore.getValueWithKeyAsync.mockImplementation(
51    async () => `unexpected value`
52  );
53
54  await expect(SecureStore.getItemAsync(null as any)).rejects.toMatchSnapshot();
55  await expect(SecureStore.getItemAsync(true as any)).rejects.toMatchSnapshot();
56  await expect(SecureStore.getItemAsync({} as any)).rejects.toMatchSnapshot();
57  await expect(SecureStore.getItemAsync((() => {}) as any)).rejects.toMatchSnapshot();
58  await expect(SecureStore.getItemAsync('@')).rejects.toMatchSnapshot();
59
60  expect(NativeModulesProxy.ExpoSecureStore.getValueWithKeyAsync).not.toHaveBeenCalled();
61});
62
63it(`checks for invalid values`, async () => {
64  await expect(SecureStore.setItemAsync('key', null as any)).rejects.toMatchSnapshot();
65  await expect(SecureStore.setItemAsync('key', true as any)).rejects.toMatchSnapshot();
66  await expect(SecureStore.setItemAsync('key', {} as any)).rejects.toMatchSnapshot();
67  await expect(SecureStore.setItemAsync('key', (() => {}) as any)).rejects.toMatchSnapshot();
68
69  expect(NativeModulesProxy.ExpoSecureStore.setValueWithKeyAsync).not.toHaveBeenCalled();
70});
71