Prechádzať zdrojové kódy

Add Simple test case for base function utils (#1099)

Lan 7 rokov pred
rodič
commit
479bd3e3ab
2 zmenil súbory, kde vykonal 43 pridanie a 2 odobranie
  1. 1 1
      src/util.js
  2. 42 1
      test/unit/util.spec.js

+ 1 - 1
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]
 }
 

+ 42 - 1
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')
+  })
 })