forked from rileytomasek/zodix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom error messages for helpers rileytomasek#27
- Loading branch information
Showing
4 changed files
with
98 additions
and
75 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
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,73 +1,76 @@ | ||
import { zx } from './'; | ||
|
||
describe('BoolAsString', () => { | ||
describe('boolAsString', () => { | ||
test('parses true as string', () => { | ||
expect(zx.BoolAsString.parse('true')).toBe(true); | ||
expect(zx.boolAsString().parse('true')).toBe(true); | ||
}); | ||
test('parses false as string', () => { | ||
expect(zx.BoolAsString.parse('false')).toBe(false); | ||
expect(zx.boolAsString().parse('false')).toBe(false); | ||
}); | ||
test('throws on non-boolean string', () => { | ||
expect(() => zx.BoolAsString.parse('hello')).toThrowError(); | ||
expect(() => zx.boolAsString().parse('hello')).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('CheckboxAsString', () => { | ||
describe('checkboxAsString', () => { | ||
test('parses "on" as boolean', () => { | ||
expect(zx.CheckboxAsString.parse('on')).toBe(true); | ||
expect(zx.checkboxAsString().parse('on')).toBe(true); | ||
}); | ||
test('parses "true" as boolean', () => { | ||
expect(zx.checkboxAsString({ trueValue: 'true' }).parse('true')).toBe(true); | ||
}); | ||
test('parses undefined as boolean', () => { | ||
expect(zx.CheckboxAsString.parse(undefined)).toBe(false); | ||
expect(zx.checkboxAsString().parse(undefined)).toBe(false); | ||
}); | ||
test('throws on non-"on" string', () => { | ||
expect(() => zx.CheckboxAsString.parse('hello')).toThrowError(); | ||
expect(() => zx.checkboxAsString().parse('hello')).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('IntAsString', () => { | ||
describe('intAsString', () => { | ||
test('parses int as string', () => { | ||
expect(zx.IntAsString.parse('3')).toBe(3); | ||
expect(zx.intAsString().parse('3')).toBe(3); | ||
}); | ||
test('parses int as string with leading 0', () => { | ||
expect(zx.IntAsString.parse('03')).toBe(3); | ||
expect(zx.intAsString().parse('03')).toBe(3); | ||
}); | ||
test('parses negative int as string', () => { | ||
expect(zx.IntAsString.parse('-3')).toBe(-3); | ||
expect(zx.intAsString().parse('-3')).toBe(-3); | ||
}); | ||
test('throws on int as number', () => { | ||
expect(() => zx.IntAsString.parse(3)).toThrowError(); | ||
expect(() => zx.intAsString().parse(3)).toThrowError(); | ||
}); | ||
test('throws on float', () => { | ||
expect(() => zx.IntAsString.parse(3.14)).toThrowError(); | ||
expect(() => zx.intAsString().parse(3.14)).toThrowError(); | ||
}); | ||
test('throws on string float', () => { | ||
expect(() => zx.IntAsString.parse('3.14')).toThrowError(); | ||
expect(() => zx.intAsString().parse('3.14')).toThrowError(); | ||
}); | ||
test('throws on non-int string', () => { | ||
expect(() => zx.IntAsString.parse('a3')).toThrowError(); | ||
expect(() => zx.intAsString().parse('a3')).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('NumAsString', () => { | ||
describe('numAsString', () => { | ||
test('parses number with decimal as string', () => { | ||
expect(zx.NumAsString.parse('3.14')).toBe(3.14); | ||
expect(zx.numAsString().parse('3.14')).toBe(3.14); | ||
}); | ||
test('parses number with decimal as string with leading 0', () => { | ||
expect(zx.NumAsString.parse('03.14')).toBe(3.14); | ||
expect(zx.numAsString().parse('03.14')).toBe(3.14); | ||
}); | ||
test('parses negative number with decimal as string', () => { | ||
expect(zx.NumAsString.parse('-3.14')).toBe(-3.14); | ||
expect(zx.numAsString().parse('-3.14')).toBe(-3.14); | ||
}); | ||
test('parses int as string', () => { | ||
expect(zx.NumAsString.parse('3')).toBe(3); | ||
expect(zx.numAsString().parse('3')).toBe(3); | ||
}); | ||
test('parses int as string with leading 0', () => { | ||
expect(zx.NumAsString.parse('03')).toBe(3); | ||
expect(zx.numAsString().parse('03')).toBe(3); | ||
}); | ||
test('parses negative int as string', () => { | ||
expect(zx.NumAsString.parse('-3')).toBe(-3); | ||
expect(zx.numAsString().parse('-3')).toBe(-3); | ||
}); | ||
test('throws on non-number string', () => { | ||
expect(() => zx.NumAsString.parse('a3')).toThrowError(); | ||
expect(() => zx.numAsString().parse('a3')).toThrowError(); | ||
}); | ||
}); |
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