|
| 1 | +import { typesHandler } from "../utils"; |
| 2 | + |
| 3 | +describe('typesHandler', () => { |
| 4 | + it('should return the original value if it is not a Map', () => { |
| 5 | + const value = { a: 1 }; |
| 6 | + expect(typesHandler(undefined, value)).toEqual(value); |
| 7 | + }); |
| 8 | + |
| 9 | + it('should return an empty object if the Map keys are not of type string', () => { |
| 10 | + const value = new Map([[1, 'one'], [2, 'two']]); |
| 11 | + expect(typesHandler(undefined, value)).toEqual({}); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should convert empty Map into empty object', () => { |
| 15 | + const value = new Map(); |
| 16 | + const expected = {}; |
| 17 | + expect(typesHandler(undefined, value)).toEqual(expected); |
| 18 | + }); |
| 19 | + |
| 20 | + it('should convert Map object with string keys into object with key-value pairs', () => { |
| 21 | + const value = new Map([['a', 1], ['b', 2]]); |
| 22 | + const expected = { a: 1, b: 2 }; |
| 23 | + expect(typesHandler(undefined, value)).toEqual(expected); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should correctly stringify a Map object with string keys', () => { |
| 27 | + const myObject = { |
| 28 | + myMap: new Map([['a', 1], ['b', 2]]) |
| 29 | + }; |
| 30 | + const expected = '{\n "myMap": {\n "a": 1,\n "b": 2\n }\n}'; |
| 31 | + |
| 32 | + const stringified = JSON.stringify(myObject, typesHandler, 2); |
| 33 | + expect(stringified).toEqual(expected); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should correctly stringify a Map object with non-string values', () => { |
| 37 | + const myObject = { |
| 38 | + myMap: new Map([['a', { foo: 'bar' }]]) |
| 39 | + }; |
| 40 | + const expected = '{\n "myMap": {\n "a": {\n "foo": "bar"\n }\n }\n}'; |
| 41 | + |
| 42 | + const stringified = JSON.stringify(myObject, typesHandler, 2); |
| 43 | + |
| 44 | + expect(stringified).toEqual(expected); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should correctly stringify a non-Map object', () => { |
| 48 | + const myObject = { |
| 49 | + foo: 'bar', |
| 50 | + baz: 42 |
| 51 | + }; |
| 52 | + const expected = '{\n "foo": "bar",\n "baz": 42\n}'; |
| 53 | + |
| 54 | + const stringified = JSON.stringify(myObject, typesHandler, 2); |
| 55 | + expect(stringified).toEqual(expected); |
| 56 | + }); |
| 57 | +}); |
0 commit comments