1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
/**
* @copyright Copyright (c) 2022 John Molakvoæ <skjnldsv@protonmail.com>
*
* @author John Molakvoæ <skjnldsv@protonmail.com>
*
* @license AGPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import { colord } from 'colord'
export const defaultPrimary = '#00679e'
export const defaultBackground = 'kamil-porembinski-clouds.jpg'
/**
* Check if a CSS variable is set to a specific color
* @param variable Variable to check
* @param expectedColor Color that is expected
*/
export function validateCSSVariable(variable: string, expectedColor: string) {
const value = window.getComputedStyle(Cypress.$('body').get(0)).getPropertyValue(variable)
console.debug(`${variable}, is: ${colord(value).toHex()} expected: ${expectedColor}`)
return colord(value).isEqual(expectedColor)
}
/**
* Validate the current page body css variables
*
* @param {string} expectedColor the expected primary color
* @param {string|null} expectedBackground the expected background
* @param {string|null} expectedBackgroundColor the expected background color (null to ignore)
*/
export function validateBodyThemingCss(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground, expectedBackgroundColor: string|null = defaultPrimary) {
// We must use `Cypress.$` here as any assertions (get is an assertion) is not allowed in wait-until's check function, see documentation
const guestBackgroundColor = Cypress.$('body').css('background-color')
const guestBackgroundImage = Cypress.$('body').css('background-image')
const isValidBackgroundColor = expectedBackgroundColor === null || colord(guestBackgroundColor).isEqual(expectedBackgroundColor)
const isValidBackgroundImage = !expectedBackground
? guestBackgroundImage === 'none'
: guestBackgroundImage.includes(expectedBackground)
console.debug({
isValidBackgroundColor,
isValidBackgroundImage,
guestBackgroundColor: colord(guestBackgroundColor).toHex(),
guestBackgroundImage,
})
return isValidBackgroundColor && isValidBackgroundImage && validateCSSVariable('--color-primary', expectedColor)
}
/**
* Check background color of element
* @param element JQuery element to check
* @param color expected color
*/
export function expectBackgroundColor(element: JQuery<HTMLElement>, color: string) {
expect(colord(element.css('background-color')).toHex()).equal(colord(color).toHex())
}
/**
* Validate the user theming default select option css
*
* @param {string} expectedColor the expected color
* @param {string} expectedBackground the expected background
*/
export const validateUserThemingDefaultCss = function(expectedColor = defaultPrimary, expectedBackground: string|null = defaultBackground) {
const defaultSelectButton = Cypress.$('[data-user-theming-background-default]')
if (defaultSelectButton.length === 0) {
return false
}
const backgroundImage = defaultSelectButton.css('background-image')
const backgroundColor = defaultSelectButton.css('background-color')
const isValidBackgroundImage = !expectedBackground
? (backgroundImage === 'none' || Cypress.$('body').css('background-image') === 'none')
: backgroundImage.includes(expectedBackground)
console.debug({
colorPickerOptionColor: colord(backgroundColor).toHex(),
expectedColor,
isValidBackgroundImage,
backgroundImage,
})
return isValidBackgroundImage && colord(backgroundColor).isEqual(expectedColor)
}
export const pickRandomColor = function(context: string, index?: number): Cypress.Chainable<string> {
// Pick one of the first 8 options
const randColour = index ?? Math.floor(Math.random() * 8)
const colorPreviewSelector = `${context} [data-admin-theming-setting-color]`
let oldColor = ''
cy.get(colorPreviewSelector).then(($el) => {
oldColor = $el.css('background-color')
})
// Open picker
cy.get(`${context} [data-admin-theming-setting-color-picker]`).scrollIntoView()
cy.get(`${context} [data-admin-theming-setting-color-picker]`).click({ force: true })
// Click on random color
cy.get('.color-picker__simple-color-circle').eq(randColour).click()
// Wait for color change
cy.waitUntil(() => Cypress.$(colorPreviewSelector).css('background-color') !== oldColor)
// Get the selected color from the color preview block
return cy.get(colorPreviewSelector).then(($el) => $el.css('background-color'))
}
|