merchant-backoffice

ZZZ: Inactive/Deprecated
Log | Files | Refs | Submodules | README

commit 010ec79d14a7f3a38942b0852db06a68e064d784
parent 0dc4e08923a9c4ee743898b6590a28c8151b160e
Author: Sebastian <sebasjm@gmail.com>
Date:   Mon, 13 Dec 2021 11:58:48 -0300

reserve

Diffstat:
Mpackages/merchant-backoffice/tests/axiosMock.ts | 7+++++++
Dpackages/merchant-backoffice/tests/hooks/swr/reserve-create.test.tsx | 152-------------------------------------------------------------------------------
Apackages/merchant-backoffice/tests/hooks/swr/reserve.test.ts | 148+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 155 insertions(+), 152 deletions(-)

diff --git a/packages/merchant-backoffice/tests/axiosMock.ts b/packages/merchant-backoffice/tests/axiosMock.ts @@ -211,6 +211,13 @@ export const API_CREATE_RESERVE: Query< post: "http://backend/instances/default/private/reserves", }; +export const API_DELETE_RESERVE: Query< + MerchantBackend.Tips.ReserveCreateRequest, + MerchantBackend.Tips.ReserveCreateConfirmation +> = { + delete: "http://backend/instances/default/private/reserves", +}; + export const API_CREATE_ORDER: Query< MerchantBackend.Orders.PostOrderRequest, MerchantBackend.Orders.PostOrderResponse diff --git a/packages/merchant-backoffice/tests/hooks/swr/reserve-create.test.tsx b/packages/merchant-backoffice/tests/hooks/swr/reserve-create.test.tsx @@ -1,152 +0,0 @@ -/* - This file is part of GNU Taler - (C) 2021 Taler Systems S.A. - - GNU Taler is free software; you can redistribute it and/or modify it under the - terms of the GNU General Public License as published by the Free Software - Foundation; either version 3, or (at your option) any later version. - - GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR - A PARTICULAR PURPOSE. See the GNU General Public License for more details. - - You should have received a copy of the GNU General Public License along with - GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> - */ - -/** - * - * @author Sebastian Javier Marchano (sebasjm) - */ - -import { renderHook } from "@testing-library/preact-hooks"; -import { act } from "preact/test-utils"; -import * as backend from "../../../src/context/backend"; -import * as instance from "../../../src/context/instance"; -import { MerchantBackend } from "../../../src/declaration"; -import { - useInstanceReserves, - useReservesAPI, -} from "../../../src/hooks/reserves"; -import { - API_CREATE_PRODUCT, - API_LIST_PRODUCTS, - API_GET_PRODUCT_BY_ID, - AxiosMockEnvironment, - assertJustExpectedRequestWereMade, - API_LIST_RESERVES, - API_CREATE_RESERVE, - API_GET_RESERVE_BY_ID, -} from "../../axiosMock"; - -jest.mock("axios"); - -describe("reserve create api", () => { - beforeEach(() => { - jest - .spyOn(backend, "useBackendContext") - .mockImplementation( - () => ({ url: "http://backend", token: "token" } as any) - ); - jest - .spyOn(instance, "useInstanceContext") - .mockImplementation( - () => ({ token: "token", id: "default", admin: true } as any) - ); - }); - - it("should mutate list cache when creating new reserve", async () => { - const env = new AxiosMockEnvironment(); - - env.addRequestExpectation(API_LIST_RESERVES, { - response: { - reserves: [ - { - reserve_pub: "11", - } as MerchantBackend.Tips.ReserveStatusEntry, - ], - }, - }); - - const { result, waitForNextUpdate } = renderHook(() => { - const api = useReservesAPI(); - const query = useInstanceReserves(); - - return { query, api }; - }); - - if (!result.current) { - expect(result.current).toBeDefined(); - return; - } - expect(result.current.query.loading).toBeTruthy(); - - await waitForNextUpdate(); - - assertJustExpectedRequestWereMade(env); - - expect(result.current.query.loading).toBeFalsy(); - expect(result.current?.query.ok).toBeTruthy(); - if (!result.current?.query.ok) return; - - expect(result.current.query.data).toEqual({ - reserves: [{ reserve_pub: "11" }], - }); - - env.addRequestExpectation(API_CREATE_RESERVE, { - request: { - initial_balance: "ARS:3333", - exchange_url: "http://url", - wire_method: "iban", - }, - response: { - reserve_pub: "22", - payto_uri: "payto", - }, - }); - - act(async () => { - await result.current?.api.createReserve({ - initial_balance: "ARS:3333", - exchange_url: "http://url", - wire_method: "iban", - }); - return; - }); - - assertJustExpectedRequestWereMade(env); - - env.addRequestExpectation(API_LIST_RESERVES, { - response: { - reserves: [ - { - reserve_pub: "11", - } as MerchantBackend.Tips.ReserveStatusEntry, - { - reserve_pub: "22", - } as MerchantBackend.Tips.ReserveStatusEntry, - ], - }, - }); - - expect(result.current.query.loading).toBeFalsy(); - - await waitForNextUpdate(); - - assertJustExpectedRequestWereMade(env); - - expect(result.current.query.loading).toBeFalsy(); - expect(result.current.query.ok).toBeTruthy(); - - expect(result.current.query.data).toEqual({ - reserves: [ - { - reserve_pub: "11", - } as MerchantBackend.Tips.ReserveStatusEntry, - { - reserve_pub: "22", - } as MerchantBackend.Tips.ReserveStatusEntry, - ], - }); - }); -}); diff --git a/packages/merchant-backoffice/tests/hooks/swr/reserve.test.ts b/packages/merchant-backoffice/tests/hooks/swr/reserve.test.ts @@ -0,0 +1,148 @@ +/* + This file is part of GNU Taler + (C) 2021 Taler Systems S.A. + + GNU Taler is free software; you can redistribute it and/or modify it under the + terms of the GNU General Public License as published by the Free Software + Foundation; either version 3, or (at your option) any later version. + + GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY + WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR + A PARTICULAR PURPOSE. See the GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along with + GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> + */ + +/** + * + * @author Sebastian Javier Marchano (sebasjm) + */ + +import { renderHook } from "@testing-library/preact-hooks"; +import { act } from "preact/test-utils"; +import * as backend from "../../../src/context/backend"; +import * as instance from "../../../src/context/instance"; +import { MerchantBackend } from "../../../src/declaration"; +import { + useInstanceReserves, + useReservesAPI +} from "../../../src/hooks/reserves"; +import { + API_CREATE_RESERVE, API_LIST_RESERVES, assertJustExpectedRequestWereMade, AxiosMockEnvironment +} from "../../axiosMock"; + +jest.mock("axios"); + +describe("reserve api", () => { + beforeEach(() => { + jest + .spyOn(backend, "useBackendContext") + .mockImplementation( + () => ({ url: "http://backend", token: "token" } as any) + ); + jest + .spyOn(instance, "useInstanceContext") + .mockImplementation( + () => ({ token: "token", id: "default", admin: true } as any) + ); + }); + + it("should mutate list cache when creating a reserve", async () => { + const env = new AxiosMockEnvironment(); + + env.addRequestExpectation(API_LIST_RESERVES, { + response: { + reserves: [ + { + reserve_pub: "11", + } as MerchantBackend.Tips.ReserveStatusEntry, + ], + }, + }); + + const { result, waitForNextUpdate } = renderHook(() => { + const api = useReservesAPI(); + const query = useInstanceReserves(); + + return { query, api }; + }); + + if (!result.current) { + expect(result.current).toBeDefined(); + return; + } + expect(result.current.query.loading).toBeTruthy(); + + await waitForNextUpdate(); + + assertJustExpectedRequestWereMade(env); + + expect(result.current.query.loading).toBeFalsy(); + expect(result.current?.query.ok).toBeTruthy(); + if (!result.current?.query.ok) return; + + expect(result.current.query.data).toEqual({ + reserves: [{ reserve_pub: "11" }], + }); + + env.addRequestExpectation(API_CREATE_RESERVE, { + request: { + initial_balance: "ARS:3333", + exchange_url: "http://url", + wire_method: "iban", + }, + response: { + reserve_pub: "22", + payto_uri: "payto", + }, + }); + + act(async () => { + await result.current?.api.createReserve({ + initial_balance: "ARS:3333", + exchange_url: "http://url", + wire_method: "iban", + }); + return; + }); + + assertJustExpectedRequestWereMade(env); + + env.addRequestExpectation(API_LIST_RESERVES, { + response: { + reserves: [ + { + reserve_pub: "11", + } as MerchantBackend.Tips.ReserveStatusEntry, + { + reserve_pub: "22", + } as MerchantBackend.Tips.ReserveStatusEntry, + ], + }, + }); + + expect(result.current.query.loading).toBeFalsy(); + + await waitForNextUpdate(); + + assertJustExpectedRequestWereMade(env); + + expect(result.current.query.loading).toBeFalsy(); + expect(result.current.query.ok).toBeTruthy(); + + expect(result.current.query.data).toEqual({ + reserves: [ + { + reserve_pub: "11", + } as MerchantBackend.Tips.ReserveStatusEntry, + { + reserve_pub: "22", + } as MerchantBackend.Tips.ReserveStatusEntry, + ], + }); + }); + + + +});