hot-reload.spec.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. import Vue from 'vue/dist/vue.common.js'
  2. import Vuex from '../../dist/vuex.common.js'
  3. const TEST = 'TEST'
  4. describe('Hot Reload', () => {
  5. it('mutations', function () {
  6. const mutations = {
  7. [TEST] (state, n) {
  8. state.a += n
  9. }
  10. }
  11. const store = new Vuex.Store({
  12. state: {
  13. a: 1
  14. },
  15. mutations,
  16. modules: {
  17. nested: {
  18. state: { a: 2 },
  19. mutations,
  20. modules: {
  21. one: {
  22. state: { a: 3 },
  23. mutations
  24. },
  25. nested: {
  26. modules: {
  27. two: {
  28. state: { a: 4 },
  29. mutations
  30. },
  31. three: {
  32. state: { a: 5 },
  33. mutations
  34. }
  35. }
  36. }
  37. }
  38. },
  39. four: {
  40. state: { a: 6 },
  41. mutations
  42. }
  43. }
  44. })
  45. store.commit(TEST, 1)
  46. expect(store.state.a).toBe(2)
  47. expect(store.state.nested.a).toBe(3)
  48. expect(store.state.nested.one.a).toBe(4)
  49. expect(store.state.nested.nested.two.a).toBe(5)
  50. expect(store.state.nested.nested.three.a).toBe(6)
  51. expect(store.state.four.a).toBe(7)
  52. // hot reload only root mutations
  53. store.hotUpdate({
  54. mutations: {
  55. [TEST] (state, n) {
  56. state.a = n
  57. }
  58. }
  59. })
  60. store.commit(TEST, 1)
  61. expect(store.state.a).toBe(1) // only root mutation updated
  62. expect(store.state.nested.a).toBe(4)
  63. expect(store.state.nested.one.a).toBe(5)
  64. expect(store.state.nested.nested.two.a).toBe(6)
  65. expect(store.state.nested.nested.three.a).toBe(7)
  66. expect(store.state.four.a).toBe(8)
  67. // hot reload modules
  68. store.hotUpdate({
  69. modules: {
  70. nested: {
  71. state: { a: 234 },
  72. mutations,
  73. modules: {
  74. one: {
  75. state: { a: 345 },
  76. mutations
  77. },
  78. nested: {
  79. modules: {
  80. two: {
  81. state: { a: 456 },
  82. mutations
  83. },
  84. three: {
  85. state: { a: 567 },
  86. mutations
  87. }
  88. }
  89. }
  90. }
  91. },
  92. four: {
  93. state: { a: 678 },
  94. mutations
  95. }
  96. }
  97. })
  98. store.commit(TEST, 2)
  99. expect(store.state.a).toBe(2)
  100. expect(store.state.nested.a).toBe(6) // should not reload initial state
  101. expect(store.state.nested.one.a).toBe(7) // should not reload initial state
  102. expect(store.state.nested.nested.two.a).toBe(8) // should not reload initial state
  103. expect(store.state.nested.nested.three.a).toBe(9) // should not reload initial state
  104. expect(store.state.four.a).toBe(10) // should not reload initial state
  105. // hot reload all
  106. store.hotUpdate({
  107. mutations: {
  108. [TEST] (state, n) {
  109. state.a -= n
  110. }
  111. },
  112. modules: {
  113. nested: {
  114. state: { a: 234 },
  115. mutations: {
  116. [TEST] (state, n) {
  117. state.a += n
  118. }
  119. },
  120. modules: {
  121. one: {
  122. state: { a: 345 },
  123. mutations: {
  124. [TEST] (state, n) {
  125. state.a += n
  126. }
  127. }
  128. },
  129. nested: {
  130. modules: {
  131. two: {
  132. state: { a: 456 },
  133. mutations: {
  134. [TEST] (state, n) {
  135. state.a += n
  136. }
  137. }
  138. },
  139. three: {
  140. state: { a: 567 },
  141. mutations: {
  142. [TEST] (state, n) {
  143. state.a -= n
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. },
  151. four: {
  152. state: { a: 678 },
  153. mutations: {
  154. [TEST] (state, n) {
  155. state.a -= n
  156. }
  157. }
  158. }
  159. }
  160. })
  161. store.commit(TEST, 3)
  162. expect(store.state.a).toBe(-1)
  163. expect(store.state.nested.a).toBe(9)
  164. expect(store.state.nested.one.a).toBe(10)
  165. expect(store.state.nested.nested.two.a).toBe(11)
  166. expect(store.state.nested.nested.three.a).toBe(6)
  167. expect(store.state.four.a).toBe(7)
  168. })
  169. it('actions', () => {
  170. const store = new Vuex.Store({
  171. state: {
  172. list: []
  173. },
  174. mutations: {
  175. [TEST] (state, n) {
  176. state.list.push(n)
  177. }
  178. },
  179. actions: {
  180. [TEST] ({ commit }) {
  181. commit(TEST, 1)
  182. }
  183. },
  184. modules: {
  185. a: {
  186. actions: {
  187. [TEST] ({ commit }) {
  188. commit(TEST, 2)
  189. }
  190. }
  191. }
  192. }
  193. })
  194. store.dispatch(TEST)
  195. expect(store.state.list.join()).toBe('1,2')
  196. // update root
  197. store.hotUpdate({
  198. actions: {
  199. [TEST] ({ commit }) {
  200. commit(TEST, 3)
  201. }
  202. }
  203. })
  204. store.dispatch(TEST)
  205. expect(store.state.list.join()).toBe('1,2,3,2')
  206. // update modules
  207. store.hotUpdate({
  208. actions: {
  209. [TEST] ({ commit }) {
  210. commit(TEST, 4)
  211. }
  212. },
  213. modules: {
  214. a: {
  215. actions: {
  216. [TEST] ({ commit }) {
  217. commit(TEST, 5)
  218. }
  219. }
  220. }
  221. }
  222. })
  223. store.dispatch(TEST)
  224. expect(store.state.list.join()).toBe('1,2,3,2,4,5')
  225. })
  226. it('getters', done => {
  227. const store = new Vuex.Store({
  228. state: {
  229. count: 0
  230. },
  231. mutations: {
  232. inc: state => state.count++
  233. },
  234. getters: {
  235. count: state => state.count
  236. },
  237. actions: {
  238. check ({ getters }, value) {
  239. expect(getters.count).toBe(value)
  240. }
  241. }
  242. })
  243. const spy = jasmine.createSpy()
  244. const vm = new Vue({
  245. computed: {
  246. a: () => store.getters.count
  247. },
  248. watch: {
  249. a: spy
  250. }
  251. })
  252. expect(vm.a).toBe(0)
  253. store.dispatch('check', 0)
  254. store.commit('inc')
  255. expect(vm.a).toBe(1)
  256. store.dispatch('check', 1)
  257. // update getters
  258. store.hotUpdate({
  259. getters: {
  260. count: state => state.count * 10
  261. }
  262. })
  263. expect(vm.a).toBe(10)
  264. store.dispatch('check', 10)
  265. Vue.nextTick(() => {
  266. expect(spy).toHaveBeenCalled()
  267. done()
  268. })
  269. })
  270. it('provide warning if a new module is given', () => {
  271. const store = new Vuex.Store({})
  272. spyOn(console, 'warn')
  273. store.hotUpdate({
  274. modules: {
  275. test: {
  276. state: {
  277. count: 0
  278. }
  279. }
  280. }
  281. })
  282. expect(console.warn).toHaveBeenCalledWith(
  283. '[vuex] trying to add a new module \'test\' on hot reloading, ' +
  284. 'manual reload is needed'
  285. )
  286. })
  287. it('update namespace', () => {
  288. // prevent to print notification of unknown action/mutation
  289. spyOn(console, 'error')
  290. const actionSpy = jasmine.createSpy()
  291. const mutationSpy = jasmine.createSpy()
  292. const store = new Vuex.Store({
  293. modules: {
  294. a: {
  295. namespaced: true,
  296. state: { value: 1 },
  297. getters: { foo: state => state.value },
  298. actions: { foo: actionSpy },
  299. mutations: { foo: mutationSpy }
  300. }
  301. }
  302. })
  303. expect(store.state.a.value).toBe(1)
  304. expect(store.getters['a/foo']).toBe(1)
  305. store.dispatch('a/foo')
  306. expect(actionSpy.calls.count()).toBe(1)
  307. store.commit('a/foo')
  308. expect(actionSpy.calls.count()).toBe(1)
  309. store.hotUpdate({
  310. modules: {
  311. a: {
  312. namespaced: false
  313. }
  314. }
  315. })
  316. expect(store.state.a.value).toBe(1)
  317. expect(store.getters['a/foo']).toBe(undefined) // removed
  318. expect(store.getters['foo']).toBe(1) // renamed
  319. // should not be called
  320. store.dispatch('a/foo')
  321. expect(actionSpy.calls.count()).toBe(1)
  322. // should be called
  323. store.dispatch('foo')
  324. expect(actionSpy.calls.count()).toBe(2)
  325. // should not be called
  326. store.commit('a/foo')
  327. expect(mutationSpy.calls.count()).toBe(1)
  328. // should be called
  329. store.commit('foo')
  330. expect(mutationSpy.calls.count()).toBe(2)
  331. })
  332. })