-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(types):
listFieldBuilder
with proper type for primitive and fields
fixes #56 #hacky
- Loading branch information
1 parent
3503913
commit 8beff9b
Showing
2 changed files
with
80 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,55 @@ | ||
import { expectTypeOf, test } from "vitest"; | ||
import { describe, expectTypeOf, test } from "vitest"; | ||
|
||
import { listFieldBuilder } from "./listFieldBuilder"; | ||
import { textField } from "../"; | ||
import { type TextField, textField } from "../"; | ||
|
||
test("listFieldBuilder - cannot build with random data", () => { | ||
const addressBuilder = listFieldBuilder(({ street }) => ({ | ||
street: textField({ name: "street", value: street }), | ||
})); | ||
describe("listFieldBuilder", () => { | ||
describe("when building list of primitive atoms", () => { | ||
const positivesBuilder = listFieldBuilder((positive) => | ||
textField({ name: "positive", value: positive }), | ||
); | ||
|
||
expectTypeOf(addressBuilder).toBeCallableWith([{ street: "foo" }]); | ||
test("empty call produces single item", () => { | ||
const single = positivesBuilder(); | ||
|
||
// Doesnt work for no-argument (due to function overload) | ||
// https://github.com/mmkal/expect-type/issues/30 | ||
// expectTypeOf(addressBuilder).toBeCallableWith(); | ||
expectTypeOf(single).toEqualTypeOf<TextField>(); | ||
}); | ||
|
||
// TODO: expect-type issue | ||
// expectTypeOf(addressBuilder).not.toBeCallableWith([{ notStreet: "foo" }]); | ||
test("call with array produces list of items", () => { | ||
// NOTE: the undefined is simply empty value | ||
const single = positivesBuilder(["pretty", "fast", undefined]); | ||
|
||
expectTypeOf(single).toEqualTypeOf<TextField[]>(); | ||
}); | ||
}); | ||
|
||
describe("when building list of form fields", () => { | ||
const addressBuilder = listFieldBuilder(({ street }) => ({ | ||
street: textField({ name: "street", value: street }), | ||
})); | ||
|
||
test("cannot build with random data", () => { | ||
expectTypeOf(addressBuilder).toBeCallableWith([{ street: "foo" }]); | ||
|
||
// Doesnt work for no-argument (due to function overload) | ||
// https://github.com/mmkal/expect-type/issues/30 | ||
// expectTypeOf(addressBuilder).toBeCallableWith(); | ||
|
||
// TODO: expect-type issue | ||
// expectTypeOf(addressBuilder).not.toBeCallableWith([{ notStreet: "foo" }]); | ||
}); | ||
|
||
test("empty call produces single item", () => { | ||
const single = addressBuilder(); | ||
|
||
expectTypeOf(single).toEqualTypeOf<{ street: TextField }>(); | ||
}); | ||
|
||
test("call with array produces list of items", () => { | ||
// NOTE: the undefined is simply empty value | ||
const multi = addressBuilder([{ street: "Hrad" }, { street: undefined }]); | ||
|
||
expectTypeOf(multi).toEqualTypeOf<{ street: TextField }[]>(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters