-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
441209b
commit 98f74dd
Showing
6 changed files
with
116 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as assertComponent } from "./src/assertComponent.mjs"; | ||
export { default as assertComponents } from "./src/assertComponents.mjs"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import assert from "node:assert/strict"; | ||
import assertComponent from "./assertComponent.mjs"; | ||
|
||
/** | ||
* @param { (import('react-test-renderer').ReactTestInstance | string)[] } results | ||
* @param { (import('react').ReactElement | string)[] } expectedElements | ||
*/ | ||
function assertComponents(results, ...expectedElements) { | ||
assert.deepEqual( | ||
results.length, | ||
expectedElements.length, | ||
`Components count doesn't match` + | ||
`\n\tactual: ${results.length}` + | ||
`\n\texpected: ${expectedElements.length}` | ||
); | ||
|
||
expectedElements.forEach((expected, i) => { | ||
assertComponent(results[i], expected); | ||
}); | ||
} | ||
|
||
export default assertComponents; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
await import("./testRenderer.test.mjs"); | ||
await import("./assertComponent.test.mjs"); | ||
await import("./assertComponents.test.mjs"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import React from "react"; | ||
import TestRenderer from "react-test-renderer"; | ||
import { assertComponents } from "../index.mjs"; | ||
import { TestComp, TestComp2 } from "./testComponents.mjs"; | ||
|
||
import { strict as assert } from "node:assert"; | ||
const { describe, it } = await (async () => { | ||
// @ts-ignore | ||
return process.isBun // @ts-ignore | ||
? Promise.resolve({ describe: (_, fn) => fn(), it: test }) | ||
: import("node:test"); | ||
})(); | ||
|
||
const h = React.createElement; | ||
|
||
describe("assertComponents.test.mjs", () => { | ||
it("should fail if components count doesn't match", () => { | ||
//given | ||
const Comp = () => { | ||
return h(React.Fragment, null, "test_text", h("div"), h(TestComp)); | ||
}; | ||
const comp = TestRenderer.create(h(Comp)).root; | ||
/** @type {Error?} */ | ||
let resError = null; | ||
|
||
//when | ||
try { | ||
assertComponents(comp.children, h("div"), "test_text"); | ||
} catch (error) { | ||
resError = error; | ||
} | ||
|
||
//then | ||
assert.deepEqual( | ||
resError?.message, | ||
"Components count doesn't match\n\tactual: 3\n\texpected: 2" | ||
); | ||
}); | ||
|
||
it("should fail if component child doesn't match", () => { | ||
//given | ||
const Comp = () => { | ||
return h("p", {}, h("comp"), h(TestComp)); | ||
}; | ||
const comp = TestRenderer.create(h(Comp)).root; | ||
/** @type {Error?} */ | ||
let resError = null; | ||
|
||
//when | ||
try { | ||
assertComponents(comp.children, h("p", {}, h("comp"), h(TestComp2))); | ||
} catch (error) { | ||
resError = error; | ||
} | ||
|
||
//then | ||
assert.deepEqual( | ||
resError?.message, | ||
"Component type doesn't match for p > TestComp2" + | ||
"\n\tactual: TestComp" + | ||
"\n\texpected: TestComp2" | ||
); | ||
}); | ||
}); |