toBeDescribedBy.ts 946 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. export default function toBeDescribedBy(
  3. this: any,
  4. received: unknown,
  5. expectedText: string,
  6. ) {
  7. if (!received || !(received instanceof HTMLElement)) {
  8. return {
  9. message: () => 'received is not an HTMLElement',
  10. pass: false,
  11. }
  12. }
  13. const notDescribedMessage = `expected element to be described by ${expectedText}`
  14. const describedById = received.getAttribute('aria-describedby')
  15. if (!describedById) {
  16. return {
  17. message: () => notDescribedMessage,
  18. pass: false,
  19. }
  20. }
  21. const descriptionElement = document.getElementById(describedById)
  22. const pass = descriptionElement?.textContent?.includes(expectedText)
  23. if (pass) {
  24. return {
  25. message: () => `expected element not to be described by ${expectedText}`,
  26. pass: true,
  27. }
  28. }
  29. return {
  30. message: () => notDescribedMessage,
  31. pass: false,
  32. }
  33. }