1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
2
import { getId } from '../../../../utils/id' ;
3
- import { removeUndefinedProps } from '../../../../utils/object' ;
3
+ import { omitUndefined } from '../../../../utils/object' ;
4
4
5
5
import { addAutoGenParams } from './autoGen' ;
6
6
@@ -9,7 +9,9 @@ jest.mock('../../../../utils/id', () => ({
9
9
} ) ) ;
10
10
11
11
jest . mock ( '../../../../utils/object' , ( ) => ( {
12
- removeUndefinedProps : jest . fn ( ( obj ) => obj ) ,
12
+ omitUndefined : jest . fn ( ( obj ) =>
13
+ Object . fromEntries ( Object . entries ( obj ) . filter ( ( [ _ , value ] ) => value !== undefined ) ) ,
14
+ ) ,
13
15
} ) ) ;
14
16
15
17
describe ( 'single table model: addAutoGenParams' , ( ) => {
@@ -30,7 +32,7 @@ describe('single table model: addAutoGenParams', () => {
30
32
} ) ;
31
33
32
34
it ( 'should correctly generate values using UUID generator' , ( ) => {
33
- const values = { id : '123' , name : 'John Doe' } ;
35
+ const values = { name : 'John Doe' } ;
34
36
const genConfig = { id : 'UUID' } as const ;
35
37
36
38
( getId as jest . Mock ) . mockReturnValue ( 'mocked-uuid' ) ;
@@ -42,7 +44,7 @@ describe('single table model: addAutoGenParams', () => {
42
44
} ) ;
43
45
44
46
it ( 'should correctly generate values using KSUID generator' , ( ) => {
45
- const values = { id : '123' , name : 'John Doe' } ;
47
+ const values = { name : 'John Doe' } ;
46
48
const genConfig = { id : 'KSUID' } as const ;
47
49
48
50
( getId as jest . Mock ) . mockReturnValue ( 'mocked-ksuid' ) ;
@@ -54,7 +56,7 @@ describe('single table model: addAutoGenParams', () => {
54
56
} ) ;
55
57
56
58
it ( 'should correctly generate values using timestamp generator' , ( ) => {
57
- const values = { id : '123' , name : 'John Doe' , createdAt : '' } ;
59
+ const values = { id : '123' , name : 'John Doe' } ;
58
60
const genConfig = { createdAt : 'timestamp' } as const ;
59
61
60
62
const mockTimestamp = '2024-01-01T00:00:00.000Z' ;
@@ -69,7 +71,7 @@ describe('single table model: addAutoGenParams', () => {
69
71
} ) ;
70
72
71
73
it ( 'should correctly generate values using count generator' , ( ) => {
72
- const values = { id : '123' , name : 'John Doe' , counter : 5 } ;
74
+ const values = { id : '123' , name : 'John Doe' } ;
73
75
const genConfig = { counter : 'count' } as const ;
74
76
75
77
const result = addAutoGenParams ( values , genConfig ) ;
@@ -78,20 +80,42 @@ describe('single table model: addAutoGenParams', () => {
78
80
} ) ;
79
81
80
82
it ( 'should handle custom function generators' , ( ) => {
81
- const values = { id : '123' , name : 'John Doe' , custom : 'initial' } ;
83
+ const values = { id : '123' , name : 'John Doe' } ;
82
84
const genConfig = { custom : ( ) => 'custom-generated-value' } as const ;
83
85
84
86
const result = addAutoGenParams ( values , genConfig ) ;
85
87
86
88
expect ( result ) . toEqual ( { id : '123' , name : 'John Doe' , custom : 'custom-generated-value' } ) ;
87
89
} ) ;
88
90
89
- it ( 'should remove undefined properties using removeUndefinedProps ' , ( ) => {
91
+ it ( 'should remove undefined properties using omitUndefined ' , ( ) => {
90
92
const values = { name : 'John Doe' , some : undefined } ;
91
93
const genConfig = { age : 'count' } as const ;
92
94
93
95
addAutoGenParams ( values , genConfig ) ;
94
96
95
- expect ( removeUndefinedProps ) . toHaveBeenCalledWith ( { name : 'John Doe' , age : 0 } ) ;
97
+ expect ( omitUndefined ) . toHaveBeenCalledWith ( { name : 'John Doe' , age : 0 } ) ;
98
+ } ) ;
99
+
100
+ it ( 'should not autogenerate if value is present' , ( ) => {
101
+ const values = { id : '123' , name : 'John Doe' } ;
102
+ const genConfig = { id : 'UUID' } as const ;
103
+
104
+ ( getId as jest . Mock ) . mockReturnValueOnce ( 'mocked-uuid' ) ;
105
+
106
+ const result = addAutoGenParams ( values , genConfig ) ;
107
+
108
+ expect ( result ) . toEqual ( { id : '123' , name : 'John Doe' } ) ;
109
+ } ) ;
110
+
111
+ it ( 'should autogenerate if value is present only as undefined' , ( ) => {
112
+ const values = { id : undefined , name : 'John Doe' } ;
113
+ const genConfig = { id : 'UUID' } as const ;
114
+
115
+ ( getId as jest . Mock ) . mockReturnValueOnce ( 'mocked-uuid' ) ;
116
+
117
+ const result = addAutoGenParams ( values , genConfig ) ;
118
+
119
+ expect ( result ) . toEqual ( { id : 'mocked-uuid' , name : 'John Doe' } ) ;
96
120
} ) ;
97
121
} ) ;
0 commit comments