Skip to content

Commit

Permalink
Expose hasEnv API now that getEnv can throw
Browse files Browse the repository at this point in the history
  • Loading branch information
thegedge committed May 9, 2024
1 parent 2ccaffe commit f13b2bc
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
41 changes: 39 additions & 2 deletions spec/api.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { IsExact } from "conditional-type-checks";
import { assert } from "conditional-type-checks";
import type { SnapshotOut } from "../src";
import { isReadOnlyNode, resolveIdentifier, types } from "../src";
import { hasEnv, isReadOnlyNode, resolveIdentifier, types } from "../src";
import {
applySnapshot,
getEnv,
Expand All @@ -15,7 +15,7 @@ import {
isRoot,
} from "../src/api";
import { NamedThingClass, TestClassModel } from "./fixtures/TestClassModel";
import { NamedThing, TestModel, TestModelSnapshot } from "./fixtures/TestModel";
import { TestModel, TestModelSnapshot } from "./fixtures/TestModel";

describe("getParent", () => {
test("returns the proper root for a read-only instance", () => {
Expand Down Expand Up @@ -80,6 +80,28 @@ describe("getRoot", () => {
});
});

describe("hasEnv", () => {
describe.each(["with", "without"])("%s an env", (withOrWithoutYou) => {
const expected = withOrWithoutYou == "with" ? true : false;
const env = withOrWithoutYou == "with" ? { test: 1 } : undefined;

test(`returns ${expected} for quick tree instances`, () => {
const m = TestModel.createReadOnly(TestModelSnapshot, env);
expect(hasEnv(m)).toEqual(expected);
});

test(`returns ${expected} for MST instances with an env`, () => {
const m = TestModel.create(TestModelSnapshot, env);
expect(hasEnv(m)).toEqual(expected);
});

test(`returns ${expected} for read only model class instances`, () => {
const m = TestClassModel.createReadOnly(TestModelSnapshot, env);
expect(hasEnv(m)).toEqual(expected);
});
});
});

describe("getEnv", () => {
test("returns expected env for quick tree instances", () => {
const m = TestModel.createReadOnly(TestModelSnapshot, { test: 1 });
Expand All @@ -92,6 +114,21 @@ describe("getEnv", () => {
expect(getEnv(m)).toEqual({ test: 1 });
});

test("returns expected env for read only model class instances", () => {
const m = TestClassModel.createReadOnly(TestModelSnapshot);
expect(() => getEnv(m)).toThrow();
});

test("returns expected env for quick tree instances", () => {
const m = TestModel.createReadOnly(TestModelSnapshot);
expect(() => getEnv(m)).toThrow();
});

test("returns expected env for MST instances", () => {
const m = TestModel.create(TestModelSnapshot);
expect(() => getEnv(m)).toThrow();
});

test("returns expected env for read only model class instances", () => {
const m = TestClassModel.createReadOnly(TestModelSnapshot, { test: 1 });
expect(getEnv(m)).toEqual({ test: 1 });
Expand Down
20 changes: 17 additions & 3 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getParentOfType as mstGetParentOfType,
getRoot as mstGetRoot,
getType as mstGetType,
hasEnv as mstHasEnv,
isArrayType as mstIsArrayType,
isIdentifierType as mstIsIdentifierType,
isMapType as mstIsMapType,
Expand Down Expand Up @@ -40,8 +41,8 @@ import type {
IStateTreeNode,
IType,
Instance,
TreeContext,
SnapshotIn,
TreeContext,
} from "./types";

export {
Expand All @@ -50,13 +51,13 @@ export {
applyPatch,
clone,
createActionTrackingMiddleware2,
getRunningActionContext,
destroy,
detach,
escapeJsonPath,
getIdentifier,
getPath,
getPathParts,
getRunningActionContext,
hasParent,
isActionContextThisOrChildOf,
isAlive,
Expand Down Expand Up @@ -178,7 +179,20 @@ export function getEnv<Env = any>(value: IAnyStateTreeNode): Env {
return mstGetEnv(value);
}

return (getContext(value)?.env ?? {}) as Env;
const env = getContext(value)?.env;
if (!env) {
throw new Error(`Failed to find the environment of ${value}`);
}

return env as Env;
}

export function hasEnv(value: IAnyStateTreeNode): boolean {
if (mstIsStateTreeNode(value)) {
return mstHasEnv(value);
}

return !!getContext(value)?.env;
}

export const getRoot = <T extends IAnyType>(value: IAnyStateTreeNode): Instance<T> => {
Expand Down

0 comments on commit f13b2bc

Please sign in to comment.