module.spec.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. import Module from '@/module/module'
  2. describe('Module', () => {
  3. it('get state', () => {
  4. const module = new Module({
  5. state: {
  6. value: true
  7. }
  8. })
  9. expect(module.state).toEqual({ value: true })
  10. })
  11. it('get state: should return object if state option is empty', () => {
  12. const module = new Module({})
  13. expect(module.state).toEqual({})
  14. })
  15. it('get namespacer: no namespace option', () => {
  16. const module = new Module({})
  17. expect(module.namespaced).toBe(false)
  18. })
  19. it('get namespacer: namespace option is true', () => {
  20. let module = new Module({
  21. namespaced: true
  22. })
  23. expect(module.namespaced).toBe(true)
  24. module = new Module({
  25. namespaced: 100
  26. })
  27. expect(module.namespaced).toBe(true)
  28. })
  29. it('add child method', () => {
  30. const module = new Module({})
  31. module.addChild('v1', new Module({}))
  32. module.addChild('v2', new Module({}))
  33. expect(Object.keys(module._children)).toEqual(['v1', 'v2'])
  34. })
  35. it('remove child method', () => {
  36. const module = new Module({})
  37. module.addChild('v1', new Module({}))
  38. module.addChild('v2', new Module({}))
  39. expect(Object.keys(module._children)).toEqual(['v1', 'v2'])
  40. module.removeChild('v2')
  41. module.removeChild('abc')
  42. expect(Object.keys(module._children)).toEqual(['v1'])
  43. })
  44. it('get child method', () => {
  45. const module = new Module({})
  46. const subModule1 = new Module({ state: { name: 'v1' }})
  47. const subModule2 = new Module({ state: { name: 'v2' }})
  48. module.addChild('v1', subModule1)
  49. module.addChild('v2', subModule2)
  50. expect(module.getChild('v2')).toEqual(subModule2)
  51. expect(module.getChild('v1')).toEqual(subModule1)
  52. })
  53. it('update method', () => {
  54. const originObject = {
  55. state: {
  56. name: 'vuex',
  57. version: '2.x.x'
  58. },
  59. namespaced: true,
  60. actions: {
  61. a1: () => {},
  62. a2: () => {}
  63. },
  64. mutations: {
  65. m1: () => {},
  66. m2: () => {}
  67. },
  68. getters: {
  69. g1: () => {},
  70. g2: () => {}
  71. }
  72. }
  73. const newObject = {
  74. actions: {
  75. a3: () => {},
  76. a4: () => {}
  77. },
  78. mutations: {
  79. m3: () => {},
  80. m2: () => {}
  81. },
  82. getters: {
  83. g1: () => {}
  84. },
  85. namespaced: false,
  86. state: {
  87. name: 'vuex',
  88. version: '3.x.x'
  89. }
  90. }
  91. const module = new Module(originObject)
  92. expect(module._rawModule).toEqual(originObject)
  93. module.update(newObject)
  94. expect(module._rawModule.actions).toEqual(newObject.actions)
  95. expect(module._rawModule.mutations).toEqual(newObject.mutations)
  96. expect(module._rawModule.getters).toEqual(newObject.getters)
  97. expect(module._rawModule.namespaced).toEqual(newObject.namespaced)
  98. expect(module._rawModule.state).toEqual(originObject.state)
  99. })
  100. it('forEachChild method', () => {
  101. const module = new Module({})
  102. const module1 = new Module({})
  103. const module2 = new Module({})
  104. module.addChild('v1', module1)
  105. module.addChild('v2', module2)
  106. const collections = []
  107. module.forEachChild((item) => { collections.push(item) })
  108. expect(collections.length).toEqual(2)
  109. expect(collections).toEqual([module2, module1])
  110. })
  111. it('forEachAction method', () => {
  112. const action1 = () => {}
  113. const action2 = () => {}
  114. const module = new Module({
  115. actions: {
  116. action1, action2
  117. }
  118. })
  119. const collections = []
  120. module.forEachAction((item) => { collections.push(item) })
  121. expect(collections.length).toEqual(2)
  122. expect(collections).toEqual([action1, action2])
  123. })
  124. it('forEachGetter method', () => {
  125. const getter1 = () => {}
  126. const getter2 = () => {}
  127. const module = new Module({
  128. getters: {
  129. getter1, getter2
  130. }
  131. })
  132. const collections = []
  133. module.forEachGetter((item) => { collections.push(item) })
  134. expect(collections.length).toEqual(2)
  135. expect(collections).toEqual([getter1, getter2])
  136. })
  137. it('forEachMutation method', () => {
  138. const mutation1 = () => {}
  139. const mutation2 = () => {}
  140. const module = new Module({
  141. mutations: {
  142. mutation1, mutation2
  143. }
  144. })
  145. const collections = []
  146. module.forEachMutation((item) => { collections.push(item) })
  147. expect(collections.length).toEqual(2)
  148. expect(collections).toEqual([mutation1, mutation2])
  149. })
  150. })