diff --git a/.size-snapshot.json b/.size-snapshot.json index 93e3cc8f..3a96a8a2 100644 --- a/.size-snapshot.json +++ b/.size-snapshot.json @@ -81,8 +81,8 @@ "gzipped": 9217 }, "dist/rbx.umd.js": { - "bundled": 89389, - "minified": 45120, - "gzipped": 9269 + "bundled": 89398, + "minified": 45131, + "gzipped": 9273 } } diff --git a/src/__tests__/testing.tsx b/src/__tests__/testing.tsx index 07b8e092..1edf8ffa 100644 --- a/src/__tests__/testing.tsx +++ b/src/__tests__/testing.tsx @@ -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 { @@ -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 } }; }, diff --git a/src/__tests__/utils.test.ts b/src/__tests__/utils.test.ts index 72325df8..5a464beb 100644 --- a/src/__tests__/utils.test.ts +++ b/src/__tests__/utils.test.ts @@ -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); }); }); diff --git a/src/utils.ts b/src/utils.ts index 3857bab1..33d828cd 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 );