UNPKG

one

Version:

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

107 lines (106 loc) 6.19 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", function () { var id = resolveEnvironmentGuard("server-only", "ssr"); expect(id).toBe("\0one-env-guard:server-only:ssr"); }), it("returns virtual id for client-only", function () { var id = resolveEnvironmentGuard("client-only", "client"); expect(id).toBe("\0one-env-guard:client-only:client"); }), it("returns virtual id for native-only", function () { var id = resolveEnvironmentGuard("native-only", "ios"); expect(id).toBe("\0one-env-guard:native-only:ios"); }), it("returns virtual id for web-only", function () { var id = resolveEnvironmentGuard("web-only", "client"); expect(id).toBe("\0one-env-guard:web-only:client"); }); }), 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 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"); 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"); expect(result).toContain("throw new Error"); }), it("server-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:server-only:android"); expect(result).toContain("throw new Error"); }), it("client-only: 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"); expect(result).toContain("throw new Error"); }), it("client-only: forbidden in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:ios"); expect(result).toContain("throw new Error"); }), it("client-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:client-only:android"); expect(result).toContain("throw new Error"); }), it("native-only: allowed in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:ios"); expect(result).toBe("export {}"); }), it("native-only: allowed in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:android"); expect(result).toBe("export {}"); }), it("native-only: forbidden in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:client"); expect(result).toContain("throw new Error"); }), it("native-only: forbidden in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:native-only:ssr"); expect(result).toContain("throw new Error"); }), it("web-only: allowed in client", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:client"); expect(result).toBe("export {}"); }), it("web-only: allowed in ssr", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:ssr"); expect(result).toBe("export {}"); }), it("web-only: forbidden in ios", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:ios"); expect(result).toContain("throw new Error"); }), it("web-only: forbidden in android", function () { var result = loadEnvironmentGuard("\0one-env-guard:web-only:android"); 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", { disabled: !0 }); 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", { disableGuards: ["client-only"] }); expect(id1).toBe("\0one-env-guard:client-only:disabled"); var id2 = resolveEnvironmentGuard("server-only", "client", { disableGuards: ["client-only"] }); expect(id2).toBe("\0one-env-guard:server-only:client"), expect(loadEnvironmentGuard(id2)).toContain("throw new Error"); }), it("disableGuards: can disable multiple guards", function () { var id1 = resolveEnvironmentGuard("client-only", "ssr", { disableGuards: ["client-only", "server-only"] }); expect(id1).toBe("\0one-env-guard:client-only:disabled"); var id2 = resolveEnvironmentGuard("server-only", "client", { disableGuards: ["client-only", "server-only"] }); expect(id2).toBe("\0one-env-guard:server-only:disabled"); var id3 = resolveEnvironmentGuard("native-only", "client", { disableGuards: ["client-only", "server-only"] }); expect(id3).toBe("\0one-env-guard:native-only:client"); }); }); }); //# sourceMappingURL=environmentGuardPlugin.test.native.js.map