store.spec.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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('dispatching actions, sync', () => {
  37. const store = new Vuex.Store({
  38. state: {
  39. a: 1
  40. },
  41. mutations: {
  42. [TEST] (state, n) {
  43. state.a += n
  44. }
  45. },
  46. actions: {
  47. [TEST] ({ commit }, n) {
  48. commit(TEST, n)
  49. }
  50. }
  51. })
  52. store.dispatch(TEST, 2)
  53. expect(store.state.a).toBe(3)
  54. })
  55. it('dispatching with object style', () => {
  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 }, payload) {
  67. commit(TEST, payload.amount)
  68. }
  69. }
  70. })
  71. store.dispatch({
  72. type: TEST,
  73. amount: 2
  74. })
  75. expect(store.state.a).toBe(3)
  76. })
  77. it('dispatching actions, with returned Promise', done => {
  78. const store = new Vuex.Store({
  79. state: {
  80. a: 1
  81. },
  82. mutations: {
  83. [TEST] (state, n) {
  84. state.a += n
  85. }
  86. },
  87. actions: {
  88. [TEST] ({ commit }, n) {
  89. return new Promise(resolve => {
  90. setTimeout(() => {
  91. commit(TEST, n)
  92. resolve()
  93. }, 0)
  94. })
  95. }
  96. }
  97. })
  98. expect(store.state.a).toBe(1)
  99. store.dispatch(TEST, 2).then(() => {
  100. expect(store.state.a).toBe(3)
  101. done()
  102. })
  103. })
  104. it('composing actions with async/await', done => {
  105. const store = new Vuex.Store({
  106. state: {
  107. a: 1
  108. },
  109. mutations: {
  110. [TEST] (state, n) {
  111. state.a += n
  112. }
  113. },
  114. actions: {
  115. [TEST] ({ commit }, n) {
  116. return new Promise(resolve => {
  117. setTimeout(() => {
  118. commit(TEST, n)
  119. resolve()
  120. }, 0)
  121. })
  122. },
  123. two: async ({ commit, dispatch }, n) => {
  124. await dispatch(TEST, 1)
  125. expect(store.state.a).toBe(2)
  126. commit(TEST, n)
  127. }
  128. }
  129. })
  130. expect(store.state.a).toBe(1)
  131. store.dispatch('two', 3).then(() => {
  132. expect(store.state.a).toBe(5)
  133. done()
  134. })
  135. })
  136. it('detecting action Promise errors', done => {
  137. const store = new Vuex.Store({
  138. actions: {
  139. [TEST] () {
  140. return new Promise((resolve, reject) => {
  141. reject('no')
  142. })
  143. }
  144. }
  145. })
  146. const spy = jasmine.createSpy()
  147. store._devtoolHook = {
  148. emit: spy
  149. }
  150. const thenSpy = jasmine.createSpy()
  151. store.dispatch(TEST)
  152. .then(thenSpy)
  153. .catch(err => {
  154. expect(thenSpy).not.toHaveBeenCalled()
  155. expect(err).toBe('no')
  156. expect(spy).toHaveBeenCalledWith('vuex:error', 'no')
  157. done()
  158. })
  159. })
  160. it('getters', () => {
  161. const store = new Vuex.Store({
  162. state: {
  163. a: 0
  164. },
  165. getters: {
  166. state: state => state.a > 0 ? 'hasAny' : 'none'
  167. },
  168. mutations: {
  169. [TEST] (state, n) {
  170. state.a += n
  171. }
  172. },
  173. actions: {
  174. check ({ getters }, value) {
  175. // check for exposing getters into actions
  176. expect(getters.state).toBe(value)
  177. }
  178. }
  179. })
  180. expect(store.getters.state).toBe('none')
  181. store.dispatch('check', 'none')
  182. store.commit(TEST, 1)
  183. expect(store.getters.state).toBe('hasAny')
  184. store.dispatch('check', 'hasAny')
  185. })
  186. it('store injection', () => {
  187. const store = new Vuex.Store()
  188. const vm = new Vue({
  189. store
  190. })
  191. const child = new Vue({ parent: vm })
  192. expect(child.$store).toBe(store)
  193. })
  194. it('should warn silent option depreciation', function () {
  195. spyOn(console, 'warn')
  196. const store = new Vuex.Store({
  197. mutations: {
  198. [TEST] () {}
  199. },
  200. })
  201. store.commit(TEST, {}, { silent: true });
  202. expect(console.warn).toHaveBeenCalledWith(
  203. `[vuex] mutation type: ${TEST}. Silent option has been removed. ` +
  204. 'Use the filter functionality in the vue-devtools'
  205. )
  206. })
  207. it('strict mode: warn mutations outside of handlers', function () {
  208. const store = new Vuex.Store({
  209. state: {
  210. a: 1
  211. },
  212. strict: true
  213. })
  214. Vue.config.silent = true
  215. expect(() => { store.state.a++ }).toThrow()
  216. Vue.config.silent = false
  217. })
  218. it('watch: with resetting vm', done => {
  219. const store = new Vuex.Store({
  220. state: {
  221. count: 0
  222. },
  223. mutations: {
  224. [TEST]: state => state.count++
  225. }
  226. })
  227. const spy = jasmine.createSpy()
  228. store.watch(state => state.count, spy)
  229. // reset store vm
  230. store.registerModule('test', {})
  231. Vue.nextTick(() => {
  232. store.commit(TEST)
  233. expect(store.state.count).toBe(1)
  234. Vue.nextTick(() => {
  235. expect(spy).toHaveBeenCalled()
  236. done()
  237. })
  238. })
  239. })
  240. it('watch: getter function has access to store\'s getters object', done => {
  241. const store = new Vuex.Store({
  242. state: {
  243. count: 0
  244. },
  245. mutations: {
  246. [TEST]: state => state.count++
  247. },
  248. getters: {
  249. getCount: state => state.count
  250. }
  251. })
  252. const getter = function getter (state, getters) {
  253. return state.count
  254. }
  255. const spy = spyOn({ getter }, 'getter').and.callThrough()
  256. const spyCb = jasmine.createSpy()
  257. store.watch(spy, spyCb)
  258. Vue.nextTick(() => {
  259. store.commit(TEST)
  260. expect(store.state.count).toBe(1)
  261. Vue.nextTick(() => {
  262. expect(spy).toHaveBeenCalledWith(store.state, store.getters)
  263. done()
  264. })
  265. })
  266. })
  267. })