Skip to content

Commit

Permalink
Fixed some minor type issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Jun 10, 2017
1 parent 2a3b309 commit f3db575
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/types/utility-types/identifier.ts
Expand Up @@ -35,7 +35,7 @@ export class IdentifierType<T> extends Type<T, T> {
}

describe() {
return `identifier(${this.identifierType})`
return `identifier(${this.identifierType.describe()})`
}

isValidSnapshot(value: any, context: IContext): IValidationResult {
Expand Down
4 changes: 1 addition & 3 deletions src/types/utility-types/reference.ts
Expand Up @@ -92,13 +92,11 @@ export class ReferenceType<T> extends Type<ReferenceSnapshot, T> {
}
}

// TODO: fix, references are not mentioned in type.describe...
// TODO support get / set
export function reference<T>(factory: IType<any, T>): IType<{ $ref: string }, T | null>;
export function reference<T>(factory: IType<any, T>): IType<string | number, T>
export function reference<T>(factory: IType<any, T>): any {
if (arguments.length === 2 && typeof arguments[1] === "string")
fail("References with base path are no longer supported. Please remove the base path.")
// FIXME: IType return type is inconsistent with what is actually returned, however, results in the best type-inference results for objects...
return new ReferenceType(factory)
}

Expand Down
23 changes: 18 additions & 5 deletions test/reference.ts
Expand Up @@ -76,7 +76,7 @@ test("identifiers are required", (t) => {

t.throws(() => Todo.create(),
"[mobx-state-tree] Error while converting `{}` to `AnonymousModel`:\n" +
"at path \"/id\" value `undefined` is not assignable to type: `identifier(string)`, expected an instance of `identifier(string)` or a snapshot like `identifier([object Object])` instead."
"at path \"/id\" value `undefined` is not assignable to type: `identifier(string)`, expected an instance of `identifier(string)` or a snapshot like `identifier(string)` instead."
)
})

Expand All @@ -89,9 +89,9 @@ test("identifiers cannot be modified", (t) => {
unprotect(todo)

t.throws(() => todo.id = "stuff", "[mobx-state-tree] Tried to change identifier from 'x' to 'stuff'. Changing identifiers is not allowed.")
t.throws(() => applySnapshot(todo, {}),
"[mobx-state-tree] Error while converting `{}` to `AnonymousModel`:\n" +
"at path \"/id\" value `undefined` is not assignable to type: `identifier(string)`, expected an instance of `identifier(string)` or a snapshot like `identifier([object Object])` instead.")
t.throws(() => applySnapshot(todo, { id: "stuff" }),
"[mobx-state-tree] Tried to change identifier from 'x' to 'stuff'. Changing identifiers is not allowed."
)
})

test("it should resolve refs during creation, when using path", t => {
Expand Down Expand Up @@ -514,7 +514,20 @@ test("References are non-nullable by default", t => {
store.maybeRef = null
t.is(store.maybeRef, null)
t.throws(
() => store.ref = null,
() => store.ref = null as any,
"[mobx-state-tree] Error while converting `null` to `reference(AnonymousModel)`:\nvalue `null` is not assignable to type: `reference(AnonymousModel)` (Value '`null`' is not a valid reference. Expected a string or number.), expected an instance of `reference(AnonymousModel)` or a snapshot like `reference(AnonymousModel)` instead."
)
})

test("References are described properly", t => {
const Todo = types.model({
id: types.identifier(types.number)
})
const Store = types.model({
todo: types.maybe(Todo),
ref: types.reference(Todo),
maybeRef: types.maybe(types.reference(Todo))
})

t.is(Store.describe(), "{ todo: ({ id: identifier(number) } | null?); ref: reference(AnonymousModel); maybeRef: (reference(AnonymousModel) | null?) }")
})

0 comments on commit f3db575

Please sign in to comment.