lists.spec.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { createPinia, setActivePinia } from 'pinia'
  2. import { useListsStore } from '../../../../src/stores/lists.js'
  3. import { createStore } from 'vuex'
  4. import apiModule from '../../../../src/modules/api.js'
  5. setActivePinia(createPinia())
  6. const store = useListsStore()
  7. window.vuex = createStore({
  8. modules: {
  9. api: apiModule
  10. }
  11. })
  12. describe('The lists store', () => {
  13. describe('actions', () => {
  14. it('updates array of all lists', () => {
  15. store.$reset()
  16. const list = { id: '1', title: 'testList' }
  17. store.setLists([list])
  18. expect(store.allLists).to.have.length(1)
  19. expect(store.allLists).to.eql([list])
  20. })
  21. it('adds a new list with a title, updating the title for existing lists', () => {
  22. store.$reset()
  23. const list = { id: '1', title: 'testList' }
  24. const modList = { id: '1', title: 'anotherTestTitle' }
  25. store.setList({ listId: list.id, title: list.title })
  26. expect(store.allListsObject[list.id]).to.eql({ title: list.title, accountIds: [] })
  27. expect(store.allLists).to.have.length(1)
  28. expect(store.allLists[0]).to.eql(list)
  29. store.setList({ listId: modList.id, title: modList.title })
  30. expect(store.allListsObject[modList.id]).to.eql({ title: modList.title, accountIds: [] })
  31. expect(store.allLists).to.have.length(1)
  32. expect(store.allLists[0]).to.eql(modList)
  33. })
  34. it('adds a new list with an array of IDs, updating the IDs for existing lists', () => {
  35. store.$reset()
  36. const list = { id: '1', accountIds: ['1', '2', '3'] }
  37. const modList = { id: '1', accountIds: ['3', '4', '5'] }
  38. store.setListAccounts({ listId: list.id, accountIds: list.accountIds })
  39. expect(store.allListsObject[list.id].accountIds).to.eql(list.accountIds)
  40. store.setListAccounts({ listId: modList.id, accountIds: modList.accountIds })
  41. expect(store.allListsObject[modList.id].accountIds).to.eql(modList.accountIds)
  42. })
  43. it('deletes a list', () => {
  44. store.$patch({
  45. allLists: [{ id: '1', title: 'testList' }],
  46. allListsObject: {
  47. 1: { title: 'testList', accountIds: ['1', '2', '3'] }
  48. }
  49. })
  50. const listId = '1'
  51. store.deleteList({ listId })
  52. expect(store.allLists).to.have.length(0)
  53. expect(store.allListsObject).to.eql({})
  54. })
  55. })
  56. describe('getters', () => {
  57. it('returns list title', () => {
  58. store.$patch({
  59. allLists: [{ id: '1', title: 'testList' }],
  60. allListsObject: {
  61. 1: { title: 'testList', accountIds: ['1', '2', '3'] }
  62. }
  63. })
  64. const id = '1'
  65. expect(store.findListTitle(id)).to.eql('testList')
  66. })
  67. it('returns list accounts', () => {
  68. store.$patch({
  69. allLists: [{ id: '1', title: 'testList' }],
  70. allListsObject: {
  71. 1: { title: 'testList', accountIds: ['1', '2', '3'] }
  72. }
  73. })
  74. const id = '1'
  75. expect(store.findListAccounts(id)).to.eql(['1', '2', '3'])
  76. })
  77. })
  78. })