Created
July 12, 2023 19:51
-
-
Save StephanMeijer/1711349b7d738ad233825feb1683f24f to your computer and use it in GitHub Desktop.
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
import { describe, test } from '@jest/globals'; | |
import { SchemaMatcher } from '../__testUtils__/zod/SchemaMatcher.js'; | |
import { ChecksumSchema } from '../../src/checksum/Checksum.js'; | |
import { HashAlgorithmType } from '../../src/checksum/algorithms/HashAlgorithmInterface.js'; | |
import { ZodError } from 'zod'; | |
describe('ChecksumSchema', () => { | |
const expectSchema = new SchemaMatcher(ChecksumSchema); | |
test(`Schema (valid)`, async () => { | |
expectSchema | |
.parsing({ | |
algorithm: HashAlgorithmType.SHA3_512, | |
hash: '123456', | |
}) | |
.toSucceed() | |
.andEqualInputValue(); | |
}); | |
test(`schema (invalid algorithm)`, async () => { | |
expectSchema | |
.parsing({ | |
algorithm: 'invalid', | |
hash: '123456', | |
}) | |
.toFail() | |
.withError( | |
new ZodError([ | |
{ | |
received: 'invalid', | |
code: 'invalid_enum_value', | |
options: ['blake2b512', 'sha1', 'sha512', 'sha3-512'], | |
path: ['algorithm'], | |
message: | |
"Invalid enum value. Expected 'blake2b512' | 'sha1' | 'sha512' | 'sha3-512', received 'invalid'", | |
}, | |
]), | |
); | |
}); | |
test(`schema (missing property hash)`, async () => { | |
expectSchema | |
.parsing({ | |
algorithm: HashAlgorithmType.SHA3_512, | |
}) | |
.toFail() | |
.withError( | |
new ZodError([ | |
{ | |
code: 'invalid_type', | |
expected: 'string', | |
received: 'undefined', | |
path: ['hash'], | |
message: 'Required', | |
}, | |
]), | |
); | |
}); | |
test(`schema (missing property 'algorithm')`, async () => { | |
expectSchema | |
.parsing({ | |
hash: '123456', | |
}) | |
.toFail() | |
.withError( | |
new ZodError([ | |
{ | |
expected: | |
"'blake2b512' | 'sha1' | 'sha512' | 'sha3-512'" as 'string', | |
received: 'undefined', | |
code: 'invalid_type', | |
path: ['algorithm'], | |
message: 'Required', | |
}, | |
]), | |
); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment