From 53a0b37cf928a53e9509e215627b7fb1eaf5ff9a Mon Sep 17 00:00:00 2001 From: simonguo Date: Wed, 10 Apr 2024 16:32:35 +0800 Subject: [PATCH] test: fix error tests --- test/utilsSpec.js | 69 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 4 deletions(-) diff --git a/test/utilsSpec.js b/test/utilsSpec.js index becba1d..e28226f 100644 --- a/test/utilsSpec.js +++ b/test/utilsSpec.js @@ -1,7 +1,5 @@ -/* eslint-disable @typescript-eslint/no-var-requires */ -require('chai').should(); -const { expect } = require('chai'); -const { formatErrorMessage, checkRequired, get } = require('../src/utils'); +import { expect } from 'chai'; +import { formatErrorMessage, checkRequired, get, set, shallowEqual } from '../src/utils'; describe('#utils', () => { describe('## formatErrorMessage', () => { @@ -95,4 +93,67 @@ describe('#utils', () => { expect(get(undefined, 'a.b', 10)).to.equal(10); }); }); + + describe('## set', () => { + it('Should set the value of the object', () => { + const obj = { a: { b: { c: 1 } } }; + set(obj, 'a.b.c', 2); + obj.a.b.c.should.equal(2); + set(obj, 'a.b', { c: 3 }); + obj.a.b.should.deep.equal({ c: 3 }); + set(obj, 'a', { b: { c: 4 } }); + obj.a.should.deep.equal({ b: { c: 4 } }); + }); + + it('Should set the value of the array', () => { + const obj = { a: [{ b: 1 }, { b: 2 }] }; + set(obj, 'a.0.b', 3); + obj.a[0].b.should.equal(3); + set(obj, 'a.1.b', 4); + obj.a[1].b.should.equal(4); + }); + + it('Should set the value of the array and object', () => { + const obj = { a: [{ b: { c: 1 } }, { b: { c: 2 } }] }; + set(obj, 'a.0.b.c', 3); + obj.a[0].b.c.should.equal(3); + set(obj, 'a.1.b.c', 4); + obj.a[1].b.c.should.equal(4); + }); + }); + + describe('## shallowEqual', () => { + it('Should compare the object', () => { + const obj1 = { a: 1, b: 2 }; + const obj2 = { a: 1, b: 2 }; + const obj3 = { a: 1, b: 3 }; + const obj4 = { a: 1, b: 2, c: 3 }; + + shallowEqual(obj1, obj2).should.equal(true); + shallowEqual(obj1, obj3).should.equal(false); + shallowEqual(obj1, obj4).should.equal(false); + }); + + it('Should compare the array', () => { + const arr1 = [1, 2]; + const arr2 = [1, 2]; + const arr3 = [1, 3]; + const arr4 = [1, 2, 3]; + + shallowEqual(arr1, arr2).should.equal(true); + shallowEqual(arr1, arr3).should.equal(false); + shallowEqual(arr1, arr4).should.equal(false); + }); + + it('Should compare the object and array', () => { + const obj = { a: 1, b: [1, 2] }; + const obj1 = { a: 1, b: [1, 2] }; + const obj2 = { a: 1, b: [1, 3] }; + const obj3 = { a: 1, b: [1, 2, 3] }; + + shallowEqual(obj, obj1).should.equal(false); + shallowEqual(obj, obj2).should.equal(false); + shallowEqual(obj, obj3).should.equal(false); + }); + }); });