|
| 1 | +import assert from 'node:assert'; |
| 2 | +import { describe, it } from 'node:test'; |
| 3 | +import { stripTypename } from '../../src/utils/strip-typename'; |
| 4 | + |
| 5 | +describe('stripTypename', () => { |
| 6 | + it('removes __typename from objects and nested structures', () => { |
| 7 | + const input = { |
| 8 | + __typename: 'Root', |
| 9 | + id: '1', |
| 10 | + name: 'Test', |
| 11 | + nested: { |
| 12 | + __typename: 'Nested', |
| 13 | + value: 42, |
| 14 | + array: [ |
| 15 | + { __typename: 'Item', itemId: 'a' }, |
| 16 | + { __typename: 'Item', itemId: 'b' }, |
| 17 | + ], |
| 18 | + }, |
| 19 | + list: [ |
| 20 | + { __typename: 'ListItem', listId: 'x' }, |
| 21 | + { __typename: 'ListItem', listId: 'y' }, |
| 22 | + ], |
| 23 | + }; |
| 24 | + |
| 25 | + const expectedOutput = { |
| 26 | + id: '1', |
| 27 | + name: 'Test', |
| 28 | + nested: { |
| 29 | + value: 42, |
| 30 | + array: [{ itemId: 'a' }, { itemId: 'b' }], |
| 31 | + }, |
| 32 | + list: [{ listId: 'x' }, { listId: 'y' }], |
| 33 | + }; |
| 34 | + |
| 35 | + const output = stripTypename(input); |
| 36 | + assert.deepStrictEqual(output, expectedOutput); |
| 37 | + }); |
| 38 | + |
| 39 | + it('handles null and primitive values correctly', () => { |
| 40 | + assert.strictEqual(stripTypename(null), null); |
| 41 | + assert.strictEqual(stripTypename(42), 42); |
| 42 | + assert.strictEqual(stripTypename('string'), 'string'); |
| 43 | + assert.strictEqual(stripTypename(true), true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('handles arrays of primitives correctly', () => { |
| 47 | + const input = [1, 2, 3, { __typename: 'Item', id: 'a' }]; |
| 48 | + const expectedOutput = [1, 2, 3, { id: 'a' }]; |
| 49 | + const output = stripTypename(input); |
| 50 | + assert.deepStrictEqual(output, expectedOutput); |
| 51 | + }); |
| 52 | +}); |
0 commit comments