toHaveClasses.ts 997 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 (!received || !(received instanceof HTMLElement)) {
  9. return {
  10. message: () => 'received is not an HTMLElement',
  11. pass: false,
  12. }
  13. }
  14. if (!classes) {
  15. return {
  16. message: () => 'no classes passed',
  17. pass: false,
  18. }
  19. }
  20. let pass = true
  21. // const errors: string[] = []
  22. classes.forEach((className) => {
  23. const localPass = received.classList.contains(className)
  24. // if (!localPass) {
  25. // errors.push(`class ${className} not found`)
  26. // }
  27. pass = pass && localPass
  28. })
  29. return {
  30. message: () =>
  31. `received element ${
  32. this.isNot ? 'has' : 'does not have'
  33. } one of the CSS classes: ${classes.join(
  34. ' ',
  35. )}\nClass list: ${received.classList.toString()}`,
  36. pass,
  37. }
  38. }