toBeAvatarElement.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. import { prettyDOM } from '@testing-library/vue'
  3. export interface ToBeAvatarOptions {
  4. vip?: boolean
  5. outOfOffice?: boolean
  6. active?: boolean
  7. image?: string
  8. type: 'user' | 'organization'
  9. }
  10. // eslint-disable-next-line sonarjs/cognitive-complexity
  11. export default function toBeAvatar(
  12. this: any,
  13. received: unknown,
  14. options: ToBeAvatarOptions,
  15. ) {
  16. if (!received || !(received instanceof HTMLElement)) {
  17. return {
  18. message: () => 'received is not an HTMLElement',
  19. pass: false,
  20. }
  21. }
  22. if (received.dataset.testId !== 'common-avatar') {
  23. return {
  24. message: () =>
  25. `received element is not an avatar\n${prettyDOM(received)}`,
  26. pass: false,
  27. }
  28. }
  29. if (!options) {
  30. return {
  31. message: () => 'received element is an avatar',
  32. pass: true,
  33. }
  34. }
  35. let pass = true
  36. const errors: string[] = []
  37. if (options.vip != null) {
  38. // TODO: if names are different in desktop, we should use a different name here
  39. const iconName = options.type === 'user' ? 'crown' : 'crown-silver'
  40. const icon = received.querySelector(`use[href="#icon-${iconName}"]`)
  41. const localPass = options.vip ? !!icon : !icon
  42. if (!localPass) {
  43. errors.push(`vip icon is ${options.vip ? 'missing' : 'present'}`)
  44. }
  45. pass = pass && localPass
  46. }
  47. if (options.outOfOffice != null) {
  48. const isOutOfOffice =
  49. received.classList.contains('opacity-100') &&
  50. received.classList.contains('grayscale-[70%]')
  51. const localPass = options.outOfOffice ? isOutOfOffice : !isOutOfOffice
  52. if (!localPass) {
  53. errors.push(
  54. `out of office class is ${options.outOfOffice ? 'missing' : 'present'}`,
  55. )
  56. }
  57. pass = pass && localPass
  58. }
  59. if (options.active != null) {
  60. const isActive =
  61. options.type === 'user'
  62. ? !received.classList.contains('opacity-20 grayscale')
  63. : !!received.querySelector('use[href="#icon-organization"]')
  64. const localPass = options.active ? isActive : !isActive
  65. if (!localPass) {
  66. errors.push(`active class is ${options.active ? 'missing' : 'present'}`)
  67. }
  68. pass = pass && isActive
  69. }
  70. if (options.image != null) {
  71. if (options.type === 'organization') {
  72. pass = false
  73. errors.push(`organization avatar doesn't have an image`)
  74. } else {
  75. const style = window.getComputedStyle(received)
  76. const imageStyle = `url(/api/users/image/${options.image})`
  77. const hasImage = style.backgroundImage === imageStyle
  78. if (!hasImage) {
  79. errors.push(
  80. `avatar has image ${style.backgroundImage} instead of ${imageStyle}`,
  81. )
  82. }
  83. pass = pass && hasImage
  84. }
  85. }
  86. return {
  87. message: () =>
  88. `received element is${
  89. this.isNot ? '' : ' not'
  90. } a correct avatar: ${errors.join('\n')}\n${prettyDOM(received)}`,
  91. pass,
  92. }
  93. }