helpers.spec.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. // Skipping for now due to unkown error;
  2. // TypeError: Cannot read property 'createComment' of null
  3. //
  4. // import { createApp } from 'vue'
  5. // import Vuex, { mapState, mapMutations, mapGetters, mapActions, createNamespacedHelpers } from '../../dist/vuex.common.js'
  6. // function mount (comp, store) {
  7. // const app = createApp({ ...comp, render: () => {} })
  8. // app.use(Vuex, store)
  9. // return app.mount({ appendChild: () => {} })
  10. // }
  11. // describe('Helpers', () => {
  12. // it('mapState (array)', () => {
  13. // const store = new Vuex.Store({
  14. // state: {
  15. // a: 1
  16. // }
  17. // })
  18. // const vm = mount({
  19. // computed: mapState(['a'])
  20. // }, store)
  21. // expect(vm.a).toBe(1)
  22. // store.state.a++
  23. // expect(vm.a).toBe(2)
  24. // })
  25. // it('mapState (object)', () => {
  26. // const store = new Vuex.Store({
  27. // state: {
  28. // a: 1
  29. // },
  30. // getters: {
  31. // b: () => 2
  32. // }
  33. // })
  34. // const vm = mount({
  35. // computed: mapState({
  36. // a: (state, getters) => {
  37. // return state.a + getters.b
  38. // }
  39. // })
  40. // }, store)
  41. // expect(vm.a).toBe(3)
  42. // store.state.a++
  43. // expect(vm.a).toBe(4)
  44. // })
  45. // it('mapState (with namespace)', () => {
  46. // const store = new Vuex.Store({
  47. // modules: {
  48. // foo: {
  49. // namespaced: true,
  50. // state: { a: 1 },
  51. // getters: {
  52. // b: state => state.a + 1
  53. // }
  54. // }
  55. // }
  56. // })
  57. // const vm = mount({
  58. // computed: mapState('foo', {
  59. // a: (state, getters) => {
  60. // return state.a + getters.b
  61. // }
  62. // })
  63. // }, store)
  64. // expect(vm.a).toBe(3)
  65. // store.state.foo.a++
  66. // expect(vm.a).toBe(5)
  67. // store.replaceState({
  68. // foo: { a: 3 }
  69. // })
  70. // expect(vm.a).toBe(7)
  71. // })
  72. // // #708
  73. // it('mapState (with namespace and a nested module)', () => {
  74. // const store = new Vuex.Store({
  75. // modules: {
  76. // foo: {
  77. // namespaced: true,
  78. // state: { a: 1 },
  79. // modules: {
  80. // bar: {
  81. // state: { b: 2 }
  82. // }
  83. // }
  84. // }
  85. // }
  86. // })
  87. // const vm = mount({
  88. // computed: mapState('foo', {
  89. // value: state => state
  90. // })
  91. // }, store)
  92. // expect(vm.value.a).toBe(1)
  93. // expect(vm.value.bar.b).toBe(2)
  94. // expect(vm.value.b).toBeUndefined()
  95. // })
  96. // it('mapState (with undefined states)', () => {
  97. // spyOn(console, 'error')
  98. // const store = new Vuex.Store({
  99. // modules: {
  100. // foo: {
  101. // namespaced: true,
  102. // state: { a: 1 }
  103. // }
  104. // }
  105. // })
  106. // const vm = mount({
  107. // computed: mapState('foo')
  108. // }, store)
  109. // expect(vm.a).toBeUndefined()
  110. // expect(console.error).toHaveBeenCalledWith('[vuex] mapState: mapper parameter must be either an Array or an Object')
  111. // })
  112. // it('mapMutations (array)', () => {
  113. // const store = new Vuex.Store({
  114. // state: { count: 0 },
  115. // mutations: {
  116. // inc: state => state.count++,
  117. // dec: state => state.count--
  118. // }
  119. // })
  120. // const vm = mount({
  121. // methods: mapMutations(['inc', 'dec'])
  122. // }, store)
  123. // vm.inc()
  124. // expect(store.state.count).toBe(1)
  125. // vm.dec()
  126. // expect(store.state.count).toBe(0)
  127. // })
  128. // it('mapMutations (object)', () => {
  129. // const store = new Vuex.Store({
  130. // state: { count: 0 },
  131. // mutations: {
  132. // inc: state => state.count++,
  133. // dec: state => state.count--
  134. // }
  135. // })
  136. // const vm = mount({
  137. // methods: mapMutations({
  138. // plus: 'inc',
  139. // minus: 'dec'
  140. // })
  141. // }, store)
  142. // vm.plus()
  143. // expect(store.state.count).toBe(1)
  144. // vm.minus()
  145. // expect(store.state.count).toBe(0)
  146. // })
  147. // it('mapMutations (function)', () => {
  148. // const store = new Vuex.Store({
  149. // state: { count: 0 },
  150. // mutations: {
  151. // inc (state, amount) {
  152. // state.count += amount
  153. // }
  154. // }
  155. // })
  156. // const vm = mount({
  157. // methods: mapMutations({
  158. // plus (commit, amount) {
  159. // commit('inc', amount + 1)
  160. // }
  161. // })
  162. // }, store)
  163. // vm.plus(42)
  164. // expect(store.state.count).toBe(43)
  165. // })
  166. // it('mapMutations (with namespace)', () => {
  167. // const store = new Vuex.Store({
  168. // modules: {
  169. // foo: {
  170. // namespaced: true,
  171. // state: { count: 0 },
  172. // mutations: {
  173. // inc: state => state.count++,
  174. // dec: state => state.count--
  175. // }
  176. // }
  177. // }
  178. // })
  179. // const vm = mount({
  180. // methods: mapMutations('foo', {
  181. // plus: 'inc',
  182. // minus: 'dec'
  183. // })
  184. // }, store)
  185. // vm.plus()
  186. // expect(store.state.foo.count).toBe(1)
  187. // vm.minus()
  188. // expect(store.state.foo.count).toBe(0)
  189. // })
  190. // it('mapMutations (function with namepsace)', () => {
  191. // const store = new Vuex.Store({
  192. // modules: {
  193. // foo: {
  194. // namespaced: true,
  195. // state: { count: 0 },
  196. // mutations: {
  197. // inc (state, amount) {
  198. // state.count += amount
  199. // }
  200. // }
  201. // }
  202. // }
  203. // })
  204. // const vm = mount({
  205. // methods: mapMutations('foo', {
  206. // plus (commit, amount) {
  207. // commit('inc', amount + 1)
  208. // }
  209. // })
  210. // }, store)
  211. // vm.plus(42)
  212. // expect(store.state.foo.count).toBe(43)
  213. // })
  214. // it('mapMutations (with undefined mutations)', () => {
  215. // spyOn(console, 'error')
  216. // const store = new Vuex.Store({
  217. // modules: {
  218. // foo: {
  219. // namespaced: true,
  220. // state: { count: 0 },
  221. // mutations: {
  222. // inc: state => state.count++,
  223. // dec: state => state.count--
  224. // }
  225. // }
  226. // }
  227. // })
  228. // const vm = mount({
  229. // methods: mapMutations('foo')
  230. // }, store)
  231. // expect(vm.inc).toBeUndefined()
  232. // expect(vm.dec).toBeUndefined()
  233. // expect(console.error).toHaveBeenCalledWith('[vuex] mapMutations: mapper parameter must be either an Array or an Object')
  234. // })
  235. // it('mapGetters (array)', () => {
  236. // const store = new Vuex.Store({
  237. // state: { count: 0 },
  238. // mutations: {
  239. // inc: state => state.count++,
  240. // dec: state => state.count--
  241. // },
  242. // getters: {
  243. // hasAny: ({ count }) => count > 0,
  244. // negative: ({ count }) => count < 0
  245. // }
  246. // })
  247. // const vm = mount({
  248. // computed: mapGetters(['hasAny', 'negative'])
  249. // }, store)
  250. // expect(vm.hasAny).toBe(false)
  251. // expect(vm.negative).toBe(false)
  252. // store.commit('inc')
  253. // expect(vm.hasAny).toBe(true)
  254. // expect(vm.negative).toBe(false)
  255. // store.commit('dec')
  256. // store.commit('dec')
  257. // expect(vm.hasAny).toBe(false)
  258. // expect(vm.negative).toBe(true)
  259. // })
  260. // it('mapGetters (object)', () => {
  261. // const store = new Vuex.Store({
  262. // state: { count: 0 },
  263. // mutations: {
  264. // inc: state => state.count++,
  265. // dec: state => state.count--
  266. // },
  267. // getters: {
  268. // hasAny: ({ count }) => count > 0,
  269. // negative: ({ count }) => count < 0
  270. // }
  271. // })
  272. // const vm = mount({
  273. // computed: mapGetters({
  274. // a: 'hasAny',
  275. // b: 'negative'
  276. // })
  277. // }, store)
  278. // expect(vm.a).toBe(false)
  279. // expect(vm.b).toBe(false)
  280. // store.commit('inc')
  281. // expect(vm.a).toBe(true)
  282. // expect(vm.b).toBe(false)
  283. // store.commit('dec')
  284. // store.commit('dec')
  285. // expect(vm.a).toBe(false)
  286. // expect(vm.b).toBe(true)
  287. // })
  288. // it('mapGetters (with namespace)', () => {
  289. // const store = new Vuex.Store({
  290. // modules: {
  291. // foo: {
  292. // namespaced: true,
  293. // state: { count: 0 },
  294. // mutations: {
  295. // inc: state => state.count++,
  296. // dec: state => state.count--
  297. // },
  298. // getters: {
  299. // hasAny: ({ count }) => count > 0,
  300. // negative: ({ count }) => count < 0
  301. // }
  302. // }
  303. // }
  304. // })
  305. // const vm = mount({
  306. // computed: mapGetters('foo', {
  307. // a: 'hasAny',
  308. // b: 'negative'
  309. // })
  310. // }, store)
  311. // expect(vm.a).toBe(false)
  312. // expect(vm.b).toBe(false)
  313. // store.commit('foo/inc')
  314. // expect(vm.a).toBe(true)
  315. // expect(vm.b).toBe(false)
  316. // store.commit('foo/dec')
  317. // store.commit('foo/dec')
  318. // expect(vm.a).toBe(false)
  319. // expect(vm.b).toBe(true)
  320. // })
  321. // it('mapGetters (with namespace and nested module)', () => {
  322. // const store = new Vuex.Store({
  323. // modules: {
  324. // foo: {
  325. // namespaced: true,
  326. // modules: {
  327. // bar: {
  328. // namespaced: true,
  329. // state: { count: 0 },
  330. // mutations: {
  331. // inc: state => state.count++,
  332. // dec: state => state.count--
  333. // },
  334. // getters: {
  335. // hasAny: ({ count }) => count > 0,
  336. // negative: ({ count }) => count < 0
  337. // }
  338. // },
  339. // cat: {
  340. // state: { count: 9 },
  341. // getters: {
  342. // count: ({ count }) => count
  343. // }
  344. // }
  345. // }
  346. // }
  347. // }
  348. // })
  349. // const vm = mount({
  350. // computed: {
  351. // ...mapGetters('foo/bar', [
  352. // 'hasAny',
  353. // 'negative'
  354. // ]),
  355. // ...mapGetters('foo', [
  356. // 'count'
  357. // ])
  358. // }
  359. // }, store)
  360. // expect(vm.hasAny).toBe(false)
  361. // expect(vm.negative).toBe(false)
  362. // store.commit('foo/bar/inc')
  363. // expect(vm.hasAny).toBe(true)
  364. // expect(vm.negative).toBe(false)
  365. // store.commit('foo/bar/dec')
  366. // store.commit('foo/bar/dec')
  367. // expect(vm.hasAny).toBe(false)
  368. // expect(vm.negative).toBe(true)
  369. // expect(vm.count).toBe(9)
  370. // })
  371. // it('mapGetters (with undefined getters)', () => {
  372. // spyOn(console, 'error')
  373. // const store = new Vuex.Store({
  374. // modules: {
  375. // foo: {
  376. // namespaced: true,
  377. // state: { count: 0 },
  378. // mutations: {
  379. // inc: state => state.count++,
  380. // dec: state => state.count--
  381. // },
  382. // getters: {
  383. // hasAny: ({ count }) => count > 0,
  384. // negative: ({ count }) => count < 0
  385. // }
  386. // }
  387. // }
  388. // })
  389. // const vm = mount({
  390. // computed: mapGetters('foo')
  391. // }, store)
  392. // expect(vm.a).toBeUndefined()
  393. // expect(vm.b).toBeUndefined()
  394. // expect(console.error).toHaveBeenCalledWith('[vuex] mapGetters: mapper parameter must be either an Array or an Object')
  395. // })
  396. // it('mapActions (array)', () => {
  397. // const a = jasmine.createSpy()
  398. // const b = jasmine.createSpy()
  399. // const store = new Vuex.Store({
  400. // actions: {
  401. // a,
  402. // b
  403. // }
  404. // })
  405. // const vm = mount({
  406. // methods: mapActions(['a', 'b'])
  407. // }, store)
  408. // vm.a()
  409. // expect(a).toHaveBeenCalled()
  410. // expect(b).not.toHaveBeenCalled()
  411. // vm.b()
  412. // expect(b).toHaveBeenCalled()
  413. // })
  414. // it('mapActions (object)', () => {
  415. // const a = jasmine.createSpy()
  416. // const b = jasmine.createSpy()
  417. // const store = new Vuex.Store({
  418. // actions: {
  419. // a,
  420. // b
  421. // }
  422. // })
  423. // const vm = mount({
  424. // methods: mapActions({
  425. // foo: 'a',
  426. // bar: 'b'
  427. // })
  428. // }, store)
  429. // vm.foo()
  430. // expect(a).toHaveBeenCalled()
  431. // expect(b).not.toHaveBeenCalled()
  432. // vm.bar()
  433. // expect(b).toHaveBeenCalled()
  434. // })
  435. // it('mapActions (function)', () => {
  436. // const a = jasmine.createSpy()
  437. // const store = new Vuex.Store({
  438. // actions: { a }
  439. // })
  440. // const vm = mount({
  441. // methods: mapActions({
  442. // foo (dispatch, arg) {
  443. // dispatch('a', arg + 'bar')
  444. // }
  445. // })
  446. // }, store)
  447. // vm.foo('foo')
  448. // expect(a.calls.argsFor(0)[1]).toBe('foobar')
  449. // })
  450. // it('mapActions (with namespace)', () => {
  451. // const a = jasmine.createSpy()
  452. // const b = jasmine.createSpy()
  453. // const store = new Vuex.Store({
  454. // modules: {
  455. // foo: {
  456. // namespaced: true,
  457. // actions: {
  458. // a,
  459. // b
  460. // }
  461. // }
  462. // }
  463. // })
  464. // const vm = mount({
  465. // methods: mapActions('foo/', {
  466. // foo: 'a',
  467. // bar: 'b'
  468. // })
  469. // }, store)
  470. // vm.foo()
  471. // expect(a).toHaveBeenCalled()
  472. // expect(b).not.toHaveBeenCalled()
  473. // vm.bar()
  474. // expect(b).toHaveBeenCalled()
  475. // })
  476. // it('mapActions (function with namespace)', () => {
  477. // const a = jasmine.createSpy()
  478. // const store = new Vuex.Store({
  479. // modules: {
  480. // foo: {
  481. // namespaced: true,
  482. // actions: { a }
  483. // }
  484. // }
  485. // })
  486. // const vm = mount({
  487. // methods: mapActions('foo/', {
  488. // foo (dispatch, arg) {
  489. // dispatch('a', arg + 'bar')
  490. // }
  491. // })
  492. // }, store)
  493. // vm.foo('foo')
  494. // expect(a.calls.argsFor(0)[1]).toBe('foobar')
  495. // })
  496. // it('mapActions (with undefined actions)', () => {
  497. // spyOn(console, 'error')
  498. // const a = jasmine.createSpy()
  499. // const store = new Vuex.Store({
  500. // modules: {
  501. // foo: {
  502. // namespaced: true,
  503. // actions: {
  504. // a
  505. // }
  506. // }
  507. // }
  508. // })
  509. // const vm = mount({
  510. // methods: mapActions('foo/')
  511. // }, store)
  512. // expect(vm.a).toBeUndefined()
  513. // expect(a).not.toHaveBeenCalled()
  514. // expect(console.error).toHaveBeenCalledWith('[vuex] mapActions: mapper parameter must be either an Array or an Object')
  515. // })
  516. // it('createNamespacedHelpers', () => {
  517. // const actionA = jasmine.createSpy()
  518. // const actionB = jasmine.createSpy()
  519. // const store = new Vuex.Store({
  520. // modules: {
  521. // foo: {
  522. // namespaced: true,
  523. // state: { count: 0 },
  524. // getters: {
  525. // isEven: state => state.count % 2 === 0
  526. // },
  527. // mutations: {
  528. // inc: state => state.count++,
  529. // dec: state => state.count--
  530. // },
  531. // actions: {
  532. // actionA,
  533. // actionB
  534. // }
  535. // }
  536. // }
  537. // })
  538. // const {
  539. // mapState,
  540. // mapGetters,
  541. // mapMutations,
  542. // mapActions
  543. // } = createNamespacedHelpers('foo/')
  544. // const vm = mount({
  545. // computed: {
  546. // ...mapState(['count']),
  547. // ...mapGetters(['isEven'])
  548. // },
  549. // methods: {
  550. // ...mapMutations(['inc', 'dec']),
  551. // ...mapActions(['actionA', 'actionB'])
  552. // }
  553. // }, store)
  554. // expect(vm.count).toBe(0)
  555. // expect(vm.isEven).toBe(true)
  556. // store.state.foo.count++
  557. // expect(vm.count).toBe(1)
  558. // expect(vm.isEven).toBe(false)
  559. // vm.inc()
  560. // expect(store.state.foo.count).toBe(2)
  561. // expect(store.getters['foo/isEven']).toBe(true)
  562. // vm.dec()
  563. // expect(store.state.foo.count).toBe(1)
  564. // expect(store.getters['foo/isEven']).toBe(false)
  565. // vm.actionA()
  566. // expect(actionA).toHaveBeenCalled()
  567. // expect(actionB).not.toHaveBeenCalled()
  568. // vm.actionB()
  569. // expect(actionB).toHaveBeenCalled()
  570. // })
  571. // })