-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoMatchJSX.tsx
More file actions
46 lines (40 loc) · 1.05 KB
/
toMatchJSX.tsx
File metadata and controls
46 lines (40 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import React from "react";
import { expect } from "@jest/globals";
import { render } from "@testing-library/react";
function isReactJSXElement(received: unknown): received is React.ReactElement {
return (
typeof received === "object" &&
received !== null &&
"$$typeof" in received &&
received.$$typeof === Symbol.for("react.element")
);
}
export function toMatchJSX(received: unknown, expected: unknown) {
if (false === isReactJSXElement(received)) {
return {
pass: false,
message: () => "Expected a JSX element",
};
}
if (false === isReactJSXElement(expected)) {
return {
pass: false,
message: () => "Expected a JSX element",
};
}
expect(render(received).asFragment().firstChild).toEqual(
render(expected).asFragment().firstChild,
);
return {
pass: true,
message: () => "loo",
};
}
declare module "expect" {
interface AsymmetricMatchers {
toMatchJSX(expected: React.JSX.Element): void;
}
interface Matchers<R> {
toMatchJSX(expected: React.JSX.Element): R;
}
}