Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/withESI.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import express from "express";
import React from "react";
import renderer from "react-test-renderer";
import withESI from "./withESI";
Expand All @@ -9,7 +8,7 @@ test("client-side", () => {
const DummyESI = withESI(Dummy, "id");
expect(DummyESI.displayName).toBe("WithESI(Dummy)");

(global.process as any).browser = true;
(global as any).navigator = {};
const component = renderer.create(<DummyESI name="Kévin" />);
expect(component).toMatchSnapshot();
});
Expand All @@ -18,7 +17,7 @@ test("client-side with serialized props", () => {
const DummyESI = withESI(Dummy, "id");
expect(DummyESI.displayName).toBe("WithESI(Dummy)");

(global.process as any).browser = true;
(global as any).navigator = {};
((global as any).__REACT_ESI__ as any) = { id: { name: "Anne" } };
const component = renderer.create(<DummyESI />);
expect(component).toMatchSnapshot();
Expand All @@ -35,7 +34,7 @@ test("client-side call getInitialProps", async () => {

const ComponentESI = withESI(Component, "initial-props");

(global.process as any).browser = true;
(global as any).navigator = {};
renderer.create(<ComponentESI />);
expect(called).toBe(true);
});
Expand All @@ -45,7 +44,7 @@ test("server-side", () => {
expect(DummyESI.displayName).toBe("WithESI(Dummy)");

process.env.REACT_ESI_SECRET = "dummy";
(global.process as any).browser = false;
delete (global as any).navigator;
const component = renderer.create(
<DummyESI esi={{ attrs: { onerror: "continue" } }} name="Kévin" />
);
Expand Down
8 changes: 2 additions & 6 deletions src/withESI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ declare global {
}
}

interface IWebpackProcess extends NodeJS.Process {
browser?: boolean;
}

interface IWithESIProps {
esi?: {
attrs?: object;
Expand Down Expand Up @@ -46,7 +42,7 @@ export default function withESI<P>(
this.esi = esi || {};
this.state.childProps = childProps;

if (!(process as IWebpackProcess).browser) {
if (typeof navigator === 'undefined') {
return;
}

Expand Down Expand Up @@ -81,7 +77,7 @@ export default function withESI<P>(
}

public render() {
if ((process as IWebpackProcess).browser) {
if (typeof navigator !== 'undefined') {
return (
<div>
<WrappedComponent {...this.state.childProps as P} />
Expand Down