CommonAvatar.spec.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { renderComponent } from '#tests/support/components/index.ts'
  3. import CommonAvatar from '../CommonAvatar.vue'
  4. describe('CommonAvatar.vue', () => {
  5. it('renders when no props are passed', () => {
  6. const view = renderComponent(CommonAvatar)
  7. const avatar = view.getByTestId('common-avatar')
  8. expect(avatar).toHaveTextContent('??')
  9. expect(avatar).toHaveClass('size-medium')
  10. expect(avatar).toHaveStyle({ 'background-image': '' })
  11. })
  12. it('renders initials and chooses color based on it', async () => {
  13. const view = renderComponent(CommonAvatar, {
  14. props: { initials: 'VL' },
  15. })
  16. const avatar = view.getByTestId('common-avatar')
  17. expect(avatar).toHaveTextContent('VL')
  18. await view.rerender({ initials: 'VS' })
  19. expect(avatar).toHaveTextContent('VS')
  20. await view.rerender({ initials: '??' })
  21. expect(avatar).toHaveTextContent('??')
  22. })
  23. it('renders an image, if it is provided', async () => {
  24. const view = renderComponent(CommonAvatar, {
  25. props: { image: '/api/v1/users/image/123', initials: 'VL' },
  26. })
  27. const avatar = view.getByTestId('common-avatar')
  28. expect(
  29. avatar,
  30. "don't render initials, when image is present to not overlap it",
  31. ).not.toHaveTextContent('VL')
  32. expect(avatar).toHaveStyle({
  33. 'background-image': 'url("/api/v1/users/image/123")',
  34. })
  35. await view.rerender({ image: 'data:image/png;base64,1' })
  36. expect(
  37. avatar,
  38. 'renders base64 as an image instead of relying on API',
  39. ).toHaveStyle({
  40. 'background-image': 'url("data:image/png;base64,1")',
  41. })
  42. })
  43. it('renders an icon, if provided', () => {
  44. const view = renderComponent(CommonAvatar, {
  45. props: { icon: 'facebook', initials: 'VL' },
  46. })
  47. const avatar = view.getByTestId('common-avatar')
  48. expect(avatar).not.toHaveTextContent('VL')
  49. expect(view.getByIconName('facebook')).toBeInTheDocument()
  50. })
  51. it('renders different sizes', async () => {
  52. const view = renderComponent(CommonAvatar)
  53. const avatar = view.getByTestId('common-avatar')
  54. expect(avatar).toHaveClass('size-medium')
  55. expect(avatar).not.toHaveClass('size-small')
  56. expect(avatar).not.toHaveClass('size-large')
  57. await view.rerender({ size: 'small' })
  58. expect(avatar).not.toHaveClass('size-medium')
  59. expect(avatar).toHaveClass('size-small')
  60. expect(avatar).not.toHaveClass('size-large')
  61. await view.rerender({ size: 'large' })
  62. expect(avatar).not.toHaveClass('size-medium')
  63. expect(avatar).not.toHaveClass('size-small')
  64. expect(avatar).toHaveClass('size-large')
  65. })
  66. it('renders vip icon', async () => {
  67. const view = renderComponent(CommonAvatar)
  68. expect(view.queryByIconName('crown')).not.toBeInTheDocument()
  69. await view.rerender({ vipIcon: 'crown' })
  70. expect(view.getByIconName('crown')).toBeInTheDocument()
  71. })
  72. })