UNPKG

@schukai/monster

Version:

Monster is a simple library for creating fast, robust and lightweight websites.

163 lines (130 loc) 3.44 kB
"use strict"; import {extend} from "../../../source/data/extend.mjs"; import {expect} from "chai" class MockForExtends { constructor() { } } describe('extend', function () { [ [ '{"thekey":{}}',{},{thekey:new MockForExtends} ], [ '{"a":{"b":[]}}', { a: { b: [ "1", "2", "3" ] } }, { a: { b: [] } }, ], [ '{"a":{"b":1,"d":1}}', { a: { b: 1 } }, { a: { d: 1 } }, ], [ '{"a":{"b":1,"d":{"x":["car"],"f":true,"g":[]}}}', {}, { a: { b: 1, d: {x: ["car"]} } }, { a: { d: { f: true, g: [] } } }, ] ].forEach(function (data) { let d = data.shift() let a = data it('.extend(' + JSON.stringify(a) + ') should result in ' + d, function () { let x = extend.apply(this, a); expect(JSON.stringify(x)).is.equal(d); }); }); [ [ {}, { a: { b: 1, d: ["car"] } }, { a: { d: { f: true, g: [] } } }, ], [ { a: {} }, { a: [] } ] ].forEach(function (data) { let a = data it('.extend(' + JSON.stringify(a) + ') should throw Error ', function () { expect(() => extend.apply(this, a)).to.throw(Error); }); }); }) describe('extend function', () => { it('should extend an object with properties from another object', () => { const target = { a: 1 }; const source = { b: 2 }; const result = extend(target, source); expect(result).to.deep.equal({ a: 1, b: 2 }); }); it('should throw an error for non-object target', () => { const target = null; const source = { b: 2 }; expect(() => extend(target, source)).to.throw(); }); it('should throw an error for non-object source', () => { const target = { a: 1 }; const source = "not an object"; expect(() => extend(target, source)).to.throw(); }); it('should handle deep object extension', () => { const target = { a: { b: 1 } }; const source = { a: { c: 2 }, d: 3 }; const result = extend(target, source); expect(result).to.deep.equal({ a: { b: 1, c: 2 }, d: 3 }); }); it('should handle array extension', () => { const target = { a: [1, 2] }; const source = { a: [3, 4] }; const result = extend(target, source); expect(result).to.deep.equal({ a: [3, 4] }); }); });