typedrequest/test/test.browser.ts

46 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-08-04 14:10:47 +00:00
import { expect, tap } from '@push.rocks/tapbundle';
2020-10-09 10:25:28 +00:00
2022-03-24 19:02:58 +00:00
import * as typedrequest from '../ts/index.js';
2020-10-09 10:25:28 +00:00
let testTypedHandler: typedrequest.TypedHandler<ITestReqRes>;
// lets define an interface
interface ITestReqRes {
method: 'hi';
request: {
name: string;
};
response: {
surname: string;
};
}
tap.test('should create a typedHandler', async () => {
// lets use the interface in a TypedHandler
testTypedHandler = new typedrequest.TypedHandler<ITestReqRes>('hi', async (reqArg) => {
return {
surname: 'wow',
};
});
});
tap.test('should define a testHandler', async () => {
const testTypedRouter = new typedrequest.TypedRouter(); // typed routers can broker typedrequests between handlers
testTypedRouter.addTypedHandler(testTypedHandler);
});
tap.test('should fire a request', async () => {
const typedRequest = new typedrequest.TypedRequest<ITestReqRes>(
2024-02-29 21:47:53 +00:00
'http://localhost:3000/typedrequest',
2020-10-09 10:25:28 +00:00
'hi'
);
2024-02-29 21:47:53 +00:00
const result = await typedRequest.fire({
name: 'yes',
}).catch(err => {
console.log(err);
});
console.log(result);
2020-10-09 10:25:28 +00:00
});
tap.start();