Skip to content

Commit

Permalink
Added withStacksContext test util
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-podzigun committed Jan 20, 2025
1 parent b30a68b commit 75d7e62
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 30 deletions.
22 changes: 22 additions & 0 deletions src/stack/WithStacksData.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @typedef {import("@farjs/blessed").Widgets.BlessedElement} BlessedElement
*/
import PanelStack from "./PanelStack.mjs";

/**
* @typedef {{
* readonly stack: PanelStack;
* readonly input: BlessedElement;
* }} WithStacksData
*/

/**
* @param {PanelStack} stack
* @param {BlessedElement} [input]
* @returns {WithStacksData}
*/
function WithStacksData(stack, input) {
return { stack, input: input ?? /** @type {BlessedElement} */ ({}) };
}

export default WithStacksData;
10 changes: 1 addition & 9 deletions src/stack/WithStacksProps.mjs
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
/**
* @typedef {import("@farjs/blessed").Widgets.BlessedElement} BlessedElement
*/
import PanelStack from "./PanelStack.mjs";

/**
* @typedef {{
* readonly stack: PanelStack;
* readonly input: BlessedElement;
* }} WithStacksData
* @typedef {import("./WithStacksData.mjs").WithStacksData} WithStacksData
*/

/**
Expand Down
26 changes: 26 additions & 0 deletions src/stack/withStacksContext.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @typedef {import("./WithStacksProps.mjs").WithStacksProps} WithStacksProps
*/
import React from "react";
import WithStacks from "./WithStacks.mjs";

const h = React.createElement;

/**
* Common test util.
*
* @param {React.ReactElement<any>} element
* @param {WithStacksProps} props
* @returns {React.ReactElement<any>}
*/
const withStacksContext = (element, props) => {
return h(
WithStacks.Context.Provider,
{
value: props,
},
element
);
};

export default withStacksContext;
45 changes: 36 additions & 9 deletions test/stack/WithStacks.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import mockFunction from "mock-fn";
import TestRenderer from "react-test-renderer";
import { assertComponents, TestErrorBoundary } from "react-assert";
import PanelStack from "../../src/stack/PanelStack.mjs";
import WithStacksData from "../../src/stack/WithStacksData.mjs";
import WithStacksProps from "../../src/stack/WithStacksProps.mjs";
import withStacksContext from "../../src/stack/withStacksContext.mjs";
import WithStacks from "../../src/stack/WithStacks.mjs";

const h = React.createElement;
Expand All @@ -23,14 +25,11 @@ const { describe, it } = await (async () => {
})();

const props = WithStacksProps(
{
stack: new PanelStack(true, [], mockFunction()),
input: /** @type {BlessedElement} */ ({}),
},
{
stack: new PanelStack(false, [], mockFunction()),
input: /** @type {BlessedElement} */ ({}),
}
WithStacksData(new PanelStack(true, [], mockFunction())),
WithStacksData(
new PanelStack(false, [], mockFunction()),
/** @type {BlessedElement} */ ({})
)
);

describe("WithStacks.test.mjs", () => {
Expand Down Expand Up @@ -91,6 +90,34 @@ describe("WithStacks.test.mjs", () => {
assert.deepEqual(otherContent, "some other content");
});

it("should render component when withStacksContext", () => {
//given
const [stacksCtx, stacksComp] = getStacksCtxHook();

//when
const result = TestRenderer.create(
withStacksContext(
h(
React.Fragment,
null,
h(stacksComp, null),
h(React.Fragment, null, "some other content")
),
props
)
).root;

//then
assert.deepEqual(WithStacks.displayName, "WithStacks");
assert.deepEqual(stacksCtx.current, props);
assert.deepEqual(result.children.length, 2);
const [resCtxHook, otherContent] = result.children.map(
(_) => /** @type {TestRenderer.ReactTestInstance} */ (_)
);
assert.deepEqual(resCtxHook.type, stacksComp);
assert.deepEqual(otherContent, "some other content");
});

it("should return active stack when WithStacksProps.active()", () => {
//given
const { left, right } = props;
Expand All @@ -117,7 +144,7 @@ describe("WithStacks.test.mjs", () => {
});

/**
* @returns {[React.MutableRefObject<WithStacksProps | null>, () => React.ReactElement]}
* @returns {[React.MutableRefObject<WithStacksProps | null>, () => React.ReactElement<any>]}
*/
function getStacksCtxHook() {
/** @type {React.MutableRefObject<WithStacksProps | null>} */
Expand Down
19 changes: 19 additions & 0 deletions types/stack/WithStacksData.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export default WithStacksData;
export type WithStacksData = {
readonly stack: PanelStack;
readonly input: BlessedElement;
};
export type BlessedElement = import("@farjs/blessed").Widgets.BlessedElement;
/**
* @typedef {{
* readonly stack: PanelStack;
* readonly input: BlessedElement;
* }} WithStacksData
*/
/**
* @param {PanelStack} stack
* @param {BlessedElement} [input]
* @returns {WithStacksData}
*/
declare function WithStacksData(stack: PanelStack, input?: import("blessed").Widgets.BlessedElement | undefined): WithStacksData;
import PanelStack from "./PanelStack.mjs";
16 changes: 4 additions & 12 deletions types/stack/WithStacksProps.d.mts
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
export default WithStacksProps;
export type WithStacksData = {
readonly stack: PanelStack;
readonly input: BlessedElement;
};
export type WithStacksData = import("./WithStacksData.mjs").WithStacksData;
export type WithStacksProps = {
readonly left: WithStacksData;
readonly right: WithStacksData;
};
export type BlessedElement = import("@farjs/blessed").Widgets.BlessedElement;
/**
* @typedef {{
* readonly stack: PanelStack;
* readonly input: BlessedElement;
* }} WithStacksData
* @typedef {import("./WithStacksData.mjs").WithStacksData} WithStacksData
*/
/**
* @typedef {{
Expand All @@ -31,11 +24,10 @@ declare namespace WithStacksProps {
* @param {WithStacksProps} stacks
* @returns {WithStacksData}
*/
function active(stacks: WithStacksProps): WithStacksData;
function active(stacks: WithStacksProps): import("./WithStacksData.mjs").WithStacksData;
/**
* @param {WithStacksProps} stacks
* @returns {WithStacksData}
*/
function nonActive(stacks: WithStacksProps): WithStacksData;
function nonActive(stacks: WithStacksProps): import("./WithStacksData.mjs").WithStacksData;
}
import PanelStack from "./PanelStack.mjs";
11 changes: 11 additions & 0 deletions types/stack/withStacksContext.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export default withStacksContext;
export type WithStacksProps = import("./WithStacksProps.mjs").WithStacksProps;
/**
* Common test util.
*
* @param {React.ReactElement<any>} element
* @param {WithStacksProps} props
* @returns {React.ReactElement<any>}
*/
declare function withStacksContext(element: React.ReactElement<any>, props: WithStacksProps): React.ReactElement<any>;
import React from "react";

0 comments on commit 75d7e62

Please sign in to comment.