-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
new(bool): Add
onlyFalse()
and onlyTrue()
methods.
- Loading branch information
Showing
10 changed files
with
167 additions
and
46 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
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Builder from './Builder'; | ||
|
||
export default class BooleanBuilder<T extends boolean = boolean> extends Builder<T> { | ||
constructor(defaultValue?: T) { | ||
super('boolean', defaultValue || (false as T)); | ||
} | ||
|
||
onlyFalse(): BooleanBuilder<false> { | ||
this.defaultValue = false as T; | ||
this.addCheck(this.checkOnlyFalse); | ||
|
||
return (this as unknown) as BooleanBuilder<false>; | ||
} | ||
|
||
checkOnlyFalse(path: string, value: T) { | ||
if (__DEV__) { | ||
this.invariant(value === false, 'May only be `false`.', path); | ||
} | ||
} | ||
|
||
onlyTrue(): BooleanBuilder<true> { | ||
this.defaultValue = true as T; | ||
this.addCheck(this.checkOnlyTrue); | ||
|
||
return (this as unknown) as BooleanBuilder<true>; | ||
} | ||
|
||
checkOnlyTrue(path: string, value: T) { | ||
if (__DEV__) { | ||
this.invariant(value === true, 'May only be `true`.', path); | ||
} | ||
} | ||
} | ||
|
||
export function bool(defaultValue: boolean = false) /* infer */ { | ||
return new BooleanBuilder<boolean>(defaultValue); | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import BooleanBuilder, { bool } from '../src/BooleanBuilder'; | ||
|
||
describe('BooleanBuilder', () => { | ||
let builder: BooleanBuilder<boolean>; | ||
|
||
beforeEach(() => { | ||
builder = bool(); | ||
}); | ||
|
||
describe('bool()', () => { | ||
it('returns a builder', () => { | ||
expect(bool(true)).toBeInstanceOf(BooleanBuilder); | ||
}); | ||
|
||
it('sets type and default value', () => { | ||
builder = bool(true); | ||
|
||
expect(builder.type).toBe('boolean'); | ||
expect(builder.defaultValue).toBe(true); | ||
}); | ||
|
||
it('errors if a non-boolean value is used', () => { | ||
expect(() => { | ||
bool().runChecks( | ||
'key', | ||
// @ts-ignore Test invalid type | ||
123, | ||
{}, | ||
); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('returns the type alias', () => { | ||
expect(bool().typeAlias()).toBe('boolean'); | ||
}); | ||
}); | ||
|
||
describe('onlyFalse()', () => { | ||
it('adds a checker', () => { | ||
builder.onlyFalse(); | ||
|
||
expect(builder.checks[1]).toEqual({ | ||
callback: builder.checkOnlyFalse, | ||
args: [], | ||
}); | ||
}); | ||
|
||
it('errors if value is `true`', () => { | ||
builder.onlyFalse(); | ||
|
||
expect(() => { | ||
builder.runChecks('key', true, { key: true }); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('passes if value is `false`', () => { | ||
builder.onlyFalse(); | ||
|
||
expect(() => { | ||
expect(builder.runChecks('key', false, { key: false })).toBe(false); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('passes if value is undefined', () => { | ||
builder.onlyFalse(); | ||
|
||
expect(() => { | ||
expect(builder.runChecks('key', undefined, { key: undefined })).toBe(false); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
|
||
describe('onlyTrue()', () => { | ||
it('adds a checker', () => { | ||
builder.onlyTrue(); | ||
|
||
expect(builder.checks[1]).toEqual({ | ||
callback: builder.checkOnlyTrue, | ||
args: [], | ||
}); | ||
}); | ||
|
||
it('errors if value is `false`', () => { | ||
builder.onlyTrue(); | ||
|
||
expect(() => { | ||
builder.runChecks('key', false, { key: false }); | ||
}).toThrowErrorMatchingSnapshot(); | ||
}); | ||
|
||
it('passes if value is `true`', () => { | ||
builder.onlyTrue(); | ||
|
||
expect(() => { | ||
expect(builder.runChecks('key', true, { key: true })).toBe(true); | ||
}).not.toThrow(); | ||
}); | ||
|
||
it('passes if value is undefined', () => { | ||
builder.onlyTrue(); | ||
|
||
expect(() => { | ||
expect(builder.runChecks('key', undefined, { key: undefined })).toBe(true); | ||
}).not.toThrow(); | ||
}); | ||
}); | ||
}); |
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
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
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
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`BooleanBuilder bool() errors if a non-boolean value is used 1`] = `"Invalid field \\"key\\". Must be a boolean."`; | ||
|
||
exports[`BooleanBuilder onlyFalse() errors if value is \`true\` 1`] = `"Invalid field \\"key\\". May only be \`false\`."`; | ||
|
||
exports[`BooleanBuilder onlyTrue() errors if value is \`false\` 1`] = `"Invalid field \\"key\\". May only be \`true\`."`; |
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