toHaveClasses.ts 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. // eslint-disable-next-line sonarjs/cognitive-complexity
  3. export default function toHaveClasses(
  4. this: any,
  5. received: unknown,
  6. classes: string[],
  7. ) {
  8. if (
  9. !received ||
  10. (!(received instanceof HTMLElement) && !(received instanceof SVGElement))
  11. ) {
  12. return {
  13. message: () => 'received is not an HTMLElement or an SVGElement',
  14. pass: false,
  15. }
  16. }
  17. if (!classes) {
  18. return {
  19. message: () => 'no classes passed',
  20. pass: false,
  21. }
  22. }
  23. let pass = true
  24. // const errors: string[] = []
  25. classes.forEach((className) => {
  26. const localPass = received.classList.contains(className)
  27. // if (!localPass) {
  28. // errors.push(`class ${className} not found`)
  29. // }
  30. pass = pass && localPass
  31. })
  32. return {
  33. message: () =>
  34. `received element ${
  35. this.isNot ? 'has' : 'does not have'
  36. } one of the CSS classes: ${classes.join(
  37. ' ',
  38. )}\nClass list: ${received.classList.toString()}`,
  39. pass,
  40. }
  41. }