|
1 | | -import React from "react"; |
| 1 | +import React, { useLayoutEffect, useState } from "react"; |
2 | 2 | import TestRenderer from "react-test-renderer"; |
3 | 3 | import assert from "node:assert/strict"; |
4 | | -import { assertComponents } from "../index.mjs"; |
| 4 | +import { actAsync, assertComponents, mockComponent } from "../index.mjs"; |
5 | 5 | import { TestComp, TestComp2 } from "./testComponents.mjs"; |
6 | 6 |
|
7 | 7 | const { describe, it } = await (async () => { |
@@ -63,4 +63,52 @@ describe("assertComponents.test.mjs", () => { |
63 | 63 | "\n\texpected: TestComp2" |
64 | 64 | ); |
65 | 65 | }); |
| 66 | + |
| 67 | + it("should not fail if components match", () => { |
| 68 | + //given |
| 69 | + const Comp1 = mockComponent(TestComp); |
| 70 | + const Comp2 = mockComponent(TestComp2); |
| 71 | + const Comp = () => { |
| 72 | + return h(Comp1, {}, h("div"), h(Comp2)); |
| 73 | + }; |
| 74 | + const comp = TestRenderer.create(h(Comp)).root; |
| 75 | + /** @type {Error?} */ |
| 76 | + let resError = null; |
| 77 | + |
| 78 | + //when |
| 79 | + try { |
| 80 | + assertComponents(comp.children, h(Comp1, {}, h("div"), h(Comp2))); |
| 81 | + } catch (error) { |
| 82 | + resError = error; |
| 83 | + } |
| 84 | + |
| 85 | + //then |
| 86 | + assert.deepEqual(resError, null); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not fail if components match async", async () => { |
| 90 | + //given |
| 91 | + const Comp1 = mockComponent(TestComp); |
| 92 | + const Comp2 = mockComponent(TestComp2); |
| 93 | + const Comp = () => { |
| 94 | + const [_, setState] = useState(0); |
| 95 | + useLayoutEffect(() => { |
| 96 | + Promise.resolve().then(() => { |
| 97 | + setState(123); |
| 98 | + }); |
| 99 | + }, []); |
| 100 | + |
| 101 | + return h(Comp1, {}, h("div"), h(Comp2)); |
| 102 | + }; |
| 103 | + |
| 104 | + //when |
| 105 | + const result = ( |
| 106 | + await actAsync(() => { |
| 107 | + return TestRenderer.create(h(Comp)); |
| 108 | + }) |
| 109 | + ).root; |
| 110 | + |
| 111 | + //then |
| 112 | + assertComponents(result.children, h(Comp1, {}, h("div"), h(Comp2))); |
| 113 | + }); |
66 | 114 | }); |
0 commit comments