toBeAvatarElement.ts 3.3 KB

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