store.spec.js 9.5 KB

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