forked from bluesky-social/atproto
-
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.
test (api, common, common-web): add further tests (bluesky-social#950)
- Loading branch information
Showing
6 changed files
with
576 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,89 @@ | ||
import { check } from '../src/index' | ||
import { ZodError } from 'zod' | ||
|
||
describe('check', () => { | ||
describe('is', () => { | ||
it('checks object against definition', () => { | ||
const checkable: check.Checkable<boolean> = { | ||
parse(obj) { | ||
return Boolean(obj) | ||
}, | ||
safeParse(obj) { | ||
return { | ||
success: true, | ||
data: Boolean(obj), | ||
} | ||
}, | ||
} | ||
|
||
expect(check.is(true, checkable)).toBe(true) | ||
}) | ||
|
||
it('handles failed checks', () => { | ||
const checkable: check.Checkable<boolean> = { | ||
parse(obj) { | ||
return Boolean(obj) | ||
}, | ||
safeParse() { | ||
return { | ||
success: false, | ||
error: new ZodError([]), | ||
} | ||
}, | ||
} | ||
|
||
expect(check.is(true, checkable)).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('assure', () => { | ||
it('returns value on success', () => { | ||
const checkable: check.Checkable<boolean> = { | ||
parse(obj) { | ||
return Boolean(obj) | ||
}, | ||
safeParse(obj) { | ||
return { | ||
success: true, | ||
data: Boolean(obj), | ||
} | ||
}, | ||
} | ||
|
||
expect(check.assure(checkable, true)).toEqual(true) | ||
}) | ||
|
||
it('throws on failure', () => { | ||
const err = new Error('foo') | ||
const checkable: check.Checkable<boolean> = { | ||
parse() { | ||
throw err | ||
}, | ||
safeParse() { | ||
throw err | ||
}, | ||
} | ||
|
||
expect(() => check.assure(checkable, true)).toThrow(err) | ||
}) | ||
}) | ||
|
||
describe('isObject', () => { | ||
const falseTestValues: unknown[] = [null, undefined, 'foo', 123, true] | ||
|
||
for (const obj of falseTestValues) { | ||
it(`returns false for ${obj}`, () => { | ||
expect(check.isObject(obj)).toBe(false) | ||
}) | ||
} | ||
|
||
it('returns true for objects', () => { | ||
expect(check.isObject({})).toBe(true) | ||
}) | ||
|
||
it('returns true for instances of classes', () => { | ||
const obj = new (class {})() | ||
expect(check.isObject(obj)).toBe(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
Oops, something went wrong.