Add tests
This commit is contained in:
parent
3cf230e3af
commit
ae36a79f8c
|
@ -45,7 +45,7 @@ export function isRequired(constraints: FieldConstraints | undefined) {
|
||||||
const isRequired =
|
const isRequired =
|
||||||
!!constraints &&
|
!!constraints &&
|
||||||
((typeof constraints.presence !== "boolean" &&
|
((typeof constraints.presence !== "boolean" &&
|
||||||
!constraints.presence?.allowEmpty) ||
|
constraints.presence?.allowEmpty === false) ||
|
||||||
constraints.presence === true)
|
constraints.presence === true)
|
||||||
return isRequired
|
return isRequired
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,35 @@
|
||||||
|
import { isRequired } from "../schema"
|
||||||
|
|
||||||
|
describe("schema utilities", () => {
|
||||||
|
describe("isRequired", () => {
|
||||||
|
it("not required by default", () => {
|
||||||
|
const result = isRequired(undefined)
|
||||||
|
expect(result).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("required when presence is true", () => {
|
||||||
|
const result = isRequired({ presence: true })
|
||||||
|
expect(result).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("not required when presence is false", () => {
|
||||||
|
const result = isRequired({ presence: false })
|
||||||
|
expect(result).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("not required when presence is an empty object", () => {
|
||||||
|
const result = isRequired({ presence: {} })
|
||||||
|
expect(result).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("not required when allowEmpty is true", () => {
|
||||||
|
const result = isRequired({ presence: { allowEmpty: true } })
|
||||||
|
expect(result).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it("required when allowEmpty is false", () => {
|
||||||
|
const result = isRequired({ presence: { allowEmpty: false } })
|
||||||
|
expect(result).toBe(true)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
Loading…
Reference in New Issue