diff --git a/packages/dom/src/lib/ElementAssertion.ts b/packages/dom/src/lib/ElementAssertion.ts index c4fc8a6..e89aa48 100644 --- a/packages/dom/src/lib/ElementAssertion.ts +++ b/packages/dom/src/lib/ElementAssertion.ts @@ -1,7 +1,7 @@ import { Assertion, AssertionError } from "@assertive-ts/core"; import equal from "fast-deep-equal"; -import { getReceivedStyle, normalizeStyles } from "./helpers/helpers"; +import { getExpectedAndReceivedStyles } from "./helpers/helpers"; export class ElementAssertion extends Assertion { @@ -158,20 +158,12 @@ export class ElementAssertion extends Assertion { */ public toHaveStyle(expected: Partial): this { - if (!this.actual.ownerDocument.defaultView) { - throw new Error("The element is not attached to a document with a default view."); - } - if (!(this.actual instanceof HTMLElement)) { - throw new Error("The element is not an HTMLElement."); - } - - const window = this.actual.ownerDocument.defaultView; - const received = window.getComputedStyle(this.actual); + const [expectedStyle, receivedStyle] = getExpectedAndReceivedStyles(this.actual, expected); - const { props, expectedStyle } = normalizeStyles(expected); - - const receivedStyle = getReceivedStyle(props, received); + if (!expectedStyle || !receivedStyle) { + throw new Error("Currently there are no available styles."); + } const error = new AssertionError({ actual: this.actual, @@ -190,6 +182,50 @@ export class ElementAssertion extends Assertion { }); } + /** + * Asserts that the element has one or more of the specified CSS styles. + * + * @example + * ``` + * expect(component).toHaveSomeStyle({ color: 'green', display: 'block' }); + * ``` + * + * @param expected the expected CSS style/s. + * @returns the assertion instance. + */ + + public toHaveSomeStyle(expected: Partial): this { + + const [expectedStyle, elementProcessedStyle] = getExpectedAndReceivedStyles(this.actual, expected); + + if (!expectedStyle || !elementProcessedStyle) { + throw new Error("No available styles."); + } + + const hasSomeStyle = Object.entries(expectedStyle).some(([expectedProp, expectedValue]) => { + return Object.entries(elementProcessedStyle).some(([receivedProp, receivedValue]) => { + return equal(expectedProp, receivedProp) && equal(expectedValue, receivedValue); + }); + }); + + const error = new AssertionError({ + actual: this.actual, + message: `Expected the element to match some of the following styles:\n${JSON.stringify(expectedStyle, null, 2)}`, + }); + + const invertedError = new AssertionError({ + actual: this.actual, + // eslint-disable-next-line max-len + message: `Expected the element NOT to match some of the following styles:\n${JSON.stringify(expectedStyle, null, 2)}`, + }); + + return this.execute({ + assertWhen: hasSomeStyle, + error, + invertedError, + }); + } + /** * Helper method to assert the presence or absence of class names. * diff --git a/packages/dom/src/lib/helpers/helpers.ts b/packages/dom/src/lib/helpers/helpers.ts index 843be7d..197cfd0 100644 --- a/packages/dom/src/lib/helpers/helpers.ts +++ b/packages/dom/src/lib/helpers/helpers.ts @@ -13,7 +13,7 @@ interface StyleDeclaration extends Record { value: string; } -export const normalizeStyles = (css: Partial): +const normalizeStyles = (css: Partial): { expectedStyle: StyleDeclaration; props: string[]; } => { const normalizer = document.createElement("div"); document.body.appendChild(normalizer); @@ -48,9 +48,36 @@ export const normalizeStyles = (css: Partial): return { expectedStyle, props }; }; -export const getReceivedStyle = (props: string[], received: CSSStyleDeclaration): StyleDeclaration => { +const getReceivedStyle = (props: string[], received: CSSStyleDeclaration): StyleDeclaration => { return props.reduce((acc, prop) => { - acc[prop] = received?.getPropertyValue(prop).trim(); + const actualStyle = received.getPropertyValue(prop).trim(); + if (!actualStyle) { + return acc; + } + acc[prop] = actualStyle; return acc; }, {} as StyleDeclaration); }; + +export const getExpectedAndReceivedStyles = +(actual: Element, expected: Partial): StyleDeclaration[] => { + if (!actual.ownerDocument.defaultView) { + throw new Error("The element is not attached to a document with a default view."); + } + if (!(actual instanceof HTMLElement)) { + throw new Error("The element is not an HTMLElement."); + } + + const window = actual.ownerDocument.defaultView; + + const rawElementStyles = window.getComputedStyle(actual); + + const { props, expectedStyle } = normalizeStyles(expected); + + const elementProcessedStyle = getReceivedStyle(props, rawElementStyles); + + return [ + expectedStyle, + elementProcessedStyle, + ]; +}; diff --git a/packages/dom/test/unit/lib/ElementAssertion.test.tsx b/packages/dom/test/unit/lib/ElementAssertion.test.tsx index f91f10c..3719659 100644 --- a/packages/dom/test/unit/lib/ElementAssertion.test.tsx +++ b/packages/dom/test/unit/lib/ElementAssertion.test.tsx @@ -337,4 +337,47 @@ describe("[Unit] ElementAssertion.test.ts", () => { }); }); }); + + describe(".toHaveSomeStyle", () => { + context("when the element contains one or more expected styles", () => { + it("returns the assertion instance", () => { + const { getByTestId } = render( +
, + ); + const divTest = getByTestId("test-div"); + const test = new ElementAssertion(divTest); + + expect(test.toHaveSomeStyle({ color: "red", display: "flex", height: "3rem", width: "2rem" })).toBeEqual(test); + + expect(() => test.not.toHaveSomeStyle({ color: "blue" })) + .toThrowError(AssertionError) + // eslint-disable-next-line max-len + .toHaveMessage("Expected the element NOT to match some of the following styles:\n{\n \"color\": \"rgb(0, 0, 255)\"\n}"); + }); + }); + + context("when the element does not contain any of the expected styles", () => { + it("throws an assertion error", () => { + const { getByTestId } = render( +
, + ); + const divTest = getByTestId("test-div"); + const test = new ElementAssertion(divTest); + + expect(() => test.toHaveSomeStyle({ color: "red", display: "flex" })) + .toThrowError(AssertionError) + // eslint-disable-next-line max-len + .toHaveMessage("Expected the element to match some of the following styles:\n{\n \"color\": \"rgb(255, 0, 0)\",\n \"display\": \"flex\"\n}"); + + expect(test.not.toHaveSomeStyle({ border: "1px solid blue", color: "red", display: "flex" })).toBeEqual(test); + }); + }); + }); });