Skip to content

Commit

Permalink
test: fix error tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonguo committed Apr 10, 2024
1 parent 5d3e504 commit 53a0b37
Showing 1 changed file with 65 additions and 4 deletions.
69 changes: 65 additions & 4 deletions test/utilsSpec.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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);
});
});
});

0 comments on commit 53a0b37

Please sign in to comment.