Skip to content

Commit 62bf9ef

Browse files
committed
Add tests for removeTypePrefix
1 parent 5fd929b commit 62bf9ef

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { describe, expect, test } from 'vitest';
2+
import { removeTypePrefix } from '../index.js';
3+
4+
describe('removeTypePrefix()', () => {
5+
test('basic functionality', () => {
6+
const input = {
7+
__typename: 'T',
8+
T__p1: 'p1',
9+
T__p2: 42,
10+
T__p3: ['p3', 32],
11+
T__p4: { nested: 'p4' },
12+
T__p5: { nested: ['p5'] },
13+
ShouldBeKept__p6: 'p6',
14+
};
15+
const expected = {
16+
__typename: 'T',
17+
p1: 'p1',
18+
p2: 42,
19+
p3: ['p3', 32],
20+
p4: { nested: 'p4' },
21+
p5: { nested: ['p5'] },
22+
ShouldBeKept__p6: 'p6',
23+
};
24+
expect(removeTypePrefix(input)).toStrictEqual(expected);
25+
});
26+
27+
test('should remove prefixes only in the same level', () => {
28+
const input = {
29+
__typename: 'T',
30+
T__p1: { T_shouldBeKept: 'shouldBeKept' },
31+
};
32+
const expected = {
33+
__typename: 'T',
34+
p1: { T_shouldBeKept: 'shouldBeKept' },
35+
};
36+
expect(removeTypePrefix(input)).toStrictEqual(expected);
37+
});
38+
39+
test('should work for nested objects', () => {
40+
const input = {
41+
__typename: 'T',
42+
T__p1: {
43+
__typename: 'U',
44+
U__p1: 'p1',
45+
U__p2: {
46+
__typename: 'V',
47+
V__p1: 'p1',
48+
},
49+
},
50+
T__p2: [{ __typename: 'U', U__p1: 'p1' }],
51+
};
52+
const expected = {
53+
__typename: 'T',
54+
p1: {
55+
__typename: 'U',
56+
p1: 'p1',
57+
p2: {
58+
__typename: 'V',
59+
p1: 'p1',
60+
},
61+
},
62+
p2: [{ __typename: 'U', p1: 'p1' }],
63+
};
64+
expect(removeTypePrefix(input)).toStrictEqual(expected);
65+
});
66+
67+
test('should not do anything if __typename is not found', () => {
68+
const input = {
69+
T__p1: 'hello',
70+
T__p2: 42,
71+
T__p3: ['hello', 32],
72+
T__p4: { nested: 'nested' },
73+
T__p5: { nested: ['hello'] },
74+
};
75+
76+
expect(removeTypePrefix(input)).toStrictEqual(input);
77+
});
78+
});

packages/optimizely-cms-sdk/src/graph/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,11 @@ type GetLinksResponse = {
144144
*
145145
* @param obj - The object to process (typically a GraphQL response)
146146
* @returns A new object with prefixes removed, or the original value for primitives
147+
*
148+
* Note: this function is exported only on this level for testing purposes.
149+
* It should not be exported in the user-facing API
147150
*/
148-
function removeTypePrefix(obj: any): any {
151+
export function removeTypePrefix(obj: any): any {
149152
if (Array.isArray(obj)) {
150153
return obj.map((e) => removeTypePrefix(e));
151154
}

0 commit comments

Comments
 (0)