diff options
author | Sebastian <sebasjm@gmail.com> | 2023-10-19 02:45:36 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2023-10-19 02:56:15 -0300 |
commit | a67518ab1a865fc79374a19bce6513b0caa2eab6 (patch) | |
tree | acc3943435b31c503f27383c824cbe5280e9ee9a /packages/merchant-backoffice-ui | |
parent | c0dd59db4f6217ddd963237a24ee2558b7d0b2c0 (diff) | |
download | wallet-core-a67518ab1a865fc79374a19bce6513b0caa2eab6.tar.xz |
update testing hook
Diffstat (limited to 'packages/merchant-backoffice-ui')
-rw-r--r-- | packages/merchant-backoffice-ui/src/hooks/testing.tsx | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/packages/merchant-backoffice-ui/src/hooks/testing.tsx b/packages/merchant-backoffice-ui/src/hooks/testing.tsx index 847d512b0..386944854 100644 --- a/packages/merchant-backoffice-ui/src/hooks/testing.tsx +++ b/packages/merchant-backoffice-ui/src/hooks/testing.tsx @@ -21,11 +21,13 @@ import { MockEnvironment } from "@gnu-taler/web-util/testing"; import { ComponentChildren, FunctionalComponent, h, VNode } from "preact"; +import { HttpRequestLibrary, HttpRequestOptions, HttpResponse } from "@gnu-taler/taler-util/http"; import { SWRConfig } from "swr"; import { ApiContextProvider } from "@gnu-taler/web-util/browser"; import { BackendContextProvider } from "../context/backend.js"; import { InstanceContextProvider } from "../context/instance.js"; import { HttpResponseOk, RequestOptions } from "@gnu-taler/web-util/browser"; +import { TalerBankIntegrationHttpClient, TalerCoreBankHttpClient } from "@gnu-taler/taler-util"; export class ApiMockEnvironment extends MockEnvironment { constructor(debug = false) { @@ -47,6 +49,7 @@ export class ApiMockEnvironment extends MockEnvironment { }: { children: ComponentChildren; }): VNode { + async function request<T>( base: string, path: string, @@ -89,6 +92,62 @@ export class ApiMockEnvironment extends MockEnvironment { } const SC: any = SWRConfig; + const mockHttpClient = new class implements HttpRequestLibrary { + async fetch(url: string, options?: HttpRequestOptions | undefined): Promise<HttpResponse> { + const _url = new URL(url); + const mocked = __SAVE_REQUEST_AND_GET_MOCKED_RESPONSE( + { + method: options?.method ?? "GET", + url: _url.href, + }, + { + qparam: _url.searchParams, + auth: options as any, + request: options?.body as any, + }, + ); + const status = mocked.expectedQuery?.query.code ?? 200; + const requestPayload = mocked.expectedQuery?.params?.request; + const responsePayload = mocked.expectedQuery?.params?.response; + + // FIXME: complete this implementation to mock any query + const resp: HttpResponse = { + requestUrl: _url.href, + status: status, + headers: {} as any, + requestMethod: options?.method ?? "GET", + json: async () => responsePayload, + text: async () => responsePayload as any as string, + bytes: async () => responsePayload as ArrayBuffer, + }; + return resp + } + get(url: string, opt?: HttpRequestOptions): Promise<HttpResponse> { + return this.fetch(url, { + method: "GET", + ...opt, + }); + } + + postJson( + url: string, + body: any, + opt?: HttpRequestOptions, + ): Promise<HttpResponse> { + return this.fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + ...opt, + }); + } + + } + const bankCore = new TalerCoreBankHttpClient("", mockHttpClient) + const bankIntegration = bankCore.getIntegrationAPI() + const bankRevenue = bankCore.getRevenueAPI("") + const bankWire = bankCore.getWireGatewayAPI("") + return ( <BackendContextProvider defaultUrl="http://backend"> <InstanceContextProvider @@ -99,7 +158,7 @@ export class ApiMockEnvironment extends MockEnvironment { changeToken: () => null, }} > - <ApiContextProvider value={{ request }}> + <ApiContextProvider value={{ request, bankCore, bankIntegration, bankRevenue, bankWire }}> <SC value={{ loadingTimeout: 0, |