Skip to content

Commit

Permalink
call options._render when we actually render' (#24)
Browse files Browse the repository at this point in the history
* call options._render when we actually render'

* add tests
  • Loading branch information
JoviDeCroock authored Apr 21, 2021
1 parent 69d5784 commit f68e141
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ export default function prepass(
c.state = {};
}

// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) options.__r(vnode);

let isClassComponent = false;

// Necessary for createContext api. Setting this property will pass
Expand All @@ -78,6 +74,9 @@ export default function prepass(
try {
const previousSkipEffects = options[_skipEffects];
options[_skipEffects] = true;
// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) options.__r(vnode);
const renderResult = Promise.resolve(
nodeName.call(vnode.__c, props, cctx)
);
Expand Down Expand Up @@ -116,6 +115,9 @@ export default function prepass(

doRender = () => {
try {
// options.render was renamed to _render (mangled to __r)
if (options.render) options.render(vnode);
if (options.__r) options.__r(vnode);
return Promise.resolve(c.render(c.props, c.state, c.context));
} catch (e) {
if (e && e.then) {
Expand Down
36 changes: 36 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,42 @@ describe("prepass", () => {
});
});

it("should call options.render for function components", async () => {
const render = jest.fn();
const r = jest.fn();
options.render = render;
options.__r = r;
const Component = jest.fn(() => <div />);

const promise = prepass(<Component prop="value" />);

const result = await promise;
expect(result).toEqual(undefined);

expect(render).toHaveBeenCalledTimes(1);
expect(r).toHaveBeenCalledTimes(1);
});

it("should call options.render for class components", async () => {
const render = jest.fn();
const r = jest.fn();
options.render = render;
options.__r = r;

class Outer extends Component {
render() {
return <div />
}
}
const outerRenderSpy = jest.spyOn(Outer.prototype, "render");

await prepass(<Outer />);

expect(outerRenderSpy).toHaveBeenCalled();
expect(render).toHaveBeenCalledTimes(1);
expect(r).toHaveBeenCalledTimes(1);
});

describe("vnode traversal", () => {
it("should traverse functional components", async () => {
const Inner = jest.fn(() => <div />);
Expand Down

0 comments on commit f68e141

Please sign in to comment.