-
Notifications
You must be signed in to change notification settings - Fork 2
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
Hugh Rawlinson
authored and
Hugh Rawlinson
committed
Jun 8, 2022
1 parent
1f0250b
commit 1d89e04
Showing
3 changed files
with
125 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { schema, literal, every, isString, isNumber } from '../src/index'; | ||
|
||
test('fails when array contains elements of an unassignable type', () => { | ||
expect(every(isString)([1, 2, 3])).toBe(false); | ||
expect(every(isNumber)([1, '2', 3])).toBe(false); | ||
}); | ||
|
||
test('passes when array contains correct type', () => { | ||
expect(every(isNumber)([1, 2, 3])).toBe(true); | ||
expect(every(isString)(['1', '2', '3'])).toBe(true); | ||
}); | ||
|
||
test('passes when array is empty', () => { | ||
expect(every(isString)([])).toBe(true); | ||
expect(every(isNumber)([])).toBe(true); | ||
}); | ||
|
||
interface TestInterface { | ||
kind: 'test'; | ||
value: { | ||
nestedString: string; | ||
nestedNumber: number; | ||
}; | ||
} | ||
|
||
const isTestInterface = schema<TestInterface>({ | ||
kind: literal('test'), | ||
value: schema<TestInterface['value']>({ | ||
nestedString: isString, | ||
nestedNumber: isNumber | ||
}) | ||
}); | ||
|
||
test('passes when array contains valid objects', () => { | ||
expect( | ||
every<TestInterface>(isTestInterface)([ | ||
{ | ||
kind: 'test', | ||
value: { | ||
nestedString: 'string', | ||
nestedNumber: 1 | ||
} | ||
} | ||
]) | ||
).toBe(true); | ||
}); | ||
|
||
test('fails when array contains invalid objects', () => { | ||
expect( | ||
every<TestInterface>(isTestInterface)([ | ||
{ | ||
kind: 'test', | ||
value: { | ||
nested_string: 'string' | ||
} | ||
} | ||
]) | ||
).toBe(false); | ||
}); | ||
|
||
every<string>(isString); | ||
|
||
let arr: unknown[] = [1, 2, 3]; | ||
if (every(isNumber)(arr)) { | ||
let typeCheck: number[] = arr; | ||
} | ||
|
||
interface TestInterfaceT { | ||
id: number; | ||
name: string; | ||
} | ||
|
||
const isTestInterfaceT = schema<TestInterfaceT>({ | ||
id: isNumber, | ||
name: isString | ||
}); | ||
|
||
let arr2: unknown[] = [{ id: 1, name: 'aaa' }]; | ||
|
||
if (every(isTestInterfaceT)(arr2)) { | ||
let typeCheck: TestInterfaceT[] = arr2; | ||
} |