hot-reload.spec.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // import { createApp, nextTick } from 'vue'
  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. // Skipping for now due to unkown error;
  228. // TypeError: Cannot read property 'createComment' of null
  229. //
  230. // it('getters', done => {
  231. // const store = new Vuex.Store({
  232. // state: {
  233. // count: 0
  234. // },
  235. // mutations: {
  236. // inc: state => state.count++
  237. // },
  238. // getters: {
  239. // count: state => state.count
  240. // },
  241. // actions: {
  242. // check ({ getters }, value) {
  243. // expect(getters.count).toBe(value)
  244. // }
  245. // }
  246. // })
  247. // const spy = jasmine.createSpy()
  248. // const app = createApp({
  249. // computed: {
  250. // a: () => store.getters.count
  251. // },
  252. // watch: {
  253. // a: spy
  254. // },
  255. // render () {}
  256. // })
  257. // app.use(Vuex, store)
  258. // const vm = app.mount({ appendChild: () => {} })
  259. // expect(vm.a).toBe(0)
  260. // store.dispatch('check', 0)
  261. // store.commit('inc')
  262. // expect(vm.a).toBe(1)
  263. // store.dispatch('check', 1)
  264. // // update getters
  265. // store.hotUpdate({
  266. // getters: {
  267. // count: state => state.count * 10
  268. // }
  269. // })
  270. // expect(vm.a).toBe(10)
  271. // store.dispatch('check', 10)
  272. // if (isSSR) {
  273. // done()
  274. // } else {
  275. // nextTick(() => {
  276. // expect(spy).toHaveBeenCalled()
  277. // done()
  278. // })
  279. // }
  280. // })
  281. it('provide warning if a new module is given', () => {
  282. const store = new Vuex.Store({})
  283. spyOn(console, 'warn')
  284. store.hotUpdate({
  285. modules: {
  286. test: {
  287. state: {
  288. count: 0
  289. }
  290. }
  291. }
  292. })
  293. expect(console.warn).toHaveBeenCalledWith(
  294. '[vuex] trying to add a new module \'test\' on hot reloading, ' +
  295. 'manual reload is needed'
  296. )
  297. })
  298. it('update namespace', () => {
  299. // prevent to print notification of unknown action/mutation
  300. spyOn(console, 'error')
  301. const actionSpy = jasmine.createSpy()
  302. const mutationSpy = jasmine.createSpy()
  303. const store = new Vuex.Store({
  304. modules: {
  305. a: {
  306. namespaced: true,
  307. state: { value: 1 },
  308. getters: { foo: state => state.value },
  309. actions: { foo: actionSpy },
  310. mutations: { foo: mutationSpy }
  311. }
  312. }
  313. })
  314. expect(store.state.a.value).toBe(1)
  315. expect(store.getters['a/foo']).toBe(1)
  316. store.dispatch('a/foo')
  317. expect(actionSpy.calls.count()).toBe(1)
  318. store.commit('a/foo')
  319. expect(actionSpy.calls.count()).toBe(1)
  320. store.hotUpdate({
  321. modules: {
  322. a: {
  323. namespaced: false
  324. }
  325. }
  326. })
  327. expect(store.state.a.value).toBe(1)
  328. expect(store.getters['a/foo']).toBe(undefined) // removed
  329. expect(store.getters['foo']).toBe(1) // renamed
  330. // should not be called
  331. store.dispatch('a/foo')
  332. expect(actionSpy.calls.count()).toBe(1)
  333. // should be called
  334. store.dispatch('foo')
  335. expect(actionSpy.calls.count()).toBe(2)
  336. // should not be called
  337. store.commit('a/foo')
  338. expect(mutationSpy.calls.count()).toBe(1)
  339. // should be called
  340. store.commit('foo')
  341. expect(mutationSpy.calls.count()).toBe(2)
  342. })
  343. })