Skip to content

Commit

Permalink
Use getProperty for hasStyle assertions so testing can be done agains…
Browse files Browse the repository at this point in the history
…t css variables
  • Loading branch information
jkeen committed Apr 8, 2024
1 parent 8eee131 commit 749f65f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 20 additions & 1 deletion packages/qunit-dom/lib/__tests__/has-style.ts
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
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

0 comments on commit 749f65f

Please sign in to comment.