|
| 1 | +import { isWeb, isReactNative } from "../utils"; |
| 2 | +import { |
| 3 | + describe, |
| 4 | + expect, |
| 5 | + jest, |
| 6 | + beforeEach, |
| 7 | + afterEach, |
| 8 | + it, |
| 9 | +} from "@jest/globals"; |
| 10 | + |
| 11 | +describe("platform detection", () => { |
| 12 | + const g = globalThis as any; |
| 13 | + |
| 14 | + const saveGlobals = () => ({ |
| 15 | + window: g.window, |
| 16 | + document: g.document, |
| 17 | + navigator: g.navigator, |
| 18 | + }); |
| 19 | + |
| 20 | + const restoreGlobals = (orig: any) => { |
| 21 | + if (typeof orig.window === "undefined") delete g.window; |
| 22 | + else g.window = orig.window; |
| 23 | + |
| 24 | + if (typeof orig.document === "undefined") delete g.document; |
| 25 | + else g.document = orig.document; |
| 26 | + |
| 27 | + if (typeof orig.navigator === "undefined") delete g.navigator; |
| 28 | + else g.navigator = orig.navigator; |
| 29 | + }; |
| 30 | + |
| 31 | + let orig: any; |
| 32 | + |
| 33 | + beforeEach(() => { |
| 34 | + orig = saveGlobals(); |
| 35 | + // Start clean: no browser-like globals unless a test sets them. |
| 36 | + delete g.window; |
| 37 | + delete g.document; |
| 38 | + delete g.navigator; |
| 39 | + jest.resetModules(); // ensures fresh imports if needed later |
| 40 | + }); |
| 41 | + |
| 42 | + afterEach(() => { |
| 43 | + restoreGlobals(orig); |
| 44 | + jest.restoreAllMocks(); |
| 45 | + }); |
| 46 | + |
| 47 | + it("returns false for both on plain Node (no globals)", () => { |
| 48 | + expect(isWeb()).toBe(false); |
| 49 | + expect(isReactNative()).toBe(false); |
| 50 | + }); |
| 51 | + |
| 52 | + it("isWeb() true when window and document exist", () => { |
| 53 | + g.window = {}; // minimal stubs are fine since you only check typeof |
| 54 | + g.document = {}; |
| 55 | + expect(isWeb()).toBe(true); |
| 56 | + expect(isReactNative()).toBe(false); |
| 57 | + }); |
| 58 | + |
| 59 | + it("isWeb() false if only window exists", () => { |
| 60 | + g.window = {}; |
| 61 | + expect(isWeb()).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it("isWeb() false if only document exists", () => { |
| 65 | + g.document = {}; |
| 66 | + expect(isWeb()).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + it("isReactNative() true when navigator.product === 'ReactNative'", () => { |
| 70 | + g.navigator = { product: "ReactNative" }; |
| 71 | + expect(isReactNative()).toBe(true); |
| 72 | + expect(isWeb()).toBe(false); |
| 73 | + }); |
| 74 | + |
| 75 | + it("isReactNative() false when navigator exists but product differs", () => { |
| 76 | + g.navigator = { product: "Gecko" }; |
| 77 | + expect(isReactNative()).toBe(false); |
| 78 | + }); |
| 79 | + |
| 80 | + it("isReactNative() false when navigator missing", () => { |
| 81 | + expect(isReactNative()).toBe(false); |
| 82 | + }); |
| 83 | + |
| 84 | + it("does not accidentally treat web + RN at the same time", () => { |
| 85 | + g.window = {}; |
| 86 | + g.document = {}; |
| 87 | + g.navigator = { product: "ReactNative" }; |
| 88 | + // Your current logic would report both true; assert current behavior or |
| 89 | + // change requirements. Here we assert the current behavior explicitly. |
| 90 | + expect(isWeb()).toBe(true); |
| 91 | + expect(isReactNative()).toBe(true); |
| 92 | + }); |
| 93 | +}); |
0 commit comments