util.spec.js 1.9 KB

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