Skip to content

Commit

Permalink
fix for SSR, ht: @jasonblalock
Browse files Browse the repository at this point in the history
  • Loading branch information
dfee committed Jan 25, 2019
1 parent cd1dd29 commit 401d61b
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 20 deletions.
6 changes: 3 additions & 3 deletions .size-snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@
"gzipped": 9217
},
"dist/rbx.umd.js": {
"bundled": 89389,
"minified": 45120,
"gzipped": 9269
"bundled": 89398,
"minified": 45131,
"gzipped": 9273
}
}
37 changes: 22 additions & 15 deletions src/__tests__/testing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,26 @@ export const contextManager = <
TState extends object
>(
enter: (params: TParams) => { context: TContext; state: TState },
exit: (
{
context,
params,
state,
}: { context: TContext; params: TParams; state: TState },
) => void,
exit: ({
context,
params,
state,
}: {
context: TContext;
params: TParams;
state: TState;
}) => void,
) => (
params: TParams,
inner: (
{
context,
params,
state,
}: { context: TContext; params: TParams; state: TState },
) => void,
inner: ({
context,
params,
state,
}: {
context: TContext;
params: TParams;
state: TState;
}) => void,
) => {
const { context, state } = enter(params);
try {
Expand All @@ -48,7 +52,10 @@ export const withWindow = contextManager(
({ value }: { value?: undefined } = {}) => {
const window = (global as any).window;
delete (global as any).window;
(global as any).window = value;

if (value !== undefined) {
(global as any).window = value;
}

return { context: {}, state: { window } };
},
Expand Down
5 changes: 4 additions & 1 deletion src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ describe("Utils", () => {
expect(canUseDOM()).toBe(true);
});

it("should return false without window", () => {
it("should return false without window (SSR)", () => {
withWindow({}, () => {
expect(() => window).toThrow(
new ReferenceError("window is not defined"),
);
expect(canUseDOM()).toBe(false);
});
});
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Lit } from "./types";

export const canUseDOM = () =>
!(
window === undefined ||
typeof window === "undefined" || // tslint:disable-line:no-typeof-undefined
window.document === undefined ||
window.document.createElement === undefined
);
Expand Down

0 comments on commit 401d61b

Please sign in to comment.