1
0

util.spec.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { find, deepCopy, forEachValue, isObject, isPromise, assert } from '../../src/util'
  2. describe('util', () => {
  3. it('find', () => {
  4. const list = [33, 22, 112, 222, 43]
  5. expect(find(list, function (a) { return a % 2 === 0 })).toEqual(22)
  6. })
  7. it('deepCopy: normal structure', () => {
  8. const original = {
  9. a: 1,
  10. b: 'string',
  11. c: true,
  12. d: null,
  13. e: undefined
  14. }
  15. const copy = deepCopy(original)
  16. expect(copy).toEqual(original)
  17. })
  18. it('deepCopy: nested structure', () => {
  19. const original = {
  20. a: {
  21. b: 1,
  22. c: [2, 3, {
  23. d: 4
  24. }]
  25. }
  26. }
  27. const copy = deepCopy(original)
  28. expect(copy).toEqual(original)
  29. })
  30. it('deepCopy: circular structure', () => {
  31. const original = {
  32. a: 1
  33. }
  34. original.circular = original
  35. const copy = deepCopy(original)
  36. expect(copy).toEqual(original)
  37. })
  38. it('forEachValue', () => {
  39. let number = 1
  40. function plus (value, key) {
  41. number += value
  42. }
  43. const origin = {
  44. a: 1,
  45. b: 3
  46. }
  47. forEachValue(origin, plus)
  48. expect(number).toEqual(5)
  49. })
  50. it('isObject', () => {
  51. expect(isObject(1)).toBe(false)
  52. expect(isObject('String')).toBe(false)
  53. expect(isObject(undefined)).toBe(false)
  54. expect(isObject({})).toBe(true)
  55. expect(isObject(null)).toBe(false)
  56. expect(isObject([])).toBe(true)
  57. expect(isObject(new Function())).toBe(false)
  58. })
  59. it('isPromise', () => {
  60. const promise = new Promise(() => {}, () => {})
  61. expect(isPromise(1)).toBe(false)
  62. expect(isPromise(promise)).toBe(true)
  63. expect(isPromise(new Function())).toBe(false)
  64. })
  65. it('assert', () => {
  66. expect(assert.bind(null, false, 'Hello')).toThrowError('[vuex] Hello')
  67. })
  68. })