toBeAvatarElement.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 = received.classList.contains('opacity-60')
  60. const localPass = options.outOfOffice ? isOutOfOffice : !isOutOfOffice
  61. if (!localPass) {
  62. errors.push(
  63. `out of office class is ${options.outOfOffice ? 'missing' : 'present'}`,
  64. )
  65. }
  66. pass = pass && localPass
  67. }
  68. }
  69. if (options.active != null) {
  70. const isActive =
  71. options.type === 'user'
  72. ? !received.classList.contains('opacity-20 grayscale')
  73. : !!received.querySelector('use[href="#icon-organization"]')
  74. const localPass = options.active ? isActive : !isActive
  75. if (!localPass) {
  76. errors.push(`active class is ${options.active ? 'missing' : 'present'}`)
  77. }
  78. pass = pass && isActive
  79. }
  80. if (options.image != null) {
  81. if (options.type === 'organization') {
  82. pass = false
  83. errors.push(`organization avatar doesn't have an image`)
  84. } else {
  85. const style = window.getComputedStyle(received)
  86. const imageStyle = `url(/api/users/image/${options.image})`
  87. const hasImage = style.backgroundImage === imageStyle
  88. if (!hasImage) {
  89. errors.push(
  90. `avatar has image ${style.backgroundImage} instead of ${imageStyle}`,
  91. )
  92. }
  93. pass = pass && hasImage
  94. }
  95. }
  96. return {
  97. message: () =>
  98. `received element is${
  99. this.isNot ? '' : ' not'
  100. } a correct avatar: ${errors.join('\n')}\n${prettyDOM(received)}`,
  101. pass,
  102. }
  103. }