forked from kevinresol/react-native-default-preference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
145 lines (122 loc) · 5.17 KB
/
index.test.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import { NativeModules, Platform } from 'react-native';
import DefaultPreference from './index';
const mockDefaultPreference = require('./__mocks__/react-native-default-preference');
const { RNDefaultPreference } = NativeModules;
jest.mock('react-native', () => {
const mockDefaultPreference = require('./__mocks__/react-native-default-preference');
return {
NativeModules: {
RNDefaultPreference: {
setName: mockDefaultPreference.setName,
getName: mockDefaultPreference.getName,
get: mockDefaultPreference.get,
set: mockDefaultPreference.set,
clear: mockDefaultPreference.clear,
getMultiple: mockDefaultPreference.getMultiple,
setMultiple: mockDefaultPreference.setMultiple,
clearMultiple: mockDefaultPreference.clearMultiple,
getAll: mockDefaultPreference.getAll,
clearAll: mockDefaultPreference.clearAll,
},
},
Platform: {
OS: 'ios', // Default to iOS; will override in tests
},
};
});
describe.each(['ios', 'android'])('DefaultPreference on %s', (platform) => {
beforeAll(() => {
// Override Platform.OS for each test suite
(Platform as any).OS = platform;
});
beforeEach(() => {
jest.clearAllMocks();
mockDefaultPreference.reset(); // Reset all mock preferences
RNDefaultPreference.clearAll();
});
it('should set and get a value for the default instance', async () => {
await DefaultPreference.set('key1', 'value1');
const value = await DefaultPreference.get('key1');
expect(value).toBe('value1');
});
it('should set and get a value for the group instance', async () => {
await DefaultPreference.setName('group.reactnative.example');
await DefaultPreference.set('key1', 'value1');
const value = await DefaultPreference.get('key1');
expect(value).toBe('value1');
});
it('should clear a value for the default instance', async () => {
await DefaultPreference.set('key2', 'value2');
await DefaultPreference.clear('key2');
const value = await DefaultPreference.get('key2');
expect(value).toBeNull();
});
it('should clear a value for the group instance', async () => {
await DefaultPreference.setName('group.reactnative.example');
await DefaultPreference.set('key2', 'value2');
await DefaultPreference.clear('key2');
const value = await DefaultPreference.get('key2');
expect(value).toBeNull();
});
it('should set and get multiple values for the default instance', async () => {
const data = { key3: 'value3', key4: 'value4' };
await DefaultPreference.setMultiple(data);
const values = await DefaultPreference.getMultiple(['key3', 'key4']);
expect(values).toEqual(['value3', 'value4']);
});
it('should clear multiple values for the default instance', async () => {
const data = { key5: 'value5', key6: 'value6' };
await DefaultPreference.setMultiple(data);
await DefaultPreference.clearMultiple(['key5', 'key6']);
const values = await DefaultPreference.getMultiple(['key5', 'key6']);
expect(values).toEqual([null, null]);
});
it('should get all values for the default instance', async () => {
const data = { key7: 'value7', key8: 'value8' };
await DefaultPreference.setMultiple(data);
const allValues = await DefaultPreference.getAll();
expect(allValues).toEqual(data);
});
it('should clear all values for the default instance', async () => {
const data = { key9: 'value9', key10: 'value10' };
await DefaultPreference.setMultiple(data);
await DefaultPreference.clearAll();
const allValues = await DefaultPreference.getAll();
expect(allValues).toEqual({});
});
it('should not set the name if no name is provided', () => {
expect(RNDefaultPreference.setName).not.toHaveBeenCalled();
});
it('should set the name if a name is provided', async () => {
await DefaultPreference.setName('group.reactnative.example');
expect(RNDefaultPreference.setName).toHaveBeenCalledWith('group.reactnative.example');
});
it('should not call setName if name is default', () => {
expect(RNDefaultPreference.setName).not.toHaveBeenCalled();
});
it('should call setName if name is not default', async () => {
await DefaultPreference.setName('customName');
expect(RNDefaultPreference.setName).toHaveBeenCalledWith('customName');
});
it('should handle multiple group names correctly', async () => {
await DefaultPreference.setName('group1');
await DefaultPreference.set('key1', 'value1');
const value1 = await DefaultPreference.get('key1');
await DefaultPreference.setName('group2');
await DefaultPreference.set('key1', 'value2');
const value2 = await DefaultPreference.get('key1');
expect(value1).toBe('value1');
expect(value2).toBe('value2');
});
it('should get the current group name', async () => {
// Arrange
const currentGroupName = 'testGroup';
mockDefaultPreference.getName.mockResolvedValue(currentGroupName);
// Act
await DefaultPreference.setName(currentGroupName);
const groupName = await DefaultPreference.getName();
// Assert
expect(RNDefaultPreference.setName).toHaveBeenCalledWith(currentGroupName);
expect(groupName).toBe(currentGroupName);
});
});