Skip to content

Commit 479bd3e

Browse files
lanzhihengktsn
authored andcommitted
Add Simple test case for base function utils (#1099)
1 parent b50f81c commit 479bd3e

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/util.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* @param {Function} f
77
* @return {*}
88
*/
9-
function find (list, f) {
9+
export function find (list, f) {
1010
return list.filter(f)[0]
1111
}
1212

test/unit/util.spec.js

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import { deepCopy } from '../../src/util'
1+
import { find, deepCopy, forEachValue, isObject, isPromise, assert } from '../../src/util'
22

33
describe('util', () => {
4+
it('find', () => {
5+
const list = [33, 22, 112, 222, 43]
6+
expect(find(list, function (a) { return a % 2 === 0 })).toEqual(22)
7+
})
8+
49
it('deepCopy: nornal structure', () => {
510
const original = {
611
a: 1,
@@ -38,4 +43,40 @@ describe('util', () => {
3843

3944
expect(copy).toEqual(original)
4045
})
46+
47+
it('forEachValue', () => {
48+
let number = 1
49+
50+
function plus (value, key) {
51+
number += value
52+
}
53+
const origin = {
54+
a: 1,
55+
b: 3
56+
}
57+
58+
forEachValue(origin, plus)
59+
expect(number).toEqual(5)
60+
})
61+
62+
it('isObject', () => {
63+
expect(isObject(1)).toBe(false)
64+
expect(isObject('String')).toBe(false)
65+
expect(isObject(undefined)).toBe(false)
66+
expect(isObject({})).toBe(true)
67+
expect(isObject(null)).toBe(false)
68+
expect(isObject([])).toBe(true)
69+
expect(isObject(new Function())).toBe(false)
70+
})
71+
72+
it('isPromise', () => {
73+
const promise = new Promise(() => {}, () => {})
74+
expect(isPromise(1)).toBe(false)
75+
expect(isPromise(promise)).toBe(true)
76+
expect(isPromise(new Function())).toBe(false)
77+
})
78+
79+
it('assert', () => {
80+
expect(assert.bind(null, false, 'Hello')).toThrowError('[vuex] Hello')
81+
})
4182
})

0 commit comments

Comments
 (0)