hot-reload.spec.js 8.1 KB

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