Skip to content

Commit

Permalink
test: 添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
yanhaijing committed Oct 10, 2018
1 parent ea76938 commit 2a9cbe5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export function extend(deep, target, ...sourceList) {
}

// 深拷贝
if (!isObject(target)) {
if (!isObject(target) && !isArray(target)) {
throw new TypeError('extend target param must is object');
}

Expand Down
2 changes: 1 addition & 1 deletion test/browser/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<script>
var libs = {
'expect.js': expect,
'../dist/index.js': jslib_extend
'../dist/index.js': jsmini_extend
};
var require = function(path) {
return libs[path];
Expand Down
46 changes: 39 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,51 @@
var expect = require('expect.js');

var base = require('../dist/index.js');
var assign = require('../dist/index.js').assign;
var extend = require('../dist/index.js').extend;

describe('单元测试', function() {
this.timeout(1000);

describe('功能1', function() {
it('相等', function() {
expect(base.name).to.equal('base');
var a = {a: 1};
var b = {a: 2, b1: 1, b2: {c1: 1}, b3: [1]};

describe('assign', function() {
it('常规', function() {
var h = assign({}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
expect(h.b2).to.equal(b.b2);
});
});

describe('extend:shallow', function() {
it('常规', function() {
// 缺省参数
var h = extend({}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
expect(h.b2).to.equal(b.b2);

// 显示参数
var h = extend(false, {}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);
expect(h.b2).to.equal(b.b2);
});
});

describe('功能2', function() {
it('不相等', function() {
expect(base.name).not.to.equal(1);
describe('extend:deep', function() {
it('常规', function() {
var h = extend(true, {}, a, b);

expect(h.a).to.equal(b.a);
expect(h.b1).to.equal(b.b1);

expect(h.b2).not.to.equal(b.b2);
expect(h.b2).to.eql(b.b2);
});
});
});

0 comments on commit 2a9cbe5

Please sign in to comment.