1
- import { convertToAttr } from "./convertToAttr" ;
2
- import { marshall } from "./marshall" ;
1
+ import { AttributeValue } from "@aws-sdk/client-dynamodb" ;
3
2
4
- jest . mock ( "./convertToAttr" ) ;
3
+ import { marshall } from "./marshall" ;
4
+ import { NumberValue } from "./NumberValue" ;
5
5
6
6
describe ( "marshall" , ( ) => {
7
- const mockOutput = { S : "mockOutput" } ;
8
- ( convertToAttr as jest . Mock ) . mockReturnValue ( { M : mockOutput } ) ;
9
-
10
- afterEach ( ( ) => {
11
- jest . clearAllMocks ( ) ;
12
- } ) ;
13
-
14
7
it ( "with object as an input" , ( ) => {
15
8
const input = { a : "A" , b : "B" } ;
16
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
17
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
18
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
9
+ expect ( marshall ( input ) ) . toEqual ( {
10
+ a : { S : "A" } ,
11
+ b : { S : "B" } ,
12
+ } ) ;
19
13
} ) ;
20
14
21
15
[ "convertEmptyValues" , "removeUndefinedValues" ] . forEach ( ( option ) => {
22
16
describe ( `options.${ option } ` , ( ) => {
23
17
[ false , true ] . forEach ( ( value ) => {
24
18
it ( `passes ${ value } to convertToAttr` , ( ) => {
25
19
const input = { a : "A" , b : "B" } ;
26
- expect ( marshall ( input , { [ option ] : value } ) ) . toEqual ( mockOutput ) ;
27
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
28
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , { [ option ] : value } ) ;
20
+ expect ( marshall ( input , { [ option ] : value } ) ) . toEqual ( {
21
+ a : { S : "A" } ,
22
+ b : { S : "B" } ,
23
+ } ) ;
29
24
} ) ;
30
25
} ) ;
31
26
} ) ;
@@ -35,9 +30,10 @@ describe("marshall", () => {
35
30
type TestInputType = { a : string ; b : string } ;
36
31
const input : TestInputType = { a : "A" , b : "B" } ;
37
32
38
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
39
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
40
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
33
+ expect ( marshall ( input ) ) . toEqual ( {
34
+ a : { S : "A" } ,
35
+ b : { S : "B" } ,
36
+ } ) ;
41
37
} ) ;
42
38
43
39
it ( "with Interface as an input" , ( ) => {
@@ -47,9 +43,145 @@ describe("marshall", () => {
47
43
}
48
44
const input : TestInputInterface = { a : "A" , b : "B" } ;
49
45
50
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
51
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
52
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
46
+ expect ( marshall ( input ) ) . toEqual ( {
47
+ a : { S : "A" } ,
48
+ b : { S : "B" } ,
49
+ } ) ;
50
+ } ) ;
51
+
52
+ it ( "should resolve signatures correctly" , ( ) => {
53
+ const ss : AttributeValue . SSMember = marshall ( new Set ( [ "a" ] ) ) ;
54
+ expect ( ss ) . toEqual ( {
55
+ SS : [ "a" ] ,
56
+ } as AttributeValue . SSMember ) ;
57
+ const ns : AttributeValue . NSMember = marshall ( new Set ( [ 0 ] ) ) ;
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 ) ;
65
+ const s : AttributeValue . SMember = marshall ( "a" ) ;
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 ) ;
79
+ const nil : AttributeValue . NULLMember = marshall ( null ) ;
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 ( {
108
+ pk : "abc" ,
109
+ sk : "xyz" ,
110
+ } ) ;
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 ) ;
53
185
} ) ;
54
186
55
187
it ( "with class instance as an input" , ( ) => {
@@ -58,8 +190,9 @@ describe("marshall", () => {
58
190
}
59
191
const input = new TestInputClass ( "A" , "B" ) ;
60
192
61
- expect ( marshall ( input ) ) . toEqual ( mockOutput ) ;
62
- expect ( convertToAttr ) . toHaveBeenCalledTimes ( 1 ) ;
63
- expect ( convertToAttr ) . toHaveBeenCalledWith ( input , undefined ) ;
193
+ expect ( marshall ( input , { convertClassInstanceToMap : true } ) ) . toEqual ( {
194
+ a : { S : "A" } ,
195
+ b : { S : "B" } ,
196
+ } ) ;
64
197
} ) ;
65
198
} ) ;
0 commit comments