Skip to content
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

Use getProperty for hasStyle assertions so testing can be done against css variables #1976

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion packages/qunit-dom/lib/__tests__/has-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ describe('assert.dom(...).hasStyle()', () => {
beforeEach(() => {
assert = new TestAssertions();
document.body.innerHTML =
'<div class="foo" style="opacity: 1; width: 200px; text-align: center;">quit-dom ftw!</div>';
'<div class="foo" style="opacity: 1; width: 200px; text-align: center; --color-text: #FFF;">quit-dom ftw!</div>';

//@ts-ignore
document.querySelector('.foo').style.setProperty('--color-background', '#333');
});

test('succeeds for correct content', () => {
Expand Down Expand Up @@ -63,6 +66,22 @@ describe('assert.dom(...).hasStyle()', () => {
]);
});

test('succeeds for custom property', () => {
assert.dom('.foo').hasStyle({
'--color-text': '#FFF',
'--color-background': '#333',
});

expect(assert.results).toEqual([
{
actual: { '--color-text': '#FFF', '--color-background': '#333' },
expected: { '--color-text': '#FFF', '--color-background': '#333' },
message: 'Element .foo has style "{"--color-text":"#FFF","--color-background":"#333"}"',
result: true,
},
]);
});

test('fails for wrong content', () => {
assert.dom('.foo').hasStyle({
opacity: 0,
Expand Down
6 changes: 4 additions & 2 deletions packages/qunit-dom/lib/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,11 +774,13 @@ export default class DOMAssertions {
}

let result = expectedProperties.every(
property => computedStyle[property] === expected[property]
property => computedStyle.getPropertyValue(property.toString()) === expected[property]
);
let actual: ActualCSSStyleDeclaration = {};

expectedProperties.forEach(property => (actual[property] = computedStyle[property]));
expectedProperties.forEach(
property => (actual[property] = computedStyle.getPropertyValue(property.toString()))
);

if (!message) {
let normalizedSelector = selector ? selector.replace(/^:{0,2}/, '::') : '';
Expand Down
Loading