|
| 1 | +// @flow strict |
| 2 | + |
| 3 | +import { expect } from 'chai'; |
| 4 | +import { describe, it } from 'mocha'; |
| 5 | + |
| 6 | +import toObjMap from '../toObjMap'; |
| 7 | +import { type ObjMapLike } from '../ObjMap'; |
| 8 | + |
| 9 | +// Workaround to make both ESLint and Flow happy |
| 10 | +const __proto__: string = '__proto__'; |
| 11 | + |
| 12 | +describe('toObjMap', () => { |
| 13 | + it('convert empty object to ObjMap', () => { |
| 14 | + const result = toObjMap({}); |
| 15 | + expect(result).to.deep.equal({}); |
| 16 | + expect(Object.getPrototypeOf(result)).to.equal(null); |
| 17 | + }); |
| 18 | + |
| 19 | + it('convert object with own properties to ObjMap', () => { |
| 20 | + const obj: ObjMapLike<string> = Object.freeze({ foo: 'bar' }); |
| 21 | + |
| 22 | + const result = toObjMap(obj); |
| 23 | + expect(result).to.deep.equal(obj); |
| 24 | + expect(Object.getPrototypeOf(result)).to.equal(null); |
| 25 | + }); |
| 26 | + |
| 27 | + it('convert object with __proto__ property to ObjMap', () => { |
| 28 | + const protoObj = Object.freeze({ toString: false }); |
| 29 | + const obj = Object.create(null); |
| 30 | + obj[__proto__] = protoObj; |
| 31 | + Object.freeze(obj); |
| 32 | + |
| 33 | + const result = toObjMap(obj); |
| 34 | + expect(Object.keys(result)).to.deep.equal(['__proto__']); |
| 35 | + expect(Object.getPrototypeOf(result)).to.equal(null); |
| 36 | + expect(result[__proto__]).to.equal(protoObj); |
| 37 | + }); |
| 38 | + |
| 39 | + it('passthrough empty ObjMap', () => { |
| 40 | + const objMap = Object.create(null); |
| 41 | + expect(toObjMap(objMap)).to.deep.equal(objMap); |
| 42 | + }); |
| 43 | + |
| 44 | + it('passthrough ObjMap with properties', () => { |
| 45 | + const objMap = Object.freeze({ |
| 46 | + __proto__: null, |
| 47 | + foo: 'bar', |
| 48 | + }); |
| 49 | + expect(toObjMap(objMap)).to.deep.equal(objMap); |
| 50 | + }); |
| 51 | +}); |
0 commit comments