From 1ae13ae4178f5dcb1fd9c8cf2fdce631aa99de21 Mon Sep 17 00:00:00 2001 From: lanzhiheng Date: Thu, 21 Dec 2017 18:03:20 +0800 Subject: [PATCH] Add Simple test case for base function utils --- src/util.js | 2 +- test/unit/util.spec.js | 43 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index 8642b82f8..fb1203251 100644 --- a/src/util.js +++ b/src/util.js @@ -6,7 +6,7 @@ * @param {Function} f * @return {*} */ -function find (list, f) { +export function find (list, f) { return list.filter(f)[0] } diff --git a/test/unit/util.spec.js b/test/unit/util.spec.js index 8f17cbc60..bcbfb0c9f 100644 --- a/test/unit/util.spec.js +++ b/test/unit/util.spec.js @@ -1,6 +1,11 @@ -import { deepCopy } from '../../src/util' +import { find, deepCopy, forEachValue, isObject, isPromise, assert } from '../../src/util' describe('util', () => { + it('find', () => { + const list = [33, 22, 112, 222, 43] + expect(find(list, function (a) { return a % 2 === 0 })).toEqual(22) + }) + it('deepCopy: nornal structure', () => { const original = { a: 1, @@ -38,4 +43,40 @@ describe('util', () => { expect(copy).toEqual(original) }) + + it('forEachValue', () => { + let number = 1 + + function plus (value, key) { + number += value + } + const origin = { + a: 1, + b: 3 + } + + forEachValue(origin, plus) + expect(number).toEqual(5) + }) + + it('isObject', () => { + expect(isObject(1)).toBe(false) + expect(isObject('String')).toBe(false) + expect(isObject(undefined)).toBe(false) + expect(isObject({})).toBe(true) + expect(isObject(null)).toBe(false) + expect(isObject([])).toBe(true) + expect(isObject(new Function())).toBe(false) + }) + + it('isPromise', () => { + const promise = new Promise(() => {}, () => {}) + expect(isPromise(1)).toBe(false) + expect(isPromise(promise)).toBe(true) + expect(isPromise(new Function())).toBe(false) + }) + + it('assert', () => { + expect(assert.bind(null, false, 'Hello')).toThrowError('[vuex] Hello') + }) })