accessibility.cy.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // eslint-disable-next-line import/no-webpack-loader-syntax, import/no-unresolved
  2. import style from '!raw-loader!../css/default.css'
  3. const testCases = {
  4. 'Main text': {
  5. foregroundColors: [
  6. 'color-main-text',
  7. // 'color-text-light', deprecated
  8. // 'color-text-lighter', deprecated
  9. 'color-text-maxcontrast',
  10. 'color-text-maxcontrast-default',
  11. ],
  12. backgroundColors: [
  13. 'color-background-main',
  14. 'color-background-hover',
  15. 'color-background-dark',
  16. // 'color-background-darker', this should only be used for elements not for text
  17. ],
  18. },
  19. Primary: {
  20. foregroundColors: [
  21. 'color-primary-text',
  22. ],
  23. backgroundColors: [
  24. // 'color-primary-default', this should only be used for elements not for text!
  25. // 'color-primary-hover', this should only be used for elements and not for text!
  26. 'color-primary',
  27. ],
  28. },
  29. 'Primary light': {
  30. foregroundColors: [
  31. 'color-primary-light-text',
  32. ],
  33. backgroundColors: [
  34. 'color-primary-light',
  35. 'color-primary-light-hover',
  36. ],
  37. },
  38. 'Primary element': {
  39. foregroundColors: [
  40. 'color-primary-element-text',
  41. 'color-primary-element-text-dark',
  42. ],
  43. backgroundColors: [
  44. 'color-primary-element',
  45. 'color-primary-element-hover',
  46. ],
  47. },
  48. 'Primary element light': {
  49. foregroundColors: [
  50. 'color-primary-element-light-text',
  51. ],
  52. backgroundColors: [
  53. 'color-primary-element-light',
  54. 'color-primary-element-light-hover',
  55. ],
  56. },
  57. 'Servity information texts': {
  58. foregroundColors: [
  59. 'color-error-text',
  60. 'color-warning-text',
  61. 'color-success-text',
  62. 'color-info-text',
  63. ],
  64. backgroundColors: [
  65. 'color-background-main',
  66. 'color-background-hover',
  67. ],
  68. },
  69. }
  70. /**
  71. * Create a wrapper element with color and background set
  72. *
  73. * @param foreground The foreground color (css variable without leading --)
  74. * @param background The background color
  75. */
  76. function createTestCase(foreground: string, background: string) {
  77. const wrapper = document.createElement('div')
  78. wrapper.innerText = `${foreground} ${background}`
  79. wrapper.style.color = `var(--${foreground})`
  80. wrapper.style.backgroundColor = `var(--${background})`
  81. wrapper.style.padding = '4px'
  82. wrapper.setAttribute('data-cy-testcase', '')
  83. return wrapper
  84. }
  85. describe('Accessibility of Nextcloud theming', () => {
  86. before(() => {
  87. cy.injectAxe()
  88. const el = document.createElement('style')
  89. el.innerText = style
  90. document.head.appendChild(el)
  91. })
  92. beforeEach(() => {
  93. cy.document().then(doc => {
  94. const root = doc.querySelector('[data-cy-root]')
  95. if (root === null) {
  96. throw new Error('No test root found')
  97. }
  98. for (const child of root.children) {
  99. root.removeChild(child)
  100. }
  101. })
  102. })
  103. for (const [name, { backgroundColors, foregroundColors }] of Object.entries(testCases)) {
  104. context(`Accessibility of CSS color variables for ${name}`, () => {
  105. for (const foreground of foregroundColors) {
  106. for (const background of backgroundColors) {
  107. it(`color contrast of ${foreground} on ${background}`, () => {
  108. const element = createTestCase(foreground, background)
  109. cy.document().then(doc => {
  110. const root = doc.querySelector('[data-cy-root]')
  111. // eslint-disable-next-line no-unused-expressions
  112. expect(root).not.to.be.undefined
  113. // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
  114. root!.appendChild(element)
  115. cy.checkA11y('[data-cy-testcase]')
  116. })
  117. })
  118. }
  119. }
  120. })
  121. }
  122. })