store.spec.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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('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 = jasmine.createSpy()
  166. store._devtoolHook = {
  167. emit: spy
  168. }
  169. const thenSpy = jasmine.createSpy()
  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. spyOn(console, 'warn')
  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(/Store must be called with the new operator/)
  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('subscribe: should handle subscriptions / unsubscriptions', () => {
  270. const subscribeSpy = jasmine.createSpy()
  271. const secondSubscribeSpy = jasmine.createSpy()
  272. const testPayload = 2
  273. const store = new Vuex.Store({
  274. state: {},
  275. mutations: {
  276. [TEST]: () => {}
  277. }
  278. })
  279. const unsubscribe = store.subscribe(subscribeSpy)
  280. store.subscribe(secondSubscribeSpy)
  281. store.commit(TEST, testPayload)
  282. unsubscribe()
  283. store.commit(TEST, testPayload)
  284. expect(subscribeSpy).toHaveBeenCalledWith(
  285. { type: TEST, payload: testPayload },
  286. store.state
  287. )
  288. expect(secondSubscribeSpy).toHaveBeenCalled()
  289. expect(subscribeSpy.calls.count()).toBe(1)
  290. expect(secondSubscribeSpy.calls.count()).toBe(2)
  291. })
  292. // store.watch should only be asserted in non-SSR environment
  293. if (!isSSR) {
  294. it('strict mode: warn mutations outside of handlers', () => {
  295. const store = new Vuex.Store({
  296. state: {
  297. a: 1
  298. },
  299. strict: true
  300. })
  301. Vue.config.silent = true
  302. expect(() => { store.state.a++ }).toThrow()
  303. Vue.config.silent = false
  304. })
  305. it('watch: with resetting vm', done => {
  306. const store = new Vuex.Store({
  307. state: {
  308. count: 0
  309. },
  310. mutations: {
  311. [TEST]: state => state.count++
  312. }
  313. })
  314. const spy = jasmine.createSpy()
  315. store.watch(state => state.count, spy)
  316. // reset store vm
  317. store.registerModule('test', {})
  318. Vue.nextTick(() => {
  319. store.commit(TEST)
  320. expect(store.state.count).toBe(1)
  321. Vue.nextTick(() => {
  322. expect(spy).toHaveBeenCalled()
  323. done()
  324. })
  325. })
  326. })
  327. it('watch: getter function has access to store\'s getters object', done => {
  328. const store = new Vuex.Store({
  329. state: {
  330. count: 0
  331. },
  332. mutations: {
  333. [TEST]: state => state.count++
  334. },
  335. getters: {
  336. getCount: state => state.count
  337. }
  338. })
  339. const getter = function getter (state, getters) {
  340. return state.count
  341. }
  342. const spy = spyOn({ getter }, 'getter').and.callThrough()
  343. const spyCb = jasmine.createSpy()
  344. store.watch(spy, spyCb)
  345. Vue.nextTick(() => {
  346. store.commit(TEST)
  347. expect(store.state.count).toBe(1)
  348. Vue.nextTick(() => {
  349. expect(spy).toHaveBeenCalledWith(store.state, store.getters)
  350. done()
  351. })
  352. })
  353. })
  354. }
  355. })