From 541be86237bca02eb3a7c863732f3649919b6b38 Mon Sep 17 00:00:00 2001 From: Tobias Bieniek Date: Sun, 2 Feb 2020 14:51:44 +0100 Subject: [PATCH] hasStyle: Throw an error if expectation object is empty --- lib/__tests__/does-not-have-style.ts | 6 ++++++ lib/__tests__/has-style.ts | 6 ++++++ lib/assertions.ts | 12 ++++++++++++ 3 files changed, 24 insertions(+) diff --git a/lib/__tests__/does-not-have-style.ts b/lib/__tests__/does-not-have-style.ts index d9001829d..1391f2e8f 100644 --- a/lib/__tests__/does-not-have-style.ts +++ b/lib/__tests__/does-not-have-style.ts @@ -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.' + ); + }); }); diff --git a/lib/__tests__/has-style.ts b/lib/__tests__/has-style.ts index 53591aace..d068ef9bd 100644 --- a/lib/__tests__/has-style.ts +++ b/lib/__tests__/has-style.ts @@ -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.' + ); + }); }); diff --git a/lib/assertions.ts b/lib/assertions.ts index 911a42245..36370040c 100644 --- a/lib/assertions.ts +++ b/lib/assertions.ts @@ -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] ); @@ -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] );