store.spec.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. import Vue from 'vue/dist/vue.common.js'
  2. import Vuex from '../../dist/vuex.js'
  3. const TEST = 'TEST'
  4. describe('Store', () => {
  5. it('committing mutations', () => {
  6. const store = new Vuex.Store({
  7. state: {
  8. a: 1
  9. },
  10. mutations: {
  11. [TEST] (state, n) {
  12. state.a += n
  13. }
  14. }
  15. })
  16. store.commit(TEST, 2)
  17. expect(store.state.a).toBe(3)
  18. })
  19. it('committing with object style', () => {
  20. const store = new Vuex.Store({
  21. state: {
  22. a: 1
  23. },
  24. mutations: {
  25. [TEST] (state, payload) {
  26. state.a += payload.amount
  27. }
  28. }
  29. })
  30. store.commit({
  31. type: TEST,
  32. amount: 2
  33. })
  34. expect(store.state.a).toBe(3)
  35. })
  36. it('asserts committed type', () => {
  37. const store = new Vuex.Store({
  38. state: {
  39. a: 1
  40. },
  41. mutations: {
  42. // Maybe registered with undefined type accidentally
  43. // if the user has typo in a constant type
  44. undefined (state, n) {
  45. state.a += n
  46. }
  47. }
  48. })
  49. expect(() => {
  50. store.commit(undefined, 2)
  51. }).toThrowError(/Expects string as the type, but found undefined/)
  52. expect(store.state.a).toBe(1)
  53. })
  54. it('dispatching actions, sync', () => {
  55. const store = new Vuex.Store({
  56. state: {
  57. a: 1
  58. },
  59. mutations: {
  60. [TEST] (state, n) {
  61. state.a += n
  62. }
  63. },
  64. actions: {
  65. [TEST] ({ commit }, n) {
  66. commit(TEST, n)
  67. }
  68. }
  69. })
  70. store.dispatch(TEST, 2)
  71. expect(store.state.a).toBe(3)
  72. })
  73. it('dispatching with object style', () => {
  74. const store = new Vuex.Store({
  75. state: {
  76. a: 1
  77. },
  78. mutations: {
  79. [TEST] (state, n) {
  80. state.a += n
  81. }
  82. },
  83. actions: {
  84. [TEST] ({ commit }, payload) {
  85. commit(TEST, payload.amount)
  86. }
  87. }
  88. })
  89. store.dispatch({
  90. type: TEST,
  91. amount: 2
  92. })
  93. expect(store.state.a).toBe(3)
  94. })
  95. it('dispatching actions, with returned Promise', done => {
  96. const store = new Vuex.Store({
  97. state: {
  98. a: 1
  99. },
  100. mutations: {
  101. [TEST] (state, n) {
  102. state.a += n
  103. }
  104. },
  105. actions: {
  106. [TEST] ({ commit }, n) {
  107. return new Promise(resolve => {
  108. setTimeout(() => {
  109. commit(TEST, n)
  110. resolve()
  111. }, 0)
  112. })
  113. }
  114. }
  115. })
  116. expect(store.state.a).toBe(1)
  117. store.dispatch(TEST, 2).then(() => {
  118. expect(store.state.a).toBe(3)
  119. done()
  120. })
  121. })
  122. it('composing actions with async/await', done => {
  123. const store = new Vuex.Store({
  124. state: {
  125. a: 1
  126. },
  127. mutations: {
  128. [TEST] (state, n) {
  129. state.a += n
  130. }
  131. },
  132. actions: {
  133. [TEST] ({ commit }, n) {
  134. return new Promise(resolve => {
  135. setTimeout(() => {
  136. commit(TEST, n)
  137. resolve()
  138. }, 0)
  139. })
  140. },
  141. two: async ({ commit, dispatch }, n) => {
  142. await dispatch(TEST, 1)
  143. expect(store.state.a).toBe(2)
  144. commit(TEST, n)
  145. }
  146. }
  147. })
  148. expect(store.state.a).toBe(1)
  149. store.dispatch('two', 3).then(() => {
  150. expect(store.state.a).toBe(5)
  151. done()
  152. })
  153. })
  154. it('detecting action Promise errors', done => {
  155. const store = new Vuex.Store({
  156. actions: {
  157. [TEST] () {
  158. return new Promise((resolve, reject) => {
  159. reject('no')
  160. })
  161. }
  162. }
  163. })
  164. const spy = jasmine.createSpy()
  165. store._devtoolHook = {
  166. emit: spy
  167. }
  168. const thenSpy = jasmine.createSpy()
  169. store.dispatch(TEST)
  170. .then(thenSpy)
  171. .catch(err => {
  172. expect(thenSpy).not.toHaveBeenCalled()
  173. expect(err).toBe('no')
  174. expect(spy).toHaveBeenCalledWith('vuex:error', 'no')
  175. done()
  176. })
  177. })
  178. it('asserts dispatched type', () => {
  179. const store = new Vuex.Store({
  180. state: {
  181. a: 1
  182. },
  183. mutations: {
  184. [TEST] (state, n) {
  185. state.a += n
  186. }
  187. },
  188. actions: {
  189. // Maybe registered with undefined type accidentally
  190. // if the user has typo in a constant type
  191. undefined ({ commit }, n) {
  192. commit(TEST, n)
  193. }
  194. }
  195. })
  196. expect(() => {
  197. store.dispatch(undefined, 2)
  198. }).toThrowError(/Expects string as the type, but found undefined/)
  199. expect(store.state.a).toBe(1)
  200. })
  201. it('getters', () => {
  202. const store = new Vuex.Store({
  203. state: {
  204. a: 0
  205. },
  206. getters: {
  207. state: state => state.a > 0 ? 'hasAny' : 'none'
  208. },
  209. mutations: {
  210. [TEST] (state, n) {
  211. state.a += n
  212. }
  213. },
  214. actions: {
  215. check ({ getters }, value) {
  216. // check for exposing getters into actions
  217. expect(getters.state).toBe(value)
  218. }
  219. }
  220. })
  221. expect(store.getters.state).toBe('none')
  222. store.dispatch('check', 'none')
  223. store.commit(TEST, 1)
  224. expect(store.getters.state).toBe('hasAny')
  225. store.dispatch('check', 'hasAny')
  226. })
  227. it('store injection', () => {
  228. const store = new Vuex.Store()
  229. const vm = new Vue({
  230. store
  231. })
  232. const child = new Vue({ parent: vm })
  233. expect(child.$store).toBe(store)
  234. })
  235. it('should warn silent option depreciation', function () {
  236. spyOn(console, 'warn')
  237. const store = new Vuex.Store({
  238. mutations: {
  239. [TEST] () {}
  240. }
  241. })
  242. store.commit(TEST, {}, { silent: true })
  243. expect(console.warn).toHaveBeenCalledWith(
  244. `[vuex] mutation type: ${TEST}. Silent option has been removed. ` +
  245. 'Use the filter functionality in the vue-devtools'
  246. )
  247. })
  248. it('strict mode: warn mutations outside of handlers', function () {
  249. const store = new Vuex.Store({
  250. state: {
  251. a: 1
  252. },
  253. strict: true
  254. })
  255. Vue.config.silent = true
  256. expect(() => { store.state.a++ }).toThrow()
  257. Vue.config.silent = false
  258. })
  259. it('watch: with resetting vm', done => {
  260. const store = new Vuex.Store({
  261. state: {
  262. count: 0
  263. },
  264. mutations: {
  265. [TEST]: state => state.count++
  266. }
  267. })
  268. const spy = jasmine.createSpy()
  269. store.watch(state => state.count, spy)
  270. // reset store vm
  271. store.registerModule('test', {})
  272. Vue.nextTick(() => {
  273. store.commit(TEST)
  274. expect(store.state.count).toBe(1)
  275. Vue.nextTick(() => {
  276. expect(spy).toHaveBeenCalled()
  277. done()
  278. })
  279. })
  280. })
  281. it('watch: getter function has access to store\'s getters object', done => {
  282. const store = new Vuex.Store({
  283. state: {
  284. count: 0
  285. },
  286. mutations: {
  287. [TEST]: state => state.count++
  288. },
  289. getters: {
  290. getCount: state => state.count
  291. }
  292. })
  293. const getter = function getter (state, getters) {
  294. return state.count
  295. }
  296. const spy = spyOn({ getter }, 'getter').and.callThrough()
  297. const spyCb = jasmine.createSpy()
  298. store.watch(spy, spyCb)
  299. Vue.nextTick(() => {
  300. store.commit(TEST)
  301. expect(store.state.count).toBe(1)
  302. Vue.nextTick(() => {
  303. expect(spy).toHaveBeenCalledWith(store.state, store.getters)
  304. done()
  305. })
  306. })
  307. })
  308. })