CommonUserAvatar.spec.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { useDateFormat } from '@vueuse/shared'
  3. import { renderComponent } from '#tests/support/components/index.ts'
  4. import { convertToGraphQLId } from '#shared/graphql/utils.ts'
  5. import { initializeUserAvatarClasses } from '#shared/initializer/initializeUserAvatarClasses.ts'
  6. import { SYSTEM_USER_ID } from '#shared/utils/constants.ts'
  7. import CommonUserAvatar, { type Props } from '../CommonUserAvatar.vue'
  8. const USER_ID = convertToGraphQLId('User', '123')
  9. vi.hoisted(() => {
  10. vi.setSystemTime(new Date('2024-11-11T00:00:00Z'))
  11. })
  12. initializeUserAvatarClasses({
  13. backgroundColors: [
  14. 'bg-gray',
  15. 'bg-red-bright',
  16. 'bg-yellow',
  17. 'bg-blue',
  18. 'bg-green',
  19. 'bg-pink',
  20. 'bg-orange',
  21. ],
  22. })
  23. describe('CommonUserAvatar', () => {
  24. it('renders user avatar', async () => {
  25. const view = renderComponent(CommonUserAvatar, {
  26. props: <Props>{
  27. entity: {
  28. id: USER_ID,
  29. firstname: 'John',
  30. lastname: 'Doe',
  31. },
  32. },
  33. })
  34. const avatar = view.getByTestId('common-avatar')
  35. expect(avatar).toHaveTextContent('JD')
  36. expect(avatar).toHaveClass('bg-blue')
  37. await view.rerender(<Props>{
  38. entity: {
  39. id: USER_ID,
  40. image: '100.png',
  41. firstname: 'John',
  42. lastname: 'Doe',
  43. },
  44. })
  45. expect(avatar).toHaveStyle(
  46. 'background-image: url(/api/users/image/100.png)',
  47. )
  48. expect(avatar).not.toHaveTextContent('JD')
  49. })
  50. it('renders system user', () => {
  51. const view = renderComponent(CommonUserAvatar, {
  52. props: <Props>{
  53. entity: {
  54. id: SYSTEM_USER_ID,
  55. },
  56. },
  57. })
  58. const avatar = view.getByTestId('common-avatar')
  59. expect(avatar).toHaveStyle({
  60. backgroundImage:
  61. 'url(/app/frontend/shared/components/CommonUserAvatar/assets/logo.svg)',
  62. })
  63. })
  64. it('renders icon by source', async () => {
  65. const view = renderComponent(CommonUserAvatar, {
  66. props: <Props>{
  67. entity: {
  68. id: USER_ID,
  69. source: 'twitter',
  70. },
  71. },
  72. })
  73. expect(view.getByIconName('twitter')).toBeInTheDocument()
  74. await view.rerender(<Props>{
  75. entity: {
  76. id: USER_ID,
  77. source: 'facebook',
  78. },
  79. })
  80. expect(view.getByIconName('facebook')).toBeInTheDocument()
  81. await view.rerender(<Props>{
  82. entity: {
  83. id: USER_ID,
  84. source: 'some-unknown-source',
  85. },
  86. })
  87. expect(view.queryByIconName('some-unknown-source')).not.toBeInTheDocument()
  88. })
  89. it('renders active', async () => {
  90. const view = renderComponent(CommonUserAvatar, {
  91. props: <Props>{
  92. entity: {
  93. id: USER_ID,
  94. active: true,
  95. },
  96. },
  97. })
  98. const avatar = view.getByTestId('common-avatar')
  99. expect(avatar).not.toHaveClass('grayscale')
  100. expect(avatar).not.toHaveClass('grayscale-[70%]')
  101. await view.rerender(<Props>{
  102. entity: {
  103. id: USER_ID,
  104. active: false,
  105. },
  106. })
  107. expect(avatar).toHaveClass('grayscale')
  108. })
  109. it('renders crown for vip', async () => {
  110. const view = renderComponent(CommonUserAvatar, {
  111. props: <Props>{
  112. entity: {
  113. id: USER_ID,
  114. vip: true,
  115. },
  116. },
  117. })
  118. expect(view.getByIconName('crown')).toBeInTheDocument()
  119. await view.rerender(<Props>{
  120. entity: {
  121. id: USER_ID,
  122. vip: true,
  123. },
  124. personal: true,
  125. })
  126. expect(view.queryByIconName('crown')).not.toBeInTheDocument()
  127. })
  128. it('can render initials only', async () => {
  129. const view = renderComponent(CommonUserAvatar, {
  130. props: <Props>{
  131. entity: {
  132. id: USER_ID,
  133. image: '100.png',
  134. firstname: 'John',
  135. lastname: 'Doe',
  136. },
  137. initialsOnly: true,
  138. },
  139. })
  140. const avatar = view.getByTestId('common-avatar')
  141. expect(avatar).toHaveTextContent('JD')
  142. expect(avatar).toHaveClass('bg-blue')
  143. expect(avatar).not.toHaveStyle(
  144. 'background-image: url(/api/users/image/100.png)',
  145. )
  146. })
  147. describe('out of office state', () => {
  148. let today: string
  149. beforeAll(() => {
  150. today = useDateFormat(new Date(), 'YYYY-MM-DD').value
  151. })
  152. it('out of office date is in presentence', async () => {
  153. const view = renderComponent(CommonUserAvatar, {
  154. props: <Props>{
  155. entity: {
  156. id: USER_ID,
  157. active: true,
  158. },
  159. },
  160. })
  161. await view.rerender(<Props>{
  162. entity: {
  163. id: USER_ID,
  164. outOfOffice: true,
  165. outOfOfficeStartAt: '2024-10-11',
  166. outOfOfficeEndAt: '2024-12-11',
  167. },
  168. })
  169. const avatar = view.getByTestId('common-avatar')
  170. expect(avatar).toHaveClass('grayscale-[70%]')
  171. })
  172. it('out of office date is in past', async () => {
  173. const view = renderComponent(CommonUserAvatar, {
  174. props: <Props>{
  175. entity: {
  176. id: USER_ID,
  177. active: true,
  178. },
  179. },
  180. })
  181. const [year, month, day] = today.split('-')
  182. const outOfOfficeStartAt = `${year}-${(+month - 2).toString().padStart(2, '0')}-${day}`
  183. const outOfOfficeEndAt = `${year}-${(+month - 1).toString().padStart(2, '0')}-${day}`
  184. await view.rerender(<Props>{
  185. entity: {
  186. id: USER_ID,
  187. outOfOffice: true,
  188. outOfOfficeStartAt,
  189. outOfOfficeEndAt,
  190. },
  191. })
  192. const avatar = view.getByTestId('common-avatar')
  193. expect(avatar).not.toHaveClass('grayscale-[70%]')
  194. })
  195. it('out of office date is in future', async () => {
  196. const view = renderComponent(CommonUserAvatar, {
  197. props: <Props>{
  198. entity: {
  199. id: USER_ID,
  200. active: true,
  201. },
  202. },
  203. })
  204. await view.rerender(<Props>{
  205. entity: {
  206. id: USER_ID,
  207. outOfOffice: true,
  208. outOfOfficeStartAt: '2024-12-11',
  209. outOfOfficeEndAt: '2025-01-11',
  210. },
  211. })
  212. const avatar = view.getByTestId('common-avatar')
  213. expect(avatar).not.toHaveClass('grayscale-[70%]')
  214. })
  215. })
  216. })