-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: ValidateMessage can throw an error
Fixes: #10
- Loading branch information
1 parent
3db8186
commit d00dcdd
Showing
4 changed files
with
54 additions
and
4 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
48 changes: 48 additions & 0 deletions
48
packages/daf-core/src/message/__tests__/abstract-message-validator.test.ts
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,48 @@ | ||
import { AbstractMessageValidator, unsupportedMessageTypeError } from '../abstract-message-validator' | ||
import { Core } from '../../core' | ||
import { Message } from '../message' | ||
|
||
class MockMessageValidator extends AbstractMessageValidator { | ||
async validate(message: Message, core: Core) { | ||
if (message.raw === 'mock') { | ||
message.type = 'mock' | ||
return message | ||
} | ||
return super.validate(message, core) | ||
} | ||
} | ||
|
||
class MockMessageValidatorWithError extends AbstractMessageValidator { | ||
async validate(message: Message, core: Core) { | ||
// This simulates a scenario when validation process encounters an error, | ||
// such as a network error | ||
|
||
throw Error('Network error') | ||
|
||
return message | ||
} | ||
} | ||
|
||
it('should return a promise and resolve it if the massage is of known type', async () => { | ||
const msg = new Message({ raw: 'mock', meta: { type: 'test' } }) | ||
const validator = new MockMessageValidator() | ||
const validated = await validator.validate(msg, null) | ||
expect(validated.type).toEqual('mock') | ||
expect(validated.isValid()).toEqual(true) | ||
}) | ||
|
||
it('should return a promise and reject it if the massage is of unknown type', async () => { | ||
const msg = new Message({ raw: 'unknown', meta: { type: 'test2' } }) | ||
const validator = new MockMessageValidator() | ||
await expect(validator.validate(msg, null)).rejects.toEqual(unsupportedMessageTypeError) | ||
}) | ||
|
||
it('can throw an error', async () => { | ||
const msg = new Message({ raw: 'mock', meta: { type: 'test3' } }) | ||
const validator = new MockMessageValidatorWithError() | ||
try { | ||
const validated = await validator.validate(msg, null) | ||
} catch (e) { | ||
expect(e !== unsupportedMessageTypeError).toEqual(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