Skip to content

Commit cab63f8

Browse files
committed
Move related tests to new OnyxKeysTest.ts file
1 parent eef42ad commit cab63f8

2 files changed

Lines changed: 150 additions & 124 deletions

File tree

tests/unit/OnyxKeysTest.ts

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
import Onyx from '../../lib';
2+
import OnyxKeys from '../../lib/OnyxKeys';
3+
4+
const ONYXKEYS = {
5+
TEST_KEY: 'test',
6+
COLLECTION: {
7+
TEST_KEY: 'test_',
8+
TEST_LEVEL_KEY: 'test_level_',
9+
TEST_LEVEL_LAST_KEY: 'test_level_last_',
10+
ROUTES: 'routes_',
11+
RAM_ONLY_COLLECTION: 'ramOnlyCollection_',
12+
},
13+
RAM_ONLY_KEY: 'ramOnlyKey',
14+
};
15+
16+
describe('OnyxKeys', () => {
17+
beforeAll(() =>
18+
Onyx.init({
19+
keys: ONYXKEYS,
20+
ramOnlyKeys: [ONYXKEYS.RAM_ONLY_KEY, ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION],
21+
}),
22+
);
23+
24+
beforeEach(() => Onyx.clear());
25+
26+
afterEach(() => jest.clearAllMocks());
27+
28+
describe('splitCollectionMemberKey', () => {
29+
describe('should return correct values', () => {
30+
const dataResult: Record<string, [string, string]> = {
31+
test_: ['test_', ''],
32+
test_level_: ['test_level_', ''],
33+
test_level_1: ['test_level_', '1'],
34+
test_level_2: ['test_level_', '2'],
35+
test_level_last_3: ['test_level_last_', '3'],
36+
test___FAKE__: ['test_', '__FAKE__'],
37+
'test_-1_something': ['test_', '-1_something'],
38+
'test_level_-1_something': ['test_level_', '-1_something'],
39+
};
40+
41+
it.each(Object.keys(dataResult))('%s', (key) => {
42+
const [collectionKey, id] = OnyxKeys.splitCollectionMemberKey(key);
43+
expect(collectionKey).toEqual(dataResult[key][0]);
44+
expect(id).toEqual(dataResult[key][1]);
45+
});
46+
});
47+
48+
it('should throw error if key does not contain underscore', () => {
49+
expect(() => {
50+
OnyxKeys.splitCollectionMemberKey(ONYXKEYS.TEST_KEY);
51+
}).toThrow("Invalid 'test' key provided, only collection keys are allowed.");
52+
expect(() => {
53+
OnyxKeys.splitCollectionMemberKey('');
54+
}).toThrow("Invalid '' key provided, only collection keys are allowed.");
55+
});
56+
57+
it('should allow passing the collection key beforehand for performance gains', () => {
58+
const [collectionKey, id] = OnyxKeys.splitCollectionMemberKey(`${ONYXKEYS.COLLECTION.TEST_KEY}id1`, ONYXKEYS.COLLECTION.TEST_KEY);
59+
expect(collectionKey).toEqual(ONYXKEYS.COLLECTION.TEST_KEY);
60+
expect(id).toEqual('id1');
61+
});
62+
63+
it("should throw error if the passed collection key isn't compatible with the key", () => {
64+
expect(() => {
65+
OnyxKeys.splitCollectionMemberKey(`${ONYXKEYS.COLLECTION.TEST_KEY}id1`, ONYXKEYS.COLLECTION.TEST_LEVEL_KEY);
66+
}).toThrow("Invalid 'test_level_' collection key provided, it isn't compatible with 'test_id1' key.");
67+
});
68+
});
69+
70+
describe('getCollectionKey', () => {
71+
describe('should return correct values', () => {
72+
const dataResult: Record<string, string> = {
73+
test_: 'test_',
74+
test_level_: 'test_level_',
75+
test_level_1: 'test_level_',
76+
test_level_2: 'test_level_',
77+
test_level_last_3: 'test_level_last_',
78+
test___FAKE__: 'test_',
79+
'test_-1_something': 'test_',
80+
'test_level_-1_something': 'test_level_',
81+
};
82+
83+
it.each(Object.keys(dataResult))('%s', (key) => {
84+
const collectionKey = OnyxKeys.getCollectionKey(key);
85+
expect(collectionKey).toEqual(dataResult[key]);
86+
});
87+
});
88+
89+
it('should return undefined if key does not contain underscore', () => {
90+
expect(OnyxKeys.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeUndefined();
91+
expect(OnyxKeys.getCollectionKey('')).toBeUndefined();
92+
});
93+
});
94+
95+
describe('isCollectionMember', () => {
96+
it('should return true for collection member keys', () => {
97+
expect(OnyxKeys.isCollectionMember('test_123')).toBe(true);
98+
expect(OnyxKeys.isCollectionMember('test_level_456')).toBe(true);
99+
expect(OnyxKeys.isCollectionMember('test_level_last_789')).toBe(true);
100+
expect(OnyxKeys.isCollectionMember('test_-1_something')).toBe(true);
101+
expect(OnyxKeys.isCollectionMember('routes_abc')).toBe(true);
102+
});
103+
104+
it('should return false for collection keys themselves', () => {
105+
expect(OnyxKeys.isCollectionMember('test_')).toBe(false);
106+
expect(OnyxKeys.isCollectionMember('test_level_')).toBe(false);
107+
expect(OnyxKeys.isCollectionMember('test_level_last_')).toBe(false);
108+
expect(OnyxKeys.isCollectionMember('routes_')).toBe(false);
109+
});
110+
111+
it('should return false for non-collection keys', () => {
112+
expect(OnyxKeys.isCollectionMember('test')).toBe(false);
113+
expect(OnyxKeys.isCollectionMember('someRegularKey')).toBe(false);
114+
expect(OnyxKeys.isCollectionMember('notACollection')).toBe(false);
115+
expect(OnyxKeys.isCollectionMember('')).toBe(false);
116+
});
117+
118+
it('should return false for invalid keys', () => {
119+
expect(OnyxKeys.isCollectionMember('invalid_key_123')).toBe(false);
120+
expect(OnyxKeys.isCollectionMember('notregistered_')).toBe(false);
121+
expect(OnyxKeys.isCollectionMember('notregistered_123')).toBe(false);
122+
});
123+
});
124+
125+
describe('isRamOnlyKey', () => {
126+
it('should return true for RAM-only key', () => {
127+
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.RAM_ONLY_KEY)).toBeTruthy();
128+
});
129+
130+
it('should return true for RAM-only collection', () => {
131+
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION)).toBeTruthy();
132+
});
133+
134+
it('should return true for RAM-only collection member', () => {
135+
expect(OnyxKeys.isRamOnlyKey(`${ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION}1`)).toBeTruthy();
136+
});
137+
138+
it('should return false for a normal key', () => {
139+
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.TEST_KEY)).toBeFalsy();
140+
});
141+
142+
it('should return false for normal collection', () => {
143+
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.COLLECTION.TEST_KEY)).toBeFalsy();
144+
});
145+
146+
it('should return false for normal collection member', () => {
147+
expect(OnyxKeys.isRamOnlyKey(`${ONYXKEYS.COLLECTION.TEST_KEY}1`)).toBeFalsy();
148+
});
149+
});
150+
});

