UNPKG

one

Version:

One is a new React Framework that makes Vite serve both native and web.

152 lines (151 loc) 7.42 kB
import { describe, expect, it } from "vitest"; import { resolveEnvironmentGuard, loadEnvironmentGuard } from "./environmentGuardPlugin.native.js"; describe("environmentGuardPlugin", function () { describe("resolveEnvironmentGuard", function () { it("returns null for non-guard specifiers", function () { expect(resolveEnvironmentGuard("react", "client")).toBeNull(); expect(resolveEnvironmentGuard("vite", "ssr")).toBeNull(); expect(resolveEnvironmentGuard("some-other-pkg", "ios")).toBeNull(); }); it("returns virtual id for server-only (with consumer)", function () { var id = resolveEnvironmentGuard("server-only", "ssr", "server"); expect(id).toBe("\0one-env-guard:server-only:ssr:server"); }); it("returns virtual id for client-only (with consumer)", function () { var id = resolveEnvironmentGuard("client-only", "client", "client"); expect(id).toBe("\0one-env-guard:client-only:client:client"); }); it("returns virtual id for native-only", function () { var id = resolveEnvironmentGuard("native-only", "ios"); expect(id).toBe("\0one-env-guard:native-only:ios:unknown"); }); it("returns virtual id for web-only", function () { var id = resolveEnvironmentGuard("web-only", "client"); expect(id).toBe("\0one-env-guard:web-only:client:unknown"); }); it("encodes worker consumer for cloudflare deploy", function () { var id = resolveEnvironmentGuard("server-only", "worker", "server"); expect(id).toBe("\0one-env-guard:server-only:worker:server"); }); }); describe("loadEnvironmentGuard", function () { it("returns null for non-virtual ids", function () { expect(loadEnvironmentGuard("react")).toBeNull(); expect(loadEnvironmentGuard("/some/path.ts")).toBeNull(); }); it("server-only: allowed in any server-consumer env", function () { expect(loadEnvironmentGuard("\0one-env-guard:server-only:ssr:server")).toBe("export {}"); expect(loadEnvironmentGuard("\0one-env-guard:server-only:worker:server")).toBe("export {}"); expect(loadEnvironmentGuard("\0one-env-guard:server-only:edge:server")).toBe("export {}"); }); it("server-only: legacy 2-part form still allowed in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:server-only:ssr"); expect(result).toBe("export {}"); }); it("server-only: forbidden in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:server-only:client:client"); expect(result).toContain("throw new Error"); expect(result).toContain("server-only"); expect(result).toContain("client"); }); it("server-only: forbidden in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:server-only:ios:client"); expect(result).toContain("throw new Error"); }); it("server-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:server-only:android:client"); expect(result).toContain("throw new Error"); }); it("client-only: allowed in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:client:client"); expect(result).toBe("export {}"); }); it("client-only: legacy 2-part form still allowed in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:client"); expect(result).toBe("export {}"); }); it("client-only: forbidden in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:ssr:server"); expect(result).toContain("throw new Error"); }); it("client-only: forbidden in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:ios:server"); expect(result).toContain("throw new Error"); }); it("client-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:android:server"); expect(result).toContain("throw new Error"); }); it("native-only: allowed in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:ios:unknown"); expect(result).toBe("export {}"); }); it("native-only: allowed in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:android:unknown"); expect(result).toBe("export {}"); }); it("native-only: forbidden in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:client:client"); expect(result).toContain("throw new Error"); }); it("native-only: forbidden in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:ssr:server"); expect(result).toContain("throw new Error"); }); it("web-only: allowed in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:client:client"); expect(result).toBe("export {}"); }); it("web-only: allowed in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:ssr:server"); expect(result).toBe("export {}"); }); it("web-only: forbidden in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:ios:unknown"); expect(result).toContain("throw new Error"); }); it("web-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:android:unknown"); expect(result).toContain("throw new Error"); }); it("disabled guards always pass", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:disabled"); expect(result).toBe("export {}"); }); }); describe("options", function () { it("disabled: true makes all guards no-ops", function () { var id = resolveEnvironmentGuard("client-only", "ssr", "server", { disabled: true }); expect(id).toBe("\0one-env-guard:client-only:disabled"); expect(loadEnvironmentGuard(id)).toBe("export {}"); }); it("disableGuards: disables specific guard types", function () { var id1 = resolveEnvironmentGuard("client-only", "ssr", "server", { disableGuards: ["client-only"] }); expect(id1).toBe("\0one-env-guard:client-only:disabled"); var id2 = resolveEnvironmentGuard("server-only", "client", "client", { disableGuards: ["client-only"] }); expect(id2).toBe("\0one-env-guard:server-only:client:client"); expect(loadEnvironmentGuard(id2)).toContain("throw new Error"); }); it("disableGuards: can disable multiple guards", function () { var id1 = resolveEnvironmentGuard("client-only", "ssr", "server", { disableGuards: ["client-only", "server-only"] }); expect(id1).toBe("\0one-env-guard:client-only:disabled"); var id2 = resolveEnvironmentGuard("server-only", "client", "client", { disableGuards: ["client-only", "server-only"] }); expect(id2).toBe("\0one-env-guard:server-only:disabled"); var id3 = resolveEnvironmentGuard("native-only", "client", "client", { disableGuards: ["client-only", "server-only"] }); expect(id3).toBe("\0one-env-guard:native-only:client:client"); }); }); }); //# sourceMappingURL=environmentGuardPlugin.test.native.js.map