1
1
import { AttributeValue } from "@aws-sdk/client-dynamodb" ;
2
2
3
- import { convertToAttr } from "./convertToAttr" ;
4
3
import { marshall } from "./marshall" ;
5
-
6
- jest . mock ( "./convertToAttr" ) ;
4
+ import { NumberValue } from "./NumberValue" ;
7
5
8
6
describe ( "marshall" , ( ) => {
9
- const mockOutput = { S : "mockOutput" } ;
10
- ( convertToAttr as jest . Mock ) . mockReturnValue ( { M : mockOutput } ) ;
11
-
12
- afterEach ( ( ) => {
13
- jest . clearAllMocks ( ) ;
14
- } ) ;
15
-
16
7
it ( "with object as an input" , ( ) => {
17
8
const input = { a : "A" , b : "B" } ;
18
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
19
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
20
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
9
+ expect ( marshall ( input ) ) . toEqual ( {
10
+ a : { S : "A" } ,
11
+ b : { S : "B" } ,
12
+ } ) ;
21
13
} ) ;
22
14
23
15
[ "convertEmptyValues" , "removeUndefinedValues" ] . forEach ( ( option ) => {
24
16
describe ( `options.${ option } ` , ( ) => {
25
17
[ false , true ] . forEach ( ( value ) => {
26
18
it ( `passes ${ value } to convertToAttr` , ( ) => {
27
19
const input = { a : "A" , b : "B" } ;
28
- expect ( marshall ( input , { [ option ] : value } ) ) . toEqual ( mockOutput ) ;
29
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
30
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , { [ option ] : value } ) ;
20
+ expect ( marshall ( input , { [ option ] : value } ) ) . toEqual ( {
21
+ a : { S : "A" } ,
22
+ b : { S : "B" } ,
23
+ } ) ;
31
24
} ) ;
32
25
} ) ;
33
26
} ) ;
@@ -37,9 +30,10 @@ describe("marshall", () => {
37
30
type TestInputType = { a : string ; b : string } ;
38
31
const input : TestInputType = { a : "A" , b : "B" } ;
39
32
40
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
41
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
42
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
33
+ expect ( marshall ( input ) ) . toEqual ( {
34
+ a : { S : "A" } ,
35
+ b : { S : "B" } ,
36
+ } ) ;
43
37
} ) ;
44
38
45
39
it ( "with Interface as an input" , ( ) => {
@@ -49,29 +43,145 @@ describe("marshall", () => {
49
43
}
50
44
const input : TestInputInterface = { a : "A" , b : "B" } ;
51
45
52
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
53
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
54
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
46
+ expect ( marshall ( input ) ) . toEqual ( {
47
+ a : { S : "A" } ,
48
+ b : { S : "B" } ,
49
+ } ) ;
55
50
} ) ;
56
51
57
52
it ( "should resolve signatures correctly" , ( ) => {
58
- // eslint-disable @typescript-eslint/no-unused-vars
59
53
const ss : AttributeValue . SSMember = marshall ( new Set ( [ "a" ] ) ) ;
54
+ expect ( ss ) . toEqual ( {
55
+ SS : [ "a" ] ,
56
+ } as AttributeValue . SSMember ) ;
60
57
const ns : AttributeValue . NSMember = marshall ( new Set ( [ 0 ] ) ) ;
61
- const bs : AttributeValue . BSMember = marshall ( new Set ( [ new Uint8Array ( ) ] ) ) ;
58
+ expect ( ns ) . toEqual ( {
59
+ NS : [ "0" ] ,
60
+ } as AttributeValue . NSMember ) ;
61
+ const bs : AttributeValue . BSMember = marshall ( new Set ( [ new Uint8Array ( 4 ) ] ) ) ;
62
+ expect ( bs ) . toEqual ( {
63
+ BS : [ new Uint8Array ( 4 ) ] ,
64
+ } as AttributeValue . BSMember ) ;
62
65
const s : AttributeValue . SMember = marshall ( "a" ) ;
63
- const n : AttributeValue . NMember = marshall ( 0 ) ;
66
+ expect ( s ) . toEqual ( {
67
+ S : "a" ,
68
+ } as AttributeValue . SMember ) ;
69
+ const n1 : AttributeValue . NMember = marshall ( 0 ) ;
70
+ expect ( n1 ) . toEqual ( { N : "0" } as AttributeValue . NMember ) ;
71
+ const n2 : AttributeValue . NMember = marshall ( BigInt ( 0 ) ) ;
72
+ expect ( n2 ) . toEqual ( { N : "0" } as AttributeValue . NMember ) ;
73
+ const n3 : AttributeValue . NMember = marshall ( NumberValue . from ( 0 ) ) ;
74
+ expect ( n3 ) . toEqual ( { N : "0" } as AttributeValue . NMember ) ;
75
+ const binary : AttributeValue . BMember = marshall ( new Uint8Array ( 4 ) ) ;
76
+ expect ( binary ) . toEqual ( {
77
+ B : new Uint8Array ( 4 ) ,
78
+ } as AttributeValue . BMember ) ;
64
79
const nil : AttributeValue . NULLMember = marshall ( null ) ;
65
- const bool : AttributeValue . BOOLMember = marshall ( false ) ;
66
- const array : AttributeValue [ ] = marshall ( [ ] ) ;
67
- const object : Record < string , AttributeValue > = marshall ( {
80
+ expect ( nil ) . toEqual ( {
81
+ NULL : true ,
82
+ } as AttributeValue . NULLMember ) ;
83
+ const bool : AttributeValue . BOOLMember = marshall ( false as boolean ) ;
84
+ expect ( bool ) . toEqual ( {
85
+ BOOL : false ,
86
+ } as AttributeValue . BOOLMember ) ;
87
+ const array : AttributeValue [ ] = marshall ( [ 1 , 2 , 3 ] ) ;
88
+ expect ( array ) . toEqual ( [ { N : "1" } , { N : "2" } , { N : "3" } ] as AttributeValue . NMember [ ] ) ;
89
+ const arrayLDefault : AttributeValue [ ] = marshall ( [ 1 , 2 , 3 ] , { } ) ;
90
+ expect ( arrayLDefault ) . toEqual ( [ { N : "1" } , { N : "2" } , { N : "3" } ] as AttributeValue . NMember [ ] ) ;
91
+ const arrayLFalse : AttributeValue [ ] = marshall ( [ 1 , 2 , 3 ] , {
92
+ convertTopLevelContainer : false ,
93
+ } ) ;
94
+ expect ( arrayLFalse ) . toEqual ( [ { N : "1" } , { N : "2" } , { N : "3" } ] as AttributeValue . NMember [ ] ) ;
95
+ const arrayLTrue : AttributeValue . LMember = marshall ( [ 1 , 2 , 3 ] , {
96
+ convertTopLevelContainer : true ,
97
+ } ) ;
98
+ expect ( arrayLTrue ) . toEqual ( {
99
+ L : [ { N : "1" } , { N : "2" } , { N : "3" } ] ,
100
+ } as AttributeValue . LMember ) ;
101
+ const arrayLBoolean : AttributeValue . LMember | AttributeValue [ ] = marshall ( [ 1 , 2 , 3 ] , {
102
+ convertTopLevelContainer : true as boolean ,
103
+ } ) ;
104
+ expect ( arrayLBoolean ) . toEqual ( {
105
+ L : [ { N : "1" } , { N : "2" } , { N : "3" } ] ,
106
+ } as AttributeValue . LMember ) ;
107
+ const object1 : Record < string , AttributeValue > = marshall ( {
68
108
pk : "abc" ,
69
109
sk : "xyz" ,
70
110
} ) ;
71
- const unrecognizedClassInstance1 : Record < string , AttributeValue > = marshall ( new Map ( ) ) ;
72
- const unrecognizedClassInstance2 : Record < string , AttributeValue > = marshall ( new Date ( ) ) ;
73
- const unrecognizedNonClassInstanceValue : AttributeValue . $UnknownMember = marshall ( BigInt ( 0 ) ) ;
74
- // eslint-enable @typescript-eslint/no-unused-vars
111
+ expect ( object1 ) . toEqual ( {
112
+ pk : { S : "abc" } ,
113
+ sk : { S : "xyz" } ,
114
+ } as Record < string , AttributeValue . SMember > ) ;
115
+ const object2 : Record < string , AttributeValue > = marshall (
116
+ {
117
+ pk : "abc" ,
118
+ sk : "xyz" ,
119
+ } ,
120
+ { }
121
+ ) ;
122
+ expect ( object2 ) . toEqual ( {
123
+ pk : { S : "abc" } ,
124
+ sk : { S : "xyz" } ,
125
+ } as Record < string , AttributeValue . SMember > ) ;
126
+ const object3 : AttributeValue . MMember = marshall (
127
+ {
128
+ pk : "abc" ,
129
+ sk : "xyz" ,
130
+ } ,
131
+ { convertTopLevelContainer : true }
132
+ ) ;
133
+ expect ( object3 ) . toEqual ( {
134
+ M : {
135
+ pk : { S : "abc" } ,
136
+ sk : { S : "xyz" } ,
137
+ } ,
138
+ } as AttributeValue . MMember ) ;
139
+ const object4 : Record < string , AttributeValue > | AttributeValue . MMember = marshall (
140
+ {
141
+ pk : "abc" ,
142
+ sk : "xyz" ,
143
+ } ,
144
+ { convertTopLevelContainer : true as boolean }
145
+ ) ;
146
+ expect ( object4 ) . toEqual ( {
147
+ M : {
148
+ pk : { S : "abc" } ,
149
+ sk : { S : "xyz" } ,
150
+ } ,
151
+ } as AttributeValue . MMember ) ;
152
+ const map : Record < string , AttributeValue > = marshall ( new Map ( [ [ "a" , "a" ] ] ) ) ;
153
+ expect ( map ) . toEqual ( {
154
+ a : { S : "a" } ,
155
+ } as Record < string , AttributeValue . SMember > ) ;
156
+ const unrecognizedClassInstance : Record < string , AttributeValue > = marshall ( new Date ( ) , {
157
+ convertClassInstanceToMap : true ,
158
+ } ) ;
159
+ expect ( unrecognizedClassInstance ) . toEqual ( { } as Record < string , AttributeValue > ) ;
160
+ const unrecognizedClassInstance2 : Record < string , AttributeValue > = marshall (
161
+ new ( class {
162
+ public a = "a" ;
163
+ public b = "b" ;
164
+ } ) ( ) ,
165
+ {
166
+ convertClassInstanceToMap : true ,
167
+ }
168
+ ) ;
169
+ expect ( unrecognizedClassInstance2 ) . toEqual ( {
170
+ a : { S : "a" } ,
171
+ b : { S : "b" } ,
172
+ } as Record < string , AttributeValue > ) ;
173
+
174
+ // this strange cast asserts that untyped fallback results in the `any` type.
175
+ const untyped : Symbol = marshall ( null as any ) as Symbol ;
176
+ expect ( untyped ) . toEqual ( {
177
+ NULL : true ,
178
+ } ) ;
179
+
180
+ const empty : Record < string , AttributeValue > = marshall ( { } as { } ) ;
181
+ expect ( empty ) . toEqual ( { } as Record < string , AttributeValue > ) ;
182
+
183
+ const empty2 : AttributeValue . MMember = marshall ( { } as { } , { convertTopLevelContainer : true } ) ;
184
+ expect ( empty2 ) . toEqual ( { M : { } } as AttributeValue . MMember ) ;
75
185
} ) ;
76
186
77
187
it ( "with class instance as an input" , ( ) => {
@@ -80,8 +190,9 @@ describe("marshall", () => {
80
190
}
81
191
const input = new TestInputClass ( "A" , "B" ) ;
82
192
83
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
84
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
85
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
193
+ expect ( marshall ( input , { convertClassInstanceToMap : true } ) ) . toEqual ( {
194
+ a : { S : "A" } ,
195
+ b : { S : "B" } ,
196
+ } ) ;
86
197
} ) ;
87
198
} ) ;
0 commit comments