themingUtils.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
  3. *
  4. * @author John Molakvoæ <skjnldsv@protonmail.com>
  5. *
  6. * @license AGPL-3.0-or-later
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. import { colord } from 'colord'
  23. export const defaultPrimary = '#0082c9'
  24. export const defaultAccessiblePrimary = '#006aa3'
  25. export const defaultBackground = 'kamil-porembinski-clouds.jpg'
  26. /**
  27. * Validate the current page body css variables
  28. *
  29. * @param {string} expectedColor the expected color
  30. * @param {string|null} expectedBackground the expected background
  31. */
  32. export const validateBodyThemingCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
  33. // We must use `Cypress.$` here as any assertions (get is an assertion) is not allowed in wait-until's check function, see documentation
  34. const guestBackgroundColor = Cypress.$('body').css('background-color')
  35. const guestBackgroundImage = Cypress.$('body').css('background-image')
  36. const isValidBackgroundColor = colord(guestBackgroundColor).isEqual(expectedColor)
  37. const isValidBackgroundImage = !expectedBackground
  38. ? guestBackgroundImage === 'none'
  39. : guestBackgroundImage.includes(expectedBackground)
  40. console.debug({ guestBackgroundColor: colord(guestBackgroundColor).toHex(), guestBackgroundImage, expectedColor, expectedBackground, isValidBackgroundColor, isValidBackgroundImage })
  41. return isValidBackgroundColor && isValidBackgroundImage
  42. }
  43. /**
  44. * Validate the user theming default select option css
  45. *
  46. * @param {string} expectedColor the expected color
  47. * @param {string} expectedBackground the expected background
  48. */
  49. export const validateUserThemingDefaultCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
  50. const defaultSelectButton = Cypress.$('[data-user-theming-background-default]')
  51. const customColorSelectButton = Cypress.$('[data-user-theming-background-color]')
  52. if (defaultSelectButton.length === 0 || customColorSelectButton.length === 0) {
  53. return false
  54. }
  55. const defaultOptionBackground = defaultSelectButton.css('background-image')
  56. const colorPickerOptionColor = customColorSelectButton.css('background-color')
  57. const isValidBackgroundImage = !expectedBackground
  58. ? defaultOptionBackground === 'none'
  59. : defaultOptionBackground.includes(expectedBackground)
  60. console.debug({ colorPickerOptionColor: colord(colorPickerOptionColor).toHex(), expectedColor, isValidBackgroundImage })
  61. return isValidBackgroundImage && colord(colorPickerOptionColor).isEqual(expectedColor)
  62. }
  63. export const pickRandomColor = function(pickerSelector: string): Cypress.Chainable<string> {
  64. // Pick one of the first 8 options
  65. const randColour = Math.floor(Math.random() * 8)
  66. // Open picker
  67. cy.get(pickerSelector).click()
  68. // Return selected colour
  69. return cy.get(pickerSelector).get('.color-picker__simple-color-circle').eq(randColour).then(($el) => {
  70. $el.trigger('click')
  71. return $el.css('background-color')
  72. })
  73. }