|
1 | | -import { isArray, isEqual, isObject, isUndefined } from './index.js'; |
| 1 | +import { isArray, isEqual, isObject, isPlainObject, isUndefined } from './index.js'; |
2 | 2 |
|
3 | 3 | describe('helpers', () => { |
4 | 4 | describe('is', () => { |
@@ -60,5 +60,54 @@ describe('helpers', () => { |
60 | 60 | expect(isUndefined('')).toBe(false); |
61 | 61 | }); |
62 | 62 | }); |
| 63 | + |
| 64 | + describe('isPlainObject', () => { |
| 65 | + it('should return true for plain objects', () => { |
| 66 | + expect(isPlainObject({})).toBe(true); |
| 67 | + expect(isPlainObject({ foo: 'bar' })).toBe(true); |
| 68 | + expect(isPlainObject(Object.create(null))).toBe(true); |
| 69 | + expect(isPlainObject(Object.create(Object.prototype))).toBe(true); |
| 70 | + expect(isPlainObject(Object.assign({}, { a: 1 }))).toBe(true); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should return false for primitives', () => { |
| 74 | + expect(isPlainObject(true)).toBe(false); |
| 75 | + expect(isPlainObject(undefined)).toBe(false); |
| 76 | + expect(isPlainObject(1)).toBe(false); |
| 77 | + expect(isPlainObject('string')).toBe(false); |
| 78 | + expect(isPlainObject(Symbol('s'))).toBe(false); |
| 79 | + }); |
| 80 | + |
| 81 | + it('should return false for `null`', () => { |
| 82 | + expect(isPlainObject(null)).toBe(false); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should return false for functions and instances', () => { |
| 86 | + function Foo() { |
| 87 | + this.abc = {}; |
| 88 | + } |
| 89 | + |
| 90 | + expect(isPlainObject(Foo)).toBe(false); |
| 91 | + expect(isPlainObject(new Foo())).toBe(false); |
| 92 | + expect(isPlainObject(() => {})).toBe(false); |
| 93 | + expect(isPlainObject(function () {})).toBe(false); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should return false for arrays', () => { |
| 97 | + expect(isPlainObject([])).toBe(false); |
| 98 | + expect(isPlainObject([1, 2, 3])).toBe(false); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should return false for built-in objects', () => { |
| 102 | + expect(isPlainObject(new Date())).toBe(false); |
| 103 | + expect(isPlainObject(new Map())).toBe(false); |
| 104 | + expect(isPlainObject(new Set())).toBe(false); |
| 105 | + expect(isPlainObject(/abc/)).toBe(false); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should return false for objects created with Object.create and a custom prototype', () => { |
| 109 | + expect(isPlainObject(Object.create({}))).toBe(false); |
| 110 | + }); |
| 111 | + }); |
63 | 112 | }); |
64 | 113 | }); |
0 commit comments