hot-reload.spec.js 8.2 KB

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