users.spec.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { cloneDeep } from 'lodash'
  2. import { defaultState, mutations, getters } from '../../../../src/modules/users.js'
  3. describe('The users module', () => {
  4. describe('mutations', () => {
  5. it('adds new users to the set, merging in new information for old users', () => {
  6. const state = cloneDeep(defaultState)
  7. const user = { id: '1', name: 'Guy' }
  8. const modUser = { id: '1', name: 'Dude' }
  9. mutations.addNewUsers(state, [user])
  10. expect(state.users).to.have.length(1)
  11. expect(state.users).to.eql([user])
  12. mutations.addNewUsers(state, [modUser])
  13. expect(state.users).to.have.length(1)
  14. expect(state.users).to.eql([user])
  15. expect(state.users[0].name).to.eql('Dude')
  16. })
  17. it('merging array field in new information for old users', () => {
  18. const state = cloneDeep(defaultState)
  19. const user = {
  20. id: '1',
  21. fields: [
  22. { name: 'Label 1', value: 'Content 1' }
  23. ]
  24. }
  25. const firstModUser = {
  26. id: '1',
  27. fields: [
  28. { name: 'Label 2', value: 'Content 2' },
  29. { name: 'Label 3', value: 'Content 3' }
  30. ]
  31. }
  32. const secondModUser = {
  33. id: '1',
  34. fields: [
  35. { name: 'Label 4', value: 'Content 4' }
  36. ]
  37. }
  38. mutations.addNewUsers(state, [user])
  39. expect(state.users[0].fields).to.have.length(1)
  40. expect(state.users[0].fields[0].name).to.eql('Label 1')
  41. mutations.addNewUsers(state, [firstModUser])
  42. expect(state.users[0].fields).to.have.length(2)
  43. expect(state.users[0].fields[0].name).to.eql('Label 2')
  44. expect(state.users[0].fields[1].name).to.eql('Label 3')
  45. mutations.addNewUsers(state, [secondModUser])
  46. expect(state.users[0].fields).to.have.length(1)
  47. expect(state.users[0].fields[0].name).to.eql('Label 4')
  48. })
  49. })
  50. describe('findUser', () => {
  51. it('returns user with matching screen_name', () => {
  52. const user = { screen_name: 'Guy', id: '1' }
  53. const state = {
  54. usersObject: {
  55. 1: user,
  56. guy: user
  57. }
  58. }
  59. const name = 'Guy'
  60. const expected = { screen_name: 'Guy', id: '1' }
  61. expect(getters.findUser(state)(name)).to.eql(expected)
  62. })
  63. it('returns user with matching id', () => {
  64. const user = { screen_name: 'Guy', id: '1' }
  65. const state = {
  66. usersObject: {
  67. 1: user,
  68. guy: user
  69. }
  70. }
  71. const id = '1'
  72. const expected = { screen_name: 'Guy', id: '1' }
  73. expect(getters.findUser(state)(id)).to.eql(expected)
  74. })
  75. })
  76. })