Skip to content

Commit

Permalink
test: TSify MoreShallowTools
Browse files Browse the repository at this point in the history
  • Loading branch information
pjonsson committed Feb 27, 2025
1 parent fab3a24 commit 037c41f
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,31 +1,36 @@
"use strict";

import type { ReactElement } from "react";
import { createRenderer } from "react-test-renderer/shallow";
import { findAll } from "react-shallow-testutils";

export function getRenderedRenderer(jsx) {
export function getRenderedRenderer(jsx: ReactElement) {
const renderer = createRenderer();
renderer.render(jsx);
return renderer;
}

export function getShallowRenderedOutput(jsx) {
export function getShallowRenderedOutput(jsx: ReactElement) {
const renderer = getRenderedRenderer(jsx);
return renderer.getRenderOutput();
}

export function getMountedInstance(jsx) {
export function getMountedInstance(jsx: ReactElement) {
const renderer = getRenderedRenderer(jsx);
return renderer.getMountedInstance();
}

export function findAllEqualTo(reactElement, text) {
return findAll(reactElement, (element) => element && element === text);
export function findAllEqualTo(reactElement: ReactElement, text: ReactElement) {
return findAll(
reactElement,
(element: ReactElement) => element && element === text
);
}

export function findAllWithPropsChildEqualTo(reactElement, text) {
export function findAllWithPropsChildEqualTo(
reactElement: ReactElement,
text: ReactElement
) {
// Returns elements with element.props.children[i] or element.props.children[i][j] equal to text, for any i or j.
return findAll(reactElement, (element) => {
return findAll(reactElement, (element: ReactElement) => {
if (!(element && element.props && element.props.children)) {
return;
}
Expand All @@ -34,7 +39,7 @@ export function findAllWithPropsChildEqualTo(reactElement, text) {
element.props.children.indexOf(text) >= 0) ||
(element.props.children.some &&
element.props.children.some(
(x) => x && x.length && x.indexOf(text) >= 0
(x: any) => x && x.length && x.indexOf(text) >= 0
))
);
});
Expand Down

0 comments on commit 037c41f

Please sign in to comment.