|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { bufferFromHexArray } from './tools/utils'; |
| 3 | +import { BSON } from '../register-bson'; |
| 4 | +import { BSON_DATA_NULL } from '../../src/constants'; |
| 5 | + |
| 6 | +describe('BSON undefined', () => { |
| 7 | + const KEY_A = '6100'; |
| 8 | + const KEY_0 = '3000'; |
| 9 | + const KEY_1 = '3100'; |
| 10 | + const KEY_2 = '3200'; |
| 11 | + |
| 12 | + describe('when deserialize is given BSON bytes with undefined value', function () { |
| 13 | + it('returns a javascript undefined value', () => { |
| 14 | + const bsonDocWithUndefined = bufferFromHexArray([ |
| 15 | + '06', // BSON undefined |
| 16 | + KEY_A |
| 17 | + ]); |
| 18 | + const doc = BSON.deserialize(bsonDocWithUndefined); |
| 19 | + expect(doc).to.have.own.property('a').that.is.undefined; |
| 20 | + }); |
| 21 | + }); |
| 22 | + |
| 23 | + describe('when serialize is given a javascript object that contains undefined', () => { |
| 24 | + describe('when ignoreUndefined is set to false', function () { |
| 25 | + it('serializes to document with a set to BSON null (type=10)', () => { |
| 26 | + const jsObject = { a: undefined }; |
| 27 | + const bytes = BSON.serialize(jsObject, { ignoreUndefined: false }); |
| 28 | + expect(bytes).to.have.lengthOf(8); |
| 29 | + const elements = BSON.onDemand.parseToElements(bytes); |
| 30 | + expect(elements).to.have.lengthOf(1); |
| 31 | + expect(elements[0][0]).to.deep.equal(BSON_DATA_NULL); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('when ignoreUndefined is set to true', function () { |
| 36 | + it('serializes to empty document', () => { |
| 37 | + const jsObject = { a: undefined }; |
| 38 | + const bytes = BSON.serialize(jsObject, { ignoreUndefined: true }); |
| 39 | + expect(bytes).to.deep.equal(Uint8Array.of(5, 0, 0, 0, 0)); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('when ignoreUndefined is unset', function () { |
| 44 | + it('serializes to empty document', () => { |
| 45 | + const jsObject = { a: undefined }; |
| 46 | + const bytes = BSON.serialize(jsObject); |
| 47 | + expect(bytes).to.deep.equal(Uint8Array.of(5, 0, 0, 0, 0)); |
| 48 | + }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe('when undefined appears inside an array', function () { |
| 53 | + describe('when ignoreUndefined is set to true', function () { |
| 54 | + it('serializes undefined values as null', function () { |
| 55 | + // because this would change the size of the array |
| 56 | + const doc = { a: [1, undefined, 3] }; |
| 57 | + const bytes = BSON.serialize(doc, { ignoreUndefined: true }); |
| 58 | + expect(bytes).to.deep.equal( |
| 59 | + bufferFromHexArray([ |
| 60 | + '04', // array |
| 61 | + KEY_A, |
| 62 | + bufferFromHexArray([ |
| 63 | + ...['10', KEY_0, '01000000'], // int "0" = 1 |
| 64 | + ...['0A', KEY_1], // null "1" |
| 65 | + ...['10', KEY_2, '03000000'] // int "2" = 3 |
| 66 | + ]).toString('hex') |
| 67 | + ]) |
| 68 | + ); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + describe('when ignoreUndefined is set to false', function () { |
| 73 | + it('serializes undefined values as null', function () { |
| 74 | + const doc = { a: [1, undefined, 3] }; |
| 75 | + const bytes = BSON.serialize(doc, { ignoreUndefined: false }); |
| 76 | + expect(bytes).to.deep.equal( |
| 77 | + bufferFromHexArray([ |
| 78 | + '04', // array |
| 79 | + KEY_A, |
| 80 | + bufferFromHexArray([ |
| 81 | + ...['10', KEY_0, '01000000'], // int "0" = 1 |
| 82 | + ...['0A', KEY_1], // null "1" |
| 83 | + ...['10', KEY_2, '03000000'] // int "2" = 3 |
| 84 | + ]).toString('hex') |
| 85 | + ]) |
| 86 | + ); |
| 87 | + }); |
| 88 | + }); |
| 89 | + |
| 90 | + describe('when ignoreUndefined is unset', function () { |
| 91 | + it('serializes undefined values as null', function () { |
| 92 | + const doc = { a: [1, undefined, 3] }; |
| 93 | + const bytes = BSON.serialize(doc); |
| 94 | + expect(bytes).to.deep.equal( |
| 95 | + bufferFromHexArray([ |
| 96 | + '04', // array |
| 97 | + KEY_A, |
| 98 | + bufferFromHexArray([ |
| 99 | + ...['10', KEY_0, '01000000'], // int "0" = 1 |
| 100 | + ...['0A', KEY_1], // null "1" |
| 101 | + ...['10', KEY_2, '03000000'] // int "2" = 3 |
| 102 | + ]).toString('hex') |
| 103 | + ]) |
| 104 | + ); |
| 105 | + }); |
| 106 | + }); |
| 107 | + }); |
| 108 | +}); |
0 commit comments