Skip to content

Commit 541be86

Browse files
committed
hasStyle: Throw an error if expectation object is empty
1 parent 2bdb294 commit 541be86

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

lib/__tests__/does-not-have-style.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,10 @@ describe('assert.dom(...).doesNotHaveStyle()', () => {
109109
'Unexpected Parameter: [object Document]'
110110
);
111111
});
112+
113+
test('throws for empty expectation object', () => {
114+
expect(() => assert.dom('div').doesNotHaveStyle({})).toThrow(
115+
'Missing style expectations. There must be at least one style property in the passed in expectation object.'
116+
);
117+
});
112118
});

lib/__tests__/has-style.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,10 @@ describe('assert.dom(...).hasStyle()', () => {
106106
'Unexpected Parameter: [object Document]'
107107
);
108108
});
109+
110+
test('throws for empty expectation object', () => {
111+
expect(() => assert.dom('div').hasStyle({})).toThrow(
112+
'Missing style expectations. There must be at least one style property in the passed in expectation object.'
113+
);
114+
});
109115
});

lib/assertions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,12 @@ export default class DOMAssertions {
600600

601601
let computedStyle = window.getComputedStyle(element, selector);
602602
let expectedProperties = Object.keys(expected) as [keyof CSSStyleDeclaration];
603+
if (expectedProperties.length <= 0) {
604+
throw new TypeError(
605+
`Missing style expectations. There must be at least one style property in the passed in expectation object.`
606+
);
607+
}
608+
603609
let result = expectedProperties.every(
604610
property => computedStyle[property] === expected[property]
605611
);
@@ -666,6 +672,12 @@ export default class DOMAssertions {
666672
let computedStyle = window.getComputedStyle(element, selector);
667673

668674
let expectedProperties = Object.keys(expected) as [keyof CSSStyleDeclaration];
675+
if (expectedProperties.length <= 0) {
676+
throw new TypeError(
677+
`Missing style expectations. There must be at least one style property in the passed in expectation object.`
678+
);
679+
}
680+
669681
let result = expectedProperties.some(
670682
property => computedStyle[property] !== expected[property]
671683
);

0 commit comments

Comments
 (0)