|
| 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 | +}); |
0 commit comments