-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: update themed to handle pseudoelements and special underline styling case #9450
Merged
jcfranco
merged 6 commits into
epic/7180-component-tokens
from
jcfranco/update-theme-to-handle-pseudoelements-and-special-underline-case
Jun 4, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
65b17dd
test: update themed to handle pseudoelements and special underline st…
jcfranco f586df9
update example
jcfranco 0ed89cb
Merge branch 'epic/7180-component-tokens' into jcfranco/update-theme-…
jcfranco 5ec3cad
Merge branch 'epic/7180-component-tokens' into jcfranco/update-theme-…
jcfranco dab5f80
add support for CSS2.1 pseudoelement syntax
jcfranco 04939ad
update action test for coverage
jcfranco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,14 @@ import { getTagAndPage } from "./utils"; | |
|
||
expect.extend(toHaveNoViolations); | ||
|
||
interface TargetInfo { | ||
el: E2EElement; | ||
selector: string; | ||
shadowSelector: string; | ||
} | ||
|
||
const pseudoElementPattern = /:{1,2}(before|after)/; | ||
|
||
/** | ||
* This object that represents component tokens and their respective test options. | ||
*/ | ||
|
@@ -17,43 +25,48 @@ export type ComponentTestTokens = Record<CalciteCSSCustomProp, TestSelectToken | | |
* Helper to test custom theming of a component's associated tokens. | ||
* | ||
* @example | ||
// describe("theme", () => { | ||
// const tokens: ComponentTestTokens = { | ||
// "--calcite-action-menu-border-color": [ | ||
// { | ||
// targetProp: "borderLeftColor", | ||
// }, | ||
// { | ||
// shadowSelector: "calcite-action", | ||
// targetProp: "--calcite-action-border-color", | ||
// }, | ||
// ], | ||
// "--calcite-action-menu-background-color": { | ||
// targetProp: "backgroundColor", | ||
// shadowSelector: ".container", | ||
// }, | ||
// "--calcite-action-menu-trigger-background-color-active": { | ||
// shadowSelector: "calcite-action", | ||
// targetProp: "--calcite-action-background-color", | ||
// state: { press: { attribute: "class", value: CSS.defaultTrigger } }, | ||
// }, | ||
// "--calcite-action-menu-trigger-background-color-focus": { | ||
// shadowSelector: "calcite-action", | ||
// targetProp: "--calcite-action-background-color", | ||
// state: "focus", | ||
// }, | ||
// "--calcite-action-menu-trigger-background-color-hover": { | ||
// shadowSelector: "calcite-action", | ||
// targetProp: "--calcite-action-background-color", | ||
// state: "hover", | ||
// }, | ||
// "--calcite-action-menu-trigger-background-color": { | ||
// shadowSelector: "calcite-action", | ||
// targetProp: "--calcite-action-background-color", | ||
// }, | ||
// }; | ||
// themed(`calcite-action-bar`, tokens); | ||
// }); | ||
* describe("theme", () => { | ||
* const tokens: ComponentTestTokens = { | ||
* "--calcite-action-menu-border-color": [ | ||
* { | ||
* targetProp: "borderLeftColor", | ||
* }, | ||
* { | ||
* shadowSelector: "calcite-action", | ||
* targetProp: "--calcite-action-border-color", | ||
* }, | ||
* { | ||
* // added to demonstrate pseudo-element support | ||
* shadowSelector: "calcite-action::after", | ||
* targetProp: "borderColor", | ||
* }, | ||
* ], | ||
* "--calcite-action-menu-background-color": { | ||
* targetProp: "backgroundColor", | ||
* shadowSelector: ".container", | ||
* }, | ||
* "--calcite-action-menu-trigger-background-color-active": { | ||
* shadowSelector: "calcite-action", | ||
* targetProp: "--calcite-action-background-color", | ||
* state: { press: { attribute: "class", value: CSS.defaultTrigger } }, | ||
* }, | ||
* "--calcite-action-menu-trigger-background-color-focus": { | ||
* shadowSelector: "calcite-action", | ||
* targetProp: "--calcite-action-background-color", | ||
* state: "focus", | ||
* }, | ||
* "--calcite-action-menu-trigger-background-color-hover": { | ||
* shadowSelector: "calcite-action", | ||
* targetProp: "--calcite-action-background-color", | ||
* state: "hover", | ||
* }, | ||
* "--calcite-action-menu-trigger-background-color": { | ||
* shadowSelector: "calcite-action", | ||
* targetProp: "--calcite-action-background-color", | ||
* }, | ||
* }; | ||
* themed(`calcite-action-bar`, tokens); | ||
* }); | ||
* | ||
* @param componentTestSetup - A component tag, html, tag + e2e page or provider for setting up a test. | ||
* @param tokens - A record of token names and their associated selectors, shadow selectors, target props, and states. | ||
|
@@ -100,7 +113,7 @@ export function themed(componentTestSetup: ComponentTestSetup, tokens: Component | |
|
||
const el = await page.find(selector); | ||
const tokenStyle = `${token}: ${setTokens[token]}`; | ||
let target = el; | ||
const target: TargetInfo = { el, selector, shadowSelector }; | ||
let contextSelector: ContextSelectByAttr; | ||
let stateName: State; | ||
|
||
|
@@ -115,13 +128,14 @@ export function themed(componentTestSetup: ComponentTestSetup, tokens: Component | |
styleTargets[selector][1].push(tokenStyle); | ||
} | ||
if (shadowSelector) { | ||
target = shadowSelector ? await page.find(`${selector} >>> ${shadowSelector}`) : target; | ||
const effectiveShadowSelector = shadowSelector.replace(pseudoElementPattern, ""); | ||
target.el = await page.find(`${selector} >>> ${effectiveShadowSelector}`); | ||
} | ||
if (state && typeof state !== "string") { | ||
contextSelector = Object.values(state)[0] as ContextSelectByAttr; | ||
} | ||
|
||
if (!target) { | ||
if (!target.el) { | ||
throw new Error( | ||
`[${token}] target (${selector}${ | ||
shadowSelector ? " >>> " + shadowSelector : "" | ||
|
@@ -168,9 +182,9 @@ type State = "press" | "hover" | "focus"; | |
*/ | ||
export type TestTarget = { | ||
/** | ||
* The element to get the computed style from. | ||
* An object with target element and selector info. | ||
*/ | ||
target: E2EElement; | ||
target: TargetInfo; | ||
|
||
/** | ||
* @todo doc | ||
|
@@ -242,15 +256,21 @@ export type TestSelectToken = { | |
* | ||
* @param element | ||
* @param property | ||
* @param pseudoElement | ||
*/ | ||
async function getComputedStylePropertyValue(element: E2EElement, property: string): Promise<string> { | ||
async function getComputedStylePropertyValue( | ||
element: E2EElement, | ||
property: string, | ||
pseudoElement?: string, | ||
): Promise<string> { | ||
type E2EElementInternal = E2EElement & { | ||
_elmHandle: ElementHandle; | ||
}; | ||
|
||
return await (element as E2EElementInternal)._elmHandle.evaluate( | ||
(el, targetProp): string => window.getComputedStyle(el).getPropertyValue(targetProp), | ||
(el, targetProp, pseudoElement): string => window.getComputedStyle(el, pseudoElement).getPropertyValue(targetProp), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💪 |
||
property, | ||
pseudoElement, | ||
); | ||
} | ||
|
||
|
@@ -272,6 +292,9 @@ async function assertThemedProps(page: E2EPage, options: TestTarget): Promise<vo | |
await page.mouse.reset(); | ||
await page.waitForChanges(); | ||
|
||
const targetEl = target.el; | ||
const pseudoElement = target.shadowSelector?.match(pseudoElementPattern)?.[0] ?? undefined; | ||
|
||
if (contextSelector) { | ||
const rect = (await page.evaluate((context: TestTarget["contextSelector"]) => { | ||
const searchInShadowDom = (node: Node): HTMLElement | SVGElement | Node | undefined => { | ||
|
@@ -344,20 +367,21 @@ async function assertThemedProps(page: E2EPage, options: TestTarget): Promise<vo | |
await page.mouse.up(); | ||
} | ||
} else if (state) { | ||
await target[state as Exclude<State, "press">](); | ||
await targetEl[state as Exclude<State, "press">](); | ||
} | ||
|
||
await page.waitForChanges(); | ||
|
||
if (targetProp.startsWith("--calcite-")) { | ||
expect(await getComputedStylePropertyValue(target, targetProp)).toBe(expectedValue); | ||
expect(await getComputedStylePropertyValue(targetEl, targetProp, pseudoElement)).toBe(expectedValue); | ||
return; | ||
} | ||
|
||
const styles = await target.getComputedStyle(); | ||
const styles = await targetEl.getComputedStyle(pseudoElement); | ||
const isFakeBorderToken = token.includes("border-color") && targetProp === "boxShadow"; | ||
const isLinearGradientUnderlineToken = token.includes("link-underline-color") && targetProp === "backgroundImage"; | ||
|
||
if (isFakeBorderToken) { | ||
if (isFakeBorderToken || isLinearGradientUnderlineToken) { | ||
expect(styles[targetProp]).toMatch(expectedValue); | ||
return; | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested this with alert, action, and menu-item. Action passed but when I tested alert and menu-item it only passed when I put a debugger on line 125 and then immediately continued to play the test.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a separate issue since all updates in this PR are synchronous or update existing async code.
I noticed the PR was not synced up with
epic/7180-component-tokens
, which I have taken care of. Would you mind testing again?