File size: 2,915 Bytes
d9494a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { removeUndefinedFields } from '../removeUndefinedFields';

interface PrimitiveTestContext {
  description: string;
  input: null | undefined | string | number | boolean;
  expected: null | undefined | string | number | boolean;
}

interface ObjectTestContext {
  description: string;
  input: Record<string, unknown>;
  expected: Record<string, unknown>;
}

describe('removeUndefinedFields', () => {
  describe.each<PrimitiveTestContext>([
    {
      description: 'null',
      input: null,
      expected: null,
    },
    {
      description: 'undefined',
      input: undefined,
      expected: undefined,
    },
    {
      description: 'string',
      input: 'string',
      expected: 'string',
    },
    {
      description: 'number',
      input: 123,
      expected: 123,
    },
    {
      description: 'boolean true',
      input: true,
      expected: true,
    },
    {
      description: 'boolean false',
      input: false,
      expected: false,
    },
  ])('primitive value: $description', ({ input, expected }) => {
    it('should return the value as is', () => {
      expect(removeUndefinedFields(input)).toBe(expected);
    });
  });

  describe.each<ObjectTestContext>([
    {
      description: 'flat object',
      input: {
        name: 'John',
        age: 30,
        email: undefined,
        phone: null,
        address: undefined,
      },
      expected: {
        name: 'John',
        age: 30,
        phone: null,
      },
    },
    {
      description: 'nested object',
      input: {
        name: 'John',
        contact: {
          email: undefined,
          phone: '123456',
          address: {
            street: '123 Main St',
            apt: undefined,
            city: 'New York',
          },
        },
        preferences: undefined,
      },
      expected: {
        name: 'John',
        contact: {
          phone: '123456',
          address: {
            street: '123 Main St',
            city: 'New York',
          },
        },
      },
    },
    {
      description: 'arrays',
      input: {
        names: ['John', undefined, 'Jane', null],
        tags: [
          { id: 1, label: 'active' },
          { id: undefined, label: 'pending' },
          undefined,
          { id: 3, label: undefined },
        ],
      },
      expected: {
        names: ['John', 'Jane', null],
        tags: [{ id: 1, label: 'active' }, { label: 'pending' }, { id: 3 }],
      },
    },
    {
      description: 'empty objects after cleaning',
      input: {
        name: 'John',
        metadata: {
          tags: undefined,
          flags: undefined,
        },
        settings: {},
      },
      expected: {
        name: 'John',
      },
    },
  ])('object case: $description', ({ input, expected }) => {
    it('should clean undefined fields correctly', () => {
      expect(removeUndefinedFields(input)).toEqual(expected);
    });
  });
});