-
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.
- Loading branch information
Showing
4 changed files
with
228 additions
and
126 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 |
---|---|---|
@@ -1,54 +1,80 @@ | ||
const attributeTypes = require('./attribute-types'); | ||
|
||
function testType(t, invalidInputs, fixtures) { | ||
for([input,warnings,errors] of invalidInputs) { | ||
expect(t.validate(input)).toMatchObject([warnings, errors]); | ||
} | ||
|
||
for ([input, output] of fixtures) { | ||
expect(t.validate(input)).toMatchObject([[],[]]); | ||
expect(t.translate(input)).toEqual(output); | ||
} | ||
}; | ||
|
||
test('basic string', () => { | ||
const invalidInputs = [ | ||
[undefined, [], ["A value must be provided"]], | ||
["", [], ["A value must be provided"]] | ||
]; | ||
|
||
const fixtures = [ | ||
["foo", "foo"], | ||
[123, "123"], | ||
]; | ||
|
||
testType(attributeTypes.basicStringType, invalidInputs, fixtures); | ||
test('required basic string', () => { | ||
// Also tests the workings of the requiredType system | ||
const t = attributeTypes.requiredType(attributeTypes.basicStringType); | ||
|
||
let r = t(undefined); | ||
expect(r.valid).toBeFalsy(); | ||
expect(r.warnings).toMatchObject([]); | ||
expect(r.errors).toMatchObject(["A value must be provided"]); | ||
|
||
r = t(""); | ||
expect(r.valid).toBeFalsy(); | ||
expect(r.warnings).toMatchObject([]); | ||
expect(r.errors).toMatchObject(["A value must be provided"]); | ||
|
||
r = t("foo"); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual("foo"); | ||
expect(r.warnings).toMatchObject([]); | ||
|
||
r = t(123); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual("123"); | ||
expect(r.warnings).toMatchObject([]); | ||
}); | ||
|
||
test('optional basic string', () => { | ||
const invalidInputs = []; | ||
// Also tests the workings of the optionalType system | ||
const t = attributeTypes.optionalType(attributeTypes.basicStringType); | ||
|
||
let r = t(undefined); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.warnings).toMatchObject([]); | ||
expect(r.value).toEqual(undefined); | ||
|
||
r = t(""); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.warnings).toMatchObject([]); | ||
expect(r.value).toEqual(undefined); | ||
|
||
r = t("foo"); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual("foo"); | ||
expect(r.warnings).toMatchObject([]); | ||
|
||
r = t(123); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual("123"); | ||
expect(r.warnings).toMatchObject([]); | ||
}); | ||
|
||
test('required basic number', () => { | ||
const t = attributeTypes.requiredType(attributeTypes.basicNumberType); | ||
|
||
const fixtures = [ | ||
["foo", "foo"], | ||
[123, "123"], | ||
["", undefined], | ||
[undefined, undefined] | ||
]; | ||
r = t("foo"); | ||
expect(r.valid).toBeFalsy(); | ||
expect(r.warnings).toMatchObject([]); | ||
expect(r.errors).toMatchObject(["This is not a valid number"]); | ||
|
||
testType(attributeTypes.optionalType(attributeTypes.basicStringType), invalidInputs, fixtures); | ||
r = t(123); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual(123); | ||
expect(r.warnings).toMatchObject([]); | ||
}); | ||
|
||
test('basic number', () => { | ||
const invalidInputs = [ | ||
[undefined, [], ["A value must be provided"]], | ||
["", [], ["A value must be provided"]], | ||
["foo", [], ["foo is not a valid number"]] | ||
]; | ||
test('optional basic number', () => { | ||
// Also tests that optionalType converts errors into warnings | ||
const t = attributeTypes.optionalType(attributeTypes.basicNumberType); | ||
|
||
const fixtures = [ | ||
[123, 123], | ||
["123", "123"], | ||
]; | ||
r = t("foo"); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toBeUndefined(); | ||
expect(r.warnings).toMatchObject(["This is not a valid number"]); | ||
|
||
testType(attributeTypes.basicNumberType, invalidInputs, fixtures); | ||
r = t(123); | ||
expect(r.valid).toBeTruthy(); | ||
expect(r.value).toEqual(123); | ||
expect(r.warnings).toMatchObject([]); | ||
}); |
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
Oops, something went wrong.