reblend-testing-library
Version:
Simple and complete Reblendjs testing utilities that encourage good testing practices.
115 lines (114 loc) • 3.27 kB
JavaScript
"use strict";
var _reblendjs = require("reblendjs");
var _pure = require("../pure");
//Reblend custom hooks are not yet stable
test("gives committed result", async () => {
//@ReblendHook
function useStateHookTest(
/* @Reblend: Transformed from function to class */
) {
const [states, setStates] = _reblendjs.useState.bind(this)(1, "states");
this.state.states = states;
this.state.setStates = setStates;
const state = {
states: this.state.states,
setStates: this.state.setStates
};
this.state.state = state;
_reblendjs.useEffect.bind(this)(() => {
this.state.setStates(2);
}, (() => []).bind(this));
_reblendjs.useEffect.bind(this)(() => {
this.state.state.states = this.state.states;
}, (() => this.state.states).bind(this));
return this.state.state;
}
const {
result
} = await (0, _pure.renderHook)(useStateHookTest);
expect(result.current).toEqual({
states: 2,
setStates: expect.any(Function)
});
});
test("allows rerendering", async () => {
//@ReblendHook
function useLeftRight(
/* @Reblend: Transformed from function to class */
) {
const [left, setLeft] = _reblendjs.useState.bind(this)("left", "left");
this.state.left = left;
this.state.setLeft = setLeft;
const [right, setRight] = _reblendjs.useState.bind(this)("right", "right");
this.state.right = right;
this.state.setRight = setRight;
const result = {
result: null
};
this.state.result = result;
_reblendjs.useProps.bind(this)(({
current: {
initialProps: {
branch
}
}
}) => {
switch (branch) {
case "left":
this.state.result.result = [this.state.left, this.state.setLeft];
break;
case "right":
this.state.result.result = [this.state.right, this.state.setRight];
break;
default:
throw new Error("No Props passed. This is a bug in the implementation");
}
});
return this.state.result;
}
const {
result,
rerender
} = await (0, _pure.renderHook)(useLeftRight, {
initialProps: {
branch: "left"
}
});
expect(result.current.result).toEqual(["left", expect.any(Function)]);
await rerender({
branch: "right"
});
expect(result.current.result).toEqual(["right", expect.any(Function)]);
});
test("allows wrapper components", async () => {
const Context = (0, _reblendjs.createContext)("default");
class Wrapper extends _reblendjs.default {
static ELEMENT_NAME = "Wrapper";
constructor() {
super();
}
async initState() {}
async initProps({
children
}) {
this.props = {};
this.props.children = children;
}
async html() {
return _reblendjs.default.construct.bind(this)(_reblendjs.default, null, this.props.children);
}
}
/* @Reblend: Transformed from function to class */
const {
result
} = await (0, _pure.renderHook)(function useAny(
/* @Reblend: Transformed from function to class */
) {
const ctx = _reblendjs.useContext.bind(this)(Context, "ctx");
this.state.ctx = ctx;
return this.state.ctx;
}, {
wrapper: Wrapper
});
expect(result.current[0]).toEqual("default");
});