store.spec.js 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. import Vue from 'vue'
  2. import Vuex from '@/index'
  3. const TEST = 'TEST'
  4. const isSSR = process.env.VUE_ENV === 'server'
  5. describe('Store', () => {
  6. it('committing mutations', () => {
  7. const store = new Vuex.Store({
  8. state: {
  9. a: 1
  10. },
  11. mutations: {
  12. [TEST] (state, n) {
  13. state.a += n
  14. }
  15. }
  16. })
  17. store.commit(TEST, 2)
  18. expect(store.state.a).toBe(3)
  19. })
  20. it('committing with object style', () => {
  21. const store = new Vuex.Store({
  22. state: {
  23. a: 1
  24. },
  25. mutations: {
  26. [TEST] (state, payload) {
  27. state.a += payload.amount
  28. }
  29. }
  30. })
  31. store.commit({
  32. type: TEST,
  33. amount: 2
  34. })
  35. expect(store.state.a).toBe(3)
  36. })
  37. it('asserts committed type', () => {
  38. const store = new Vuex.Store({
  39. state: {
  40. a: 1
  41. },
  42. mutations: {
  43. // Maybe registered with undefined type accidentally
  44. // if the user has typo in a constant type
  45. undefined (state, n) {
  46. state.a += n
  47. }
  48. }
  49. })
  50. expect(() => {
  51. store.commit(undefined, 2)
  52. }).toThrowError(/expects string as the type, but found undefined/)
  53. expect(store.state.a).toBe(1)
  54. })
  55. it('dispatching actions, sync', () => {
  56. const store = new Vuex.Store({
  57. state: {
  58. a: 1
  59. },
  60. mutations: {
  61. [TEST] (state, n) {
  62. state.a += n
  63. }
  64. },
  65. actions: {
  66. [TEST] ({ commit }, n) {
  67. commit(TEST, n)
  68. }
  69. }
  70. })
  71. store.dispatch(TEST, 2)
  72. expect(store.state.a).toBe(3)
  73. })
  74. it('dispatching with object style', () => {
  75. const store = new Vuex.Store({
  76. state: {
  77. a: 1
  78. },
  79. mutations: {
  80. [TEST] (state, n) {
  81. state.a += n
  82. }
  83. },
  84. actions: {
  85. [TEST] ({ commit }, payload) {
  86. commit(TEST, payload.amount)
  87. }
  88. }
  89. })
  90. store.dispatch({
  91. type: TEST,
  92. amount: 2
  93. })
  94. expect(store.state.a).toBe(3)
  95. })
  96. it('dispatching actions, with returned Promise', done => {
  97. const store = new Vuex.Store({
  98. state: {
  99. a: 1
  100. },
  101. mutations: {
  102. [TEST] (state, n) {
  103. state.a += n
  104. }
  105. },
  106. actions: {
  107. [TEST] ({ commit }, n) {
  108. return new Promise(resolve => {
  109. setTimeout(() => {
  110. commit(TEST, n)
  111. resolve()
  112. }, 0)
  113. })
  114. }
  115. }
  116. })
  117. expect(store.state.a).toBe(1)
  118. store.dispatch(TEST, 2).then(() => {
  119. expect(store.state.a).toBe(3)
  120. done()
  121. })
  122. })
  123. it('composing actions with async/await', done => {
  124. const store = new Vuex.Store({
  125. state: {
  126. a: 1
  127. },
  128. mutations: {
  129. [TEST] (state, n) {
  130. state.a += n
  131. }
  132. },
  133. actions: {
  134. [TEST] ({ commit }, n) {
  135. return new Promise(resolve => {
  136. setTimeout(() => {
  137. commit(TEST, n)
  138. resolve()
  139. }, 0)
  140. })
  141. },
  142. two: async ({ commit, dispatch }, n) => {
  143. await dispatch(TEST, 1)
  144. expect(store.state.a).toBe(2)
  145. commit(TEST, n)
  146. }
  147. }
  148. })
  149. expect(store.state.a).toBe(1)
  150. store.dispatch('two', 3).then(() => {
  151. expect(store.state.a).toBe(5)
  152. done()
  153. })
  154. })
  155. it('detecting action Promise errors', done => {
  156. const store = new Vuex.Store({
  157. actions: {
  158. [TEST] () {
  159. return new Promise((resolve, reject) => {
  160. reject('no')
  161. })
  162. }
  163. }
  164. })
  165. const spy = jest.fn()
  166. store._devtoolHook = {
  167. emit: spy
  168. }
  169. const thenSpy = jest.fn()
  170. store.dispatch(TEST)
  171. .then(thenSpy)
  172. .catch(err => {
  173. expect(thenSpy).not.toHaveBeenCalled()
  174. expect(err).toBe('no')
  175. expect(spy).toHaveBeenCalledWith('vuex:error', 'no')
  176. done()
  177. })
  178. })
  179. it('asserts dispatched type', () => {
  180. const store = new Vuex.Store({
  181. state: {
  182. a: 1
  183. },
  184. mutations: {
  185. [TEST] (state, n) {
  186. state.a += n
  187. }
  188. },
  189. actions: {
  190. // Maybe registered with undefined type accidentally
  191. // if the user has typo in a constant type
  192. undefined ({ commit }, n) {
  193. commit(TEST, n)
  194. }
  195. }
  196. })
  197. expect(() => {
  198. store.dispatch(undefined, 2)
  199. }).toThrowError(/expects string as the type, but found undefined/)
  200. expect(store.state.a).toBe(1)
  201. })
  202. it('getters', () => {
  203. const store = new Vuex.Store({
  204. state: {
  205. a: 0
  206. },
  207. getters: {
  208. state: state => state.a > 0 ? 'hasAny' : 'none'
  209. },
  210. mutations: {
  211. [TEST] (state, n) {
  212. state.a += n
  213. }
  214. },
  215. actions: {
  216. check ({ getters }, value) {
  217. // check for exposing getters into actions
  218. expect(getters.state).toBe(value)
  219. }
  220. }
  221. })
  222. expect(store.getters.state).toBe('none')
  223. store.dispatch('check', 'none')
  224. store.commit(TEST, 1)
  225. expect(store.getters.state).toBe('hasAny')
  226. store.dispatch('check', 'hasAny')
  227. })
  228. it('store injection', () => {
  229. const store = new Vuex.Store()
  230. const vm = new Vue({
  231. store
  232. })
  233. const child = new Vue({ parent: vm })
  234. expect(child.$store).toBe(store)
  235. })
  236. it('should warn silent option depreciation', () => {
  237. jest.spyOn(console, 'warn').mockImplementation()
  238. const store = new Vuex.Store({
  239. mutations: {
  240. [TEST] () {}
  241. }
  242. })
  243. store.commit(TEST, {}, { silent: true })
  244. expect(console.warn).toHaveBeenCalledWith(
  245. `[vuex] mutation type: ${TEST}. Silent option has been removed. ` +
  246. 'Use the filter functionality in the vue-devtools'
  247. )
  248. })
  249. it('asserts the call with the new operator', () => {
  250. expect(() => {
  251. Vuex.Store({})
  252. }).toThrowError(/Cannot call a class as a function/)
  253. })
  254. it('should accept state as function', () => {
  255. const store = new Vuex.Store({
  256. state: () => ({
  257. a: 1
  258. }),
  259. mutations: {
  260. [TEST] (state, n) {
  261. state.a += n
  262. }
  263. }
  264. })
  265. expect(store.state.a).toBe(1)
  266. store.commit(TEST, 2)
  267. expect(store.state.a).toBe(3)
  268. })
  269. it('should not call root state function twice', () => {
  270. const spy = jest.fn().mockReturnValue(1)
  271. new Vuex.Store({
  272. state: spy
  273. })
  274. expect(spy).toHaveBeenCalledTimes(1)
  275. })
  276. it('subscribe: should handle subscriptions / unsubscriptions', () => {
  277. const subscribeSpy = jest.fn()
  278. const secondSubscribeSpy = jest.fn()
  279. const testPayload = 2
  280. const store = new Vuex.Store({
  281. state: {},
  282. mutations: {
  283. [TEST]: () => {}
  284. }
  285. })
  286. const unsubscribe = store.subscribe(subscribeSpy)
  287. store.subscribe(secondSubscribeSpy)
  288. store.commit(TEST, testPayload)
  289. unsubscribe()
  290. store.commit(TEST, testPayload)
  291. expect(subscribeSpy).toHaveBeenCalledWith(
  292. { type: TEST, payload: testPayload },
  293. store.state
  294. )
  295. expect(secondSubscribeSpy).toHaveBeenCalled()
  296. expect(subscribeSpy).toHaveBeenCalledTimes(1)
  297. expect(secondSubscribeSpy).toHaveBeenCalledTimes(2)
  298. })
  299. it('subscribe: should handle subscriptions with synchronous unsubscriptions', () => {
  300. const subscribeSpy = jest.fn()
  301. const testPayload = 2
  302. const store = new Vuex.Store({
  303. state: {},
  304. mutations: {
  305. [TEST]: () => {}
  306. }
  307. })
  308. const unsubscribe = store.subscribe(() => unsubscribe())
  309. store.subscribe(subscribeSpy)
  310. store.commit(TEST, testPayload)
  311. expect(subscribeSpy).toHaveBeenCalledWith(
  312. { type: TEST, payload: testPayload },
  313. store.state
  314. )
  315. expect(subscribeSpy).toHaveBeenCalledTimes(1)
  316. })
  317. it('subscribeAction: should handle subscriptions with synchronous unsubscriptions', () => {
  318. const subscribeSpy = jest.fn()
  319. const testPayload = 2
  320. const store = new Vuex.Store({
  321. state: {},
  322. actions: {
  323. [TEST]: () => {}
  324. }
  325. })
  326. const unsubscribe = store.subscribeAction(() => unsubscribe())
  327. store.subscribeAction(subscribeSpy)
  328. store.dispatch(TEST, testPayload)
  329. expect(subscribeSpy).toHaveBeenCalledWith(
  330. { type: TEST, payload: testPayload },
  331. store.state
  332. )
  333. expect(subscribeSpy).toHaveBeenCalledTimes(1)
  334. })
  335. // store.watch should only be asserted in non-SSR environment
  336. if (!isSSR) {
  337. it('strict mode: warn mutations outside of handlers', () => {
  338. const spy = jest.spyOn(console, 'error').mockImplementation()
  339. const store = new Vuex.Store({
  340. state: {
  341. a: 1
  342. },
  343. strict: true
  344. })
  345. Vue.config.silent = true
  346. store.state.a++
  347. expect(spy).toHaveBeenCalled()
  348. Vue.config.silent = false
  349. })
  350. it('watch: with resetting vm', done => {
  351. const store = new Vuex.Store({
  352. state: {
  353. count: 0
  354. },
  355. mutations: {
  356. [TEST]: state => state.count++
  357. }
  358. })
  359. const spy = jest.fn()
  360. store.watch(state => state.count, spy)
  361. // reset store vm
  362. store.registerModule('test', {})
  363. Vue.nextTick(() => {
  364. store.commit(TEST)
  365. expect(store.state.count).toBe(1)
  366. Vue.nextTick(() => {
  367. expect(spy).toHaveBeenCalled()
  368. done()
  369. })
  370. })
  371. })
  372. it('watch: getter function has access to store\'s getters object', done => {
  373. const store = new Vuex.Store({
  374. state: {
  375. count: 0
  376. },
  377. mutations: {
  378. [TEST]: state => state.count++
  379. },
  380. getters: {
  381. getCount: state => state.count
  382. }
  383. })
  384. const getter = function getter (state, getters) {
  385. return state.count
  386. }
  387. const spy = jest.spyOn({ getter }, 'getter')
  388. const spyCb = jest.fn()
  389. store.watch(spy, spyCb)
  390. Vue.nextTick(() => {
  391. store.commit(TEST)
  392. expect(store.state.count).toBe(1)
  393. Vue.nextTick(() => {
  394. expect(spy).toHaveBeenCalledWith(store.state, store.getters)
  395. done()
  396. })
  397. })
  398. })
  399. }
  400. })