tests/unit/onyxUtilsTest.ts

Lines changed: 0 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {act} from '@testing-library/react-native';
22
import Onyx from '../../lib';
3-
import OnyxKeys from '../../lib/OnyxKeys';
43
import OnyxUtils from '../../lib/OnyxUtils';
54
import type {GenericDeepRecord} from '../types';
65
import utils from '../../lib/utils';
@@ -95,48 +94,6 @@ describe('OnyxUtils', () => {
9594

9695
afterEach(() => jest.clearAllMocks());
9796

98-
describe('splitCollectionMemberKey', () => {
99-
describe('should return correct values', () => {
100-
const dataResult: Record<string, [string, string]> = {
101-
test_: ['test_', ''],
102-
test_level_: ['test_level_', ''],
103-
test_level_1: ['test_level_', '1'],
104-
test_level_2: ['test_level_', '2'],
105-
test_level_last_3: ['test_level_last_', '3'],
106-
test___FAKE__: ['test_', '__FAKE__'],
107-
'test_-1_something': ['test_', '-1_something'],
108-
'test_level_-1_something': ['test_level_', '-1_something'],
109-
};
110-
111-
it.each(Object.keys(dataResult))('%s', (key) => {
112-
const [collectionKey, id] = OnyxKeys.splitCollectionMemberKey(key);
113-
expect(collectionKey).toEqual(dataResult[key][0]);
114-
expect(id).toEqual(dataResult[key][1]);
115-
});
116-
});
117-
118-
it('should throw error if key does not contain underscore', () => {
119-
expect(() => {
120-
OnyxKeys.splitCollectionMemberKey(ONYXKEYS.TEST_KEY);
121-
}).toThrowError("Invalid 'test' key provided, only collection keys are allowed.");
122-
expect(() => {
123-
OnyxKeys.splitCollectionMemberKey('');
124-
}).toThrowError("Invalid '' key provided, only collection keys are allowed.");
125-
});
126-
127-
it('should allow passing the collection key beforehand for performance gains', () => {
128-
const [collectionKey, id] = OnyxKeys.splitCollectionMemberKey(`${ONYXKEYS.COLLECTION.TEST_KEY}id1`, ONYXKEYS.COLLECTION.TEST_KEY);
129-
expect(collectionKey).toEqual(ONYXKEYS.COLLECTION.TEST_KEY);
130-
expect(id).toEqual('id1');
131-
});
132-
133-
it("should throw error if the passed collection key isn't compatible with the key", () => {
134-
expect(() => {
135-
OnyxKeys.splitCollectionMemberKey(`${ONYXKEYS.COLLECTION.TEST_KEY}id1`, ONYXKEYS.COLLECTION.TEST_LEVEL_KEY);
136-
}).toThrowError("Invalid 'test_level_' collection key provided, it isn't compatible with 'test_id1' key.");
137-
});
138-
});
139-
14097
describe('partialSetCollection', () => {
14198
beforeEach(() => {
14299
Onyx.clear();
@@ -324,61 +281,6 @@ describe('OnyxUtils', () => {
324281
});
325282
});
326283

327-
describe('getCollectionKey', () => {
328-
describe('should return correct values', () => {
329-
const dataResult: Record<string, string> = {
330-
test_: 'test_',
331-
test_level_: 'test_level_',
332-
test_level_1: 'test_level_',
333-
test_level_2: 'test_level_',
334-
test_level_last_3: 'test_level_last_',
335-
test___FAKE__: 'test_',
336-
'test_-1_something': 'test_',
337-
'test_level_-1_something': 'test_level_',
338-
};
339-
340-
it.each(Object.keys(dataResult))('%s', (key) => {
341-
const collectionKey = OnyxKeys.getCollectionKey(key);
342-
expect(collectionKey).toEqual(dataResult[key]);
343-
});
344-
});
345-
346-
it('should return undefined if key does not contain underscore', () => {
347-
expect(OnyxKeys.getCollectionKey(ONYXKEYS.TEST_KEY)).toBeUndefined();
348-
expect(OnyxKeys.getCollectionKey('')).toBeUndefined();
349-
});
350-
});
351-
352-
describe('isCollectionMember', () => {
353-
it('should return true for collection member keys', () => {
354-
expect(OnyxKeys.isCollectionMember('test_123')).toBe(true);
355-
expect(OnyxKeys.isCollectionMember('test_level_456')).toBe(true);
356-
expect(OnyxKeys.isCollectionMember('test_level_last_789')).toBe(true);
357-
expect(OnyxKeys.isCollectionMember('test_-1_something')).toBe(true);
358-
expect(OnyxKeys.isCollectionMember('routes_abc')).toBe(true);
359-
});
360-
361-
it('should return false for collection keys themselves', () => {
362-
expect(OnyxKeys.isCollectionMember('test_')).toBe(false);
363-
expect(OnyxKeys.isCollectionMember('test_level_')).toBe(false);
364-
expect(OnyxKeys.isCollectionMember('test_level_last_')).toBe(false);
365-
expect(OnyxKeys.isCollectionMember('routes_')).toBe(false);
366-
});
367-
368-
it('should return false for non-collection keys', () => {
369-
expect(OnyxKeys.isCollectionMember('test')).toBe(false);
370-
expect(OnyxKeys.isCollectionMember('someRegularKey')).toBe(false);
371-
expect(OnyxKeys.isCollectionMember('notACollection')).toBe(false);
372-
expect(OnyxKeys.isCollectionMember('')).toBe(false);
373-
});
374-
375-
it('should return false for invalid keys', () => {
376-
expect(OnyxKeys.isCollectionMember('invalid_key_123')).toBe(false);
377-
expect(OnyxKeys.isCollectionMember('notregistered_')).toBe(false);
378-
expect(OnyxKeys.isCollectionMember('notregistered_123')).toBe(false);
379-
});
380-
});
381-
382284
describe('mergeChanges', () => {
383285
it("should return the last change if it's an array", () => {
384286
const {result} = OnyxUtils.mergeChanges([...testMergeChanges, [0, 1, 2]], testObject);
@@ -493,32 +395,6 @@ describe('OnyxUtils', () => {
493395
});
494396
});
495397

496-
describe('isRamOnlyKey', () => {
497-
it('should return true for RAM-only key', () => {
498-
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.RAM_ONLY_KEY)).toBeTruthy();
499-
});
500-
501-
it('should return true for RAM-only collection', () => {
502-
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION)).toBeTruthy();
503-
});
504-
505-
it('should return true for RAM-only collection member', () => {
506-
expect(OnyxKeys.isRamOnlyKey(`${ONYXKEYS.COLLECTION.RAM_ONLY_COLLECTION}1`)).toBeTruthy();
507-
});
508-
509-
it('should return false for a normal key', () => {
510-
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.TEST_KEY)).toBeFalsy();
511-
});
512-
513-
it('should return false for normal collection', () => {
514-
expect(OnyxKeys.isRamOnlyKey(ONYXKEYS.COLLECTION.TEST_KEY)).toBeFalsy();
515-
});
516-
517-
it('should return false for normal collection member', () => {
518-
expect(OnyxKeys.isRamOnlyKey(`${ONYXKEYS.COLLECTION.TEST_KEY}1`)).toBeFalsy();
519-
});
520-
});
521-
522398
describe('afterInit', () => {
523399
beforeEach(() => {
524400
// Resets the deferred init task before each test.

0 commit comments

Comments
 (0)