Skip to content

Commit 6161f05

Browse files
committed
test: marshalling JavaScript maps
1 parent 35bb27f commit 6161f05

File tree

1 file changed

+70
-29
lines changed

1 file changed

+70
-29
lines changed

packages/util-dynamodb/src/convertToAttr.spec.ts

+70-29
Original file line numberDiff line numberDiff line change
@@ -347,64 +347,105 @@ describe("convertToAttr", () => {
347347
({ input, output }) => {
348348
[true, false].forEach((useObjectCreate) => {
349349
const inputObject = useObjectCreate ? Object.create(input) : input;
350-
it(`testing object: ${inputObject}`, () => {
350+
it(`testing object: ${inputObject}${useObjectCreate && " with Object.create()"}`, () => {
351351
expect(convertToAttr(inputObject)).toEqual({ M: output });
352352
});
353353
});
354+
355+
const inputMap = new Map(Object.entries(input));
356+
it(`testing map: ${inputMap}`, () => {
357+
expect(convertToAttr(inputMap)).toEqual({ M: output });
358+
});
354359
}
355360
);
356361

357-
[true, false].forEach((useObjectCreate) => {
358-
it(`testing object with options.convertEmptyValues=true`, () => {
359-
const input = { stringKey: "", binaryKey: new Uint8Array(), setKey: new Set([]) };
362+
describe(`with options.convertEmptyValues=true`, () => {
363+
const input = { stringKey: "", binaryKey: new Uint8Array(), setKey: new Set([]) };
364+
const output = { stringKey: { NULL: true }, binaryKey: { NULL: true }, setKey: { NULL: true } };
365+
366+
[true, false].forEach((useObjectCreate) => {
360367
const inputObject = useObjectCreate ? Object.create(input) : input;
361-
expect(convertToAttr(inputObject, { convertEmptyValues: true })).toEqual({
362-
M: { stringKey: { NULL: true }, binaryKey: { NULL: true }, setKey: { NULL: true } },
368+
it(`testing object${useObjectCreate && " with Object.create()"}`, () => {
369+
expect(convertToAttr(inputObject, { convertEmptyValues: true })).toEqual({ M: output });
363370
});
364371
});
372+
373+
const inputMap = new Map(Object.entries(input));
374+
it(`testing map`, () => {
375+
expect(convertToAttr(inputMap, { convertEmptyValues: true })).toEqual({ M: output });
376+
});
365377
});
366378

367-
[true, false].forEach((useObjectCreate) => {
368-
describe(`testing object with options.removeUndefinedValues`, () => {
369-
describe("throws error", () => {
370-
const testErrorMapWithUndefinedValues = (useObjectCreate: boolean, options?: marshallOptions) => {
371-
const input = { definedKey: "definedKey", undefinedKey: undefined };
372-
const inputObject = useObjectCreate ? Object.create(input) : input;
373-
expect(() => {
374-
convertToAttr(inputObject, options);
375-
}).toThrowError(`Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.`);
376-
};
379+
describe(`with options.removeUndefinedValues=true`, () => {
380+
describe("throws error", () => {
381+
const testErrorMapWithUndefinedValues = (input: any, options?: marshallOptions) => {
382+
expect(() => {
383+
convertToAttr(input, options);
384+
}).toThrowError(`Pass options.removeUndefinedValues=true to remove undefined values from map/array/set.`);
385+
};
377386

378-
[undefined, {}, { convertEmptyValues: false }].forEach((options) => {
379-
it(`when options=${options}`, () => {
380-
testErrorMapWithUndefinedValues(useObjectCreate, options);
387+
[undefined, {}, { convertEmptyValues: false }].forEach((options) => {
388+
const input = { definedKey: "definedKey", undefinedKey: undefined };
389+
[true, false].forEach((useObjectCreate) => {
390+
const inputObject = useObjectCreate ? Object.create(input) : input;
391+
it(`testing object${useObjectCreate && " with Object.create()"} when options=${options}`, () => {
392+
testErrorMapWithUndefinedValues(inputObject, options);
381393
});
382394
});
395+
396+
const inputMap = new Map(Object.entries(input));
397+
it(`testing map when options=${options}`, () => {
398+
testErrorMapWithUndefinedValues(inputMap, options);
399+
});
383400
});
401+
});
384402

385-
it(`returns when options.removeUndefinedValues=true`, () => {
386-
const input = { definedKey: "definedKey", undefinedKey: undefined };
403+
describe(`returns when options.removeUndefinedValues=true`, () => {
404+
const input = { definedKey: "definedKey", undefinedKey: undefined };
405+
const output = { definedKey: { S: "definedKey" } };
406+
[true, false].forEach((useObjectCreate) => {
387407
const inputObject = useObjectCreate ? Object.create(input) : input;
388-
expect(convertToAttr(inputObject, { removeUndefinedValues: true })).toEqual({
389-
M: { definedKey: { S: "definedKey" } },
408+
it(`testing object${useObjectCreate && " with Object.create()"}`, () => {
409+
expect(convertToAttr(inputObject, { removeUndefinedValues: true })).toEqual({ M: output });
390410
});
391411
});
412+
413+
const inputMap = new Map(Object.entries(input));
414+
it(`testing map`, () => {
415+
expect(convertToAttr(inputMap, { removeUndefinedValues: true })).toEqual({ M: output });
416+
});
392417
});
393418
});
394419

395-
it(`testing Object.create with function`, () => {
396-
const person = {
397-
isHuman: true,
398-
printIntroduction: function () {
399-
console.log(`Am I human? ${this.isHuman}`);
420+
describe(`testing with function`, () => {
421+
const input = {
422+
bool: true,
423+
func: function () {
424+
console.log(`bool: ${this.bool}`);
400425
},
401426
};
402-
expect(convertToAttr(Object.create(person))).toEqual({ M: { isHuman: { BOOL: true } } });
427+
const output = { bool: { BOOL: true } };
428+
429+
[true, false].forEach((useObjectCreate) => {
430+
const inputObject = useObjectCreate ? Object.create(input) : input;
431+
it(`testing object${useObjectCreate && " with Object.create()"}`, () => {
432+
expect(convertToAttr(inputObject)).toEqual({ M: output });
433+
});
434+
});
435+
436+
const inputMap = new Map(Object.entries(input));
437+
it(`testing map`, () => {
438+
expect(convertToAttr(inputMap)).toEqual({ M: output });
439+
});
403440
});
404441

405442
it(`testing Object.create(null)`, () => {
406443
expect(convertToAttr(Object.create(null))).toEqual({ M: {} });
407444
});
445+
446+
it(`testing empty Map`, () => {
447+
expect(convertToAttr(new Map())).toEqual({ M: {} });
448+
});
408449
});
409450

410451
describe("string", () => {

0 commit comments

Comments
 (0)