Skip to content

Commit

Permalink
hasStyle: Throw an error if expectation object is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Turbo87 committed Feb 2, 2020
1 parent 2bdb294 commit 541be86
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/__tests__/does-not-have-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,10 @@ describe('assert.dom(...).doesNotHaveStyle()', () => {
'Unexpected Parameter: [object Document]'
);
});

test('throws for empty expectation object', () => {
expect(() => assert.dom('div').doesNotHaveStyle({})).toThrow(
'Missing style expectations. There must be at least one style property in the passed in expectation object.'
);
});
});
6 changes: 6 additions & 0 deletions lib/__tests__/has-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,10 @@ describe('assert.dom(...).hasStyle()', () => {
'Unexpected Parameter: [object Document]'
);
});

test('throws for empty expectation object', () => {
expect(() => assert.dom('div').hasStyle({})).toThrow(
'Missing style expectations. There must be at least one style property in the passed in expectation object.'
);
});
});
12 changes: 12 additions & 0 deletions lib/assertions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@ export default class DOMAssertions {

let computedStyle = window.getComputedStyle(element, selector);
let expectedProperties = Object.keys(expected) as [keyof CSSStyleDeclaration];
if (expectedProperties.length <= 0) {
throw new TypeError(
`Missing style expectations. There must be at least one style property in the passed in expectation object.`
);
}

let result = expectedProperties.every(
property => computedStyle[property] === expected[property]
);
Expand Down Expand Up @@ -666,6 +672,12 @@ export default class DOMAssertions {
let computedStyle = window.getComputedStyle(element, selector);

let expectedProperties = Object.keys(expected) as [keyof CSSStyleDeclaration];
if (expectedProperties.length <= 0) {
throw new TypeError(
`Missing style expectations. There must be at least one style property in the passed in expectation object.`
);
}

let result = expectedProperties.some(
property => computedStyle[property] !== expected[property]
);
Expand Down

0 comments on commit 541be86

Please sign in to comment.