aboutsummaryrefslogtreecommitdiff
path: root/test/unit/specs/modules/users.spec.js
blob: dfa5684d15bc5def3973d318a7339b6ac6952ef4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { cloneDeep } from 'lodash'

import { defaultState, mutations, getters } from '../../../../src/modules/users.js'

describe('The users module', () => {
  describe('mutations', () => {
    it('adds new users to the set, merging in new information for old users', () => {
      const state = cloneDeep(defaultState)
      const user = { id: '1', name: 'Guy' }
      const modUser = { id: '1', name: 'Dude' }

      mutations.addNewUsers(state, [user])
      expect(state.users).to.have.length(1)
      expect(state.users).to.eql([user])

      mutations.addNewUsers(state, [modUser])
      expect(state.users).to.have.length(1)
      expect(state.users).to.eql([user])
      expect(state.users[0].name).to.eql('Dude')
    })

    it('merging array field in new information for old users', () => {
      const state = cloneDeep(defaultState)
      const user = {
        id: '1',
        fields: [
          { name: 'Label 1', value: 'Content 1' }
        ]
      }
      const firstModUser = {
        id: '1',
        fields: [
          { name: 'Label 2', value: 'Content 2' },
          { name: 'Label 3', value: 'Content 3' }
        ]
      }
      const secondModUser = {
        id: '1',
        fields: [
          { name: 'Label 4', value: 'Content 4' }
        ]
      }

      mutations.addNewUsers(state, [user])
      expect(state.users[0].fields).to.have.length(1)
      expect(state.users[0].fields[0].name).to.eql('Label 1')

      mutations.addNewUsers(state, [firstModUser])
      expect(state.users[0].fields).to.have.length(2)
      expect(state.users[0].fields[0].name).to.eql('Label 2')
      expect(state.users[0].fields[1].name).to.eql('Label 3')

      mutations.addNewUsers(state, [secondModUser])
      expect(state.users[0].fields).to.have.length(1)
      expect(state.users[0].fields[0].name).to.eql('Label 4')
    })
  })

  describe('findUser', () => {
    it('returns user with matching screen_name', () => {
      const user = { screen_name: 'Guy', id: '1' }
      const state = {
        usersObject: {
          1: user,
          guy: user
        }
      }
      const name = 'Guy'
      const expected = { screen_name: 'Guy', id: '1' }
      expect(getters.findUser(state)(name)).to.eql(expected)
    })

    it('returns user with matching id', () => {
      const user = { screen_name: 'Guy', id: '1' }
      const state = {
        usersObject: {
          1: user,
          guy: user
        }
      }
      const id = '1'
      const expected = { screen_name: 'Guy', id: '1' }
      expect(getters.findUser(state)(id)).to.eql(expected)
    })
  })
})