Skip to content

Commit 53f1d32

Browse files
committed
Add: test for partial match
1 parent 33320b6 commit 53f1d32

File tree

2 files changed

+65
-46
lines changed

2 files changed

+65
-46
lines changed

packages/dom/src/lib/ElementAssertion.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,13 @@ export class ElementAssertion<T extends Element> extends Assertion<T> {
148148
/**
149149
* Asserts that the element has the specified CSS styles.
150150
*
151-
* @param expected - The expected CSS styles.
152-
* @returns The assertion instance.
151+
* @example
152+
* ```
153+
* expect(component).toHaveStyle({ color: 'green', display: 'block' });
154+
* ```
155+
*
156+
* @param expected the expected CSS styles.
157+
* @returns the assertion instance.
153158
*/
154159

155160
public toHaveStyle(expected: Partial<CSSStyleDeclaration>): this {

packages/dom/test/unit/lib/ElementAssertion.test.tsx

Lines changed: 58 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -259,68 +259,82 @@ describe("[Unit] ElementAssertion.test.ts", () => {
259259
expect(() => test.toHaveAllClasses("foo", "bar", "baz"))
260260
.toThrowError(AssertionError)
261261
.toHaveMessage('Expected the element to have all of these classes: "foo bar baz"');
262-
262+
263263
expect(test.not.toHaveAllClasses("foo", "bar", "baz")).toBeEqual(test);
264264
});
265265
});
266266
});
267-
267+
268268
describe(".toHaveStyle", () => {
269-
context("when the style is passed as a string", () => {
270-
context("and the element has the expected style", () => {
271-
it("returns the assertion instance", () => {
272-
const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />);
273-
const divTest = getByTestId("test-div");
274-
const test = new ElementAssertion(divTest);
275-
276-
expect(test.toHaveStyle("display: flex; color: red; border: 1px solid black")).toBeEqual(test);
277-
278-
expect(() => test.not.toHaveStyle("display: flex; color: red; border: 1px solid black"))
279-
.toThrowError(AssertionError)
280-
.toHaveMessage('Expected the element to NOT have {"display":"flex","color":"rgb(255, 0, 0)","border":"1px solid black"} style');
281-
});
269+
context("when the element has the expected style", () => {
270+
it("returns the assertion instance", () => {
271+
const { getByTestId } = render(
272+
<div
273+
className="foo bar test"
274+
style={{ border: "1px solid black", color: "red", display: "flex" }}
275+
data-testid="test-div"
276+
/>);
277+
const divTest = getByTestId("test-div");
278+
const test = new ElementAssertion(divTest);
279+
280+
expect(test.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
281+
282+
expect(() => test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" }))
283+
.toThrowError(AssertionError)
284+
.toHaveMessage(
285+
// eslint-disable-next-line max-len
286+
'Expected the element to NOT match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
287+
);
282288
});
289+
});
283290

284-
context("and the element does not have the expected style", () => {
291+
context("when the element does not have the expected style", () => {
285292
it("throws an assertion error", () => {
286-
const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red" }} data-testid="test-div" />);
293+
const { getByTestId } = render(
294+
<div
295+
className="foo bar test"
296+
style={{ color: "blue", display: "block" }}
297+
data-testid="test-div"
298+
/>,
299+
);
300+
287301
const divTest = getByTestId("test-div");
288302
const test = new ElementAssertion(divTest);
289-
290-
expect(test.not.toHaveStyle("color: red; display: flex; border: 1px solid black;")).toBeEqual(test);
303+
304+
expect(() => test.toHaveStyle(({ border: "1px solid black", color: "red", display: "flex" })))
305+
.toThrowError(AssertionError)
306+
.toHaveMessage(
307+
// eslint-disable-next-line max-len
308+
'Expected the element to match the following style:\n{\n "border": "1px solid black",\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
309+
);
310+
311+
expect(test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
291312

292313
});
293-
});
294314
});
315+
context("when the element partially match the style", () => {
316+
it("throws an assertion error", () => {
317+
const { getByTestId } = render(
318+
<div
319+
className="foo bar test"
320+
style={{ border: "1px solid black", color: "blue", display: "block" }}
321+
data-testid="test-div"
322+
/>,
323+
);
295324

296-
context("when the style is passed as an object", () => {
297-
context("and the element has the expected style", () => {
298-
it("returns the assertion instance", () => {
299-
const { getByTestId } = render(<div className="foo bar test" style={{ display: "flex", color: "red", border: "1px solid black" }} data-testid="test-div" />);
300325
const divTest = getByTestId("test-div");
301326
const test = new ElementAssertion(divTest);
302-
303-
expect(test.toHaveStyle({color: "red", display: "flex", border: "1px solid black"})).toBeEqual(test);
304327

305-
expect(() => test.not.toHaveStyle({color: "red", display: "flex", border: "1px solid black"}))
328+
expect(() => test.toHaveStyle(({ color: "red", display: "flex" })))
306329
.toThrowError(AssertionError)
307-
.toHaveMessage('Expected the element to NOT have {"color":"rgb(255, 0, 0)","display":"flex","border":"1px solid black"} style');
308-
309-
});
310-
});
330+
.toHaveMessage(
331+
// eslint-disable-next-line max-len
332+
'Expected the element to match the following style:\n{\n "color": "rgb(255, 0, 0)",\n "display": "flex"\n}',
333+
);
334+
335+
expect(test.not.toHaveStyle({ border: "1px solid black", color: "red", display: "flex" })).toBeEqual(test);
311336

312-
context("and the element does not have the expected style", () => {
313-
it("throws an assertion error", () => {
314-
const { getByTestId } = render(<div className="foo bar test" style={{ display: "block", color: "blue" }} data-testid="test-div" />);
315-
const divTest = getByTestId("test-div");
316-
const test = new ElementAssertion(divTest);
317-
318-
expect(() => test.toHaveStyle(({ color: "red", display: "flex", border: "1px solid black" })))
319-
.toThrowError(AssertionError)
320-
.toHaveMessage("Expected the element to have {\"color\":\"rgb(255, 0, 0)\",\"display\":\"flex\",\"border\":\"1px solid black\"} style");
321-
322-
});
323337
});
324-
});
338+
});
325339
});
326-
})
340+
});

0 commit comments

Comments
 (0)