@whook/whook
Version:
Build strong and efficient REST web services.
228 lines • 5.52 kB
JavaScript
import { describe, test, beforeEach, jest, expect } from '@jest/globals';
import initHandlerCommand from './route.js';
import { YError } from 'yerror';
describe('routeCommand', () => {
const log = jest.fn();
const $injector = jest.fn();
beforeEach(() => {
log.mockReset();
$injector.mockReset();
});
describe('should work', () => {
test('with all parameters', async () => {
$injector.mockResolvedValueOnce({
putEcho: async ({ body }) => ({
status: 200,
body,
}),
});
const routeCommand = await initHandlerCommand({
log,
$injector,
});
await routeCommand({
command: 'whook',
rest: ['route'],
namedArguments: {
name: 'putEcho',
parameters: '{"body": {"echo": "YOLO!"} }',
},
});
expect({
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
injectorCalls: $injector.mock.calls,
}).toMatchInlineSnapshot(`
{
"injectorCalls": [
[
[
"putEcho",
],
],
],
"logCalls": [
[
"debug",
"route",
"putEcho",
],
[
"debug",
"parameters",
{
"body": {
"echo": "YOLO!",
},
},
],
[
"info",
"{
"status": 200,
"body": {
"echo": "YOLO!"
}
}",
],
],
}
`);
});
test('with route only', async () => {
$injector.mockResolvedValueOnce({
getPing: async ({ body }) => ({
status: 200,
body,
}),
});
const routeCommand = await initHandlerCommand({
log,
$injector,
});
await routeCommand({
command: 'whook',
rest: ['route'],
namedArguments: {
name: 'getPing',
},
});
expect({
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
injectorCalls: $injector.mock.calls,
}).toMatchInlineSnapshot(`
{
"injectorCalls": [
[
[
"getPing",
],
],
],
"logCalls": [
[
"debug",
"route",
"getPing",
],
[
"debug",
"parameters",
{},
],
[
"info",
"{
"status": 200
}",
],
],
}
`);
});
});
describe('should fail', () => {
test('with non JSON parameters', async () => {
$injector.mockResolvedValueOnce({
putEcho: async ({ body }) => ({
status: 200,
body,
}),
});
const routeCommand = await initHandlerCommand({
log,
$injector,
});
try {
await routeCommand({
command: 'whook',
rest: ['route'],
namedArguments: {
name: 'putEcho',
parameters: '{"body: {"echo": "YOLO!"} }',
},
});
throw new YError('E_UNEXPECTED_SUCCESS');
}
catch (err) {
expect({
errorCode: err.code,
errorParams: err.params.slice(0, -1),
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
injectorCalls: $injector.mock.calls,
}).toMatchInlineSnapshot(`
{
"errorCode": "E_BAD_PARAMETERS",
"errorParams": [
"{"body: {"echo": "YOLO!"} }",
],
"injectorCalls": [],
"logCalls": [],
}
`);
}
});
test('with a failing route', async () => {
$injector.mockResolvedValueOnce({
putEcho: async () => {
throw new YError('E_HANDLER_ERROR');
},
});
const routeCommand = await initHandlerCommand({
log,
$injector,
});
try {
await routeCommand({
command: 'whook',
rest: ['route'],
namedArguments: {
name: 'putEcho',
parameters: '{"body": {"echo": "YOLO!"} }',
},
});
throw new YError('E_UNEXPECTED_SUCCESS');
}
catch (err) {
expect({
errorCode: err.code,
errorParams: err.params,
logCalls: log.mock.calls.filter(([type]) => !type.endsWith('stack')),
injectorCalls: $injector.mock.calls,
}).toMatchInlineSnapshot(`
{
"errorCode": "E_UNEXPECTED_SUCCESS",
"errorParams": [],
"injectorCalls": [
[
[
"putEcho",
],
],
],
"logCalls": [
[
"debug",
"route",
"putEcho",
],
[
"debug",
"parameters",
{
"body": {
"echo": "YOLO!",
},
},
],
[
"error",
"Got an error while running the route.",
],
],
}
`);
}
});
});
});
//# sourceMappingURL=route.test.js.map