summaryrefslogtreecommitdiffstats
path: root/src/components/AvatarWrapper/AvatarWrapper.spec.js
blob: c7f0eaf9bc0a6ca802e5e998adc1fbe0f9530880 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { shallowMount } from '@vue/test-utils'
import { cloneDeep } from 'lodash'
import Vuex from 'vuex'

import NcAvatar from '@nextcloud/vue/dist/Components/NcAvatar.js'

import AvatarWrapper from './AvatarWrapper.vue'

import storeConfig from '../../store/storeConfig.js'

describe('AvatarWrapper.vue', () => {
	let testStoreConfig
	let store
	const USER_ID = 'user-id'
	const USER_NAME = 'John Doe'
	const PRELOADED_USER_STATUS = { status: 'online', message: null, icon: null }
	const MENU_CONTAINER = '#menu-container'

	beforeEach(() => {
		testStoreConfig = cloneDeep(storeConfig)
		testStoreConfig.modules.uiModeStore.getters.getMainContainerSelector = jest.fn().mockReturnValue(() => MENU_CONTAINER)
		store = new Vuex.Store(testStoreConfig)
	})

	describe('render user avatar', () => {
		test('component renders NcAvatar with standard size by default', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: USER_NAME,
				},
			})

			const avatar = wrapper.findComponent(NcAvatar)
			expect(avatar.exists()).toBeTruthy()
			expect(avatar.props('size')).toBe(44)
		})

		test('component does not render NcAvatar for non-users', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: 'emails',
					source: 'emails',
				},
			})

			const avatar = wrapper.findComponent(NcAvatar)
			expect(avatar.exists()).toBeFalsy()
		})

		test('component renders NcAvatar with smaller size', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: USER_NAME,
					small: true,
				},
			})

			const avatar = wrapper.findComponent(NcAvatar)
			expect(avatar.props('size')).toBe(22)
		})

		test('component pass props to NcAvatar correctly', async () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					id: USER_ID,
					name: USER_NAME,
					showUserStatus: true,
					preloadedUserStatus: PRELOADED_USER_STATUS,
				},
			})

			const avatar = wrapper.findComponent(NcAvatar)
			await avatar.vm.$nextTick()

			expect(avatar.props('user')).toBe(USER_ID)
			expect(avatar.props('displayName')).toBe(USER_NAME)
			expect(avatar.props('menuContainer')).toBe(MENU_CONTAINER)
			expect(avatar.props('showUserStatus')).toBe(true)
			expect(avatar.props('showUserStatusCompact')).toBe(false)
			expect(avatar.props('preloadedUserStatus')).toBe(PRELOADED_USER_STATUS)
			expect(avatar.props('size')).toBe(44)
		})
	})

	describe('render specific icons', () => {
		test('component render emails icon properly', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: 'emails',
					source: 'emails',
				},
			})

			const icon = wrapper.find('.icon')
			expect(icon.exists()).toBeTruthy()
			expect(icon.classes('icon-mail')).toBeTruthy()
		})

		test('component render groups icon properly', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: 'groups',
					source: 'groups',
				},
			})

			const icon = wrapper.find('.icon')
			expect(icon.exists()).toBeTruthy()
			expect(icon.classes('icon-contacts')).toBeTruthy()
		})
	})

	describe('render guests', () => {
		test('component render icon of guest properly', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: t('spreed', 'Guest'),
					source: 'guests',
				},
			})

			const guest = wrapper.find('.guest')
			expect(guest.exists()).toBeTruthy()
			expect(guest.text()).toBe('?')
		})

		test('component render icon of guest with name properly', () => {
			const wrapper = shallowMount(AvatarWrapper, {
				store,
				propsData: {
					name: USER_NAME,
					source: 'guests',
				},
			})

			const guest = wrapper.find('.guest')
			expect(guest.text()).toBe(USER_NAME.charAt(0))
		})