diff options
Diffstat (limited to 'packages/merchant-backoffice-ui/src/context')
| -rw-r--r-- | packages/merchant-backoffice-ui/src/context/api.ts (renamed from packages/merchant-backoffice-ui/src/context/fetch.ts) | 33 | ||||
| -rw-r--r-- | packages/merchant-backoffice-ui/src/context/backend.test.ts | 131 | ||||
| -rw-r--r-- | packages/merchant-backoffice-ui/src/context/backend.ts | 3 | 
3 files changed, 145 insertions, 22 deletions
| diff --git a/packages/merchant-backoffice-ui/src/context/fetch.ts b/packages/merchant-backoffice-ui/src/context/api.ts index 88c9bc30c..81586bd35 100644 --- a/packages/merchant-backoffice-ui/src/context/fetch.ts +++ b/packages/merchant-backoffice-ui/src/context/api.ts @@ -19,36 +19,25 @@   * @author Sebastian Javier Marchano (sebasjm)   */ -import { h, createContext, VNode, ComponentChildren } from "preact"; +import { ComponentChildren, createContext, h, VNode } from "preact";  import { useContext } from "preact/hooks"; -import useSWR from "swr"; -import useSWRInfinite from "swr/infinite"; +import { defaultRequestHandler } from "../utils/request.js";  interface Type { -  useSWR: typeof useSWR; -  useSWRInfinite: typeof useSWRInfinite; +  request: typeof defaultRequestHandler;  } -const Context = createContext<Type>({} as Type); +const Context = createContext<Type>({ +  request: defaultRequestHandler, +}); -export const useFetchContext = (): Type => useContext(Context); -export const FetchContextProvider = ({ +export const useApiContext = (): Type => useContext(Context); +export const ApiContextProvider = ({    children, +  value,  }: { +  value: Type;    children: ComponentChildren;  }): VNode => { -  return h(Context.Provider, { value: { useSWR, useSWRInfinite }, children }); -}; - -export const FetchContextProviderTesting = ({ -  children, -  data, -}: { -  children: ComponentChildren; -  data: any; -}): VNode => { -  return h(Context.Provider, { -    value: { useSWR: () => data, useSWRInfinite }, -    children, -  }); +  return h(Context.Provider, { value, children });  }; diff --git a/packages/merchant-backoffice-ui/src/context/backend.test.ts b/packages/merchant-backoffice-ui/src/context/backend.test.ts new file mode 100644 index 000000000..c7fb19293 --- /dev/null +++ b/packages/merchant-backoffice-ui/src/context/backend.test.ts @@ -0,0 +1,131 @@ +/* + This file is part of GNU Taler + (C) 2021-2023 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 { tests } from "@gnu-taler/web-util/lib/index.browser"; +import { ComponentChildren, h, VNode } from "preact"; +import { MerchantBackend } from "../declaration.js"; +import { +  useAdminAPI, +  useInstanceAPI, +  useManagementAPI, +} from "../hooks/instance.js"; +import { expect } from "chai"; +import { ApiMockEnvironment } from "../hooks/testing.js"; +import { API_CREATE_INSTANCE, API_UPDATE_CURRENT_INSTANCE_AUTH, API_UPDATE_INSTANCE_AUTH_BY_ID } from "../hooks/urls.js"; + +interface TestingContextProps { +  children?: ComponentChildren; +} + +describe("backend context api ", () => { + +  it("should use new token after updating the instance token in the settings as user", async () => { +    const env = new ApiMockEnvironment(); + +    const hookBehavior = await tests.hookBehaveLikeThis( +      () => { +        const instance = useInstanceAPI(); +        const management = useManagementAPI("default"); +        const admin = useAdminAPI(); + +        return { instance, management, admin }; +      }, +      {}, +      [ +        ({ instance, management, admin }) => { +          env.addRequestExpectation(API_UPDATE_INSTANCE_AUTH_BY_ID("default"), { +            request: { +              method: "token", +              token: "another_token", +            }, +            response: { +              name: "instance_name", +            } as MerchantBackend.Instances.QueryInstancesResponse, +          }); + +          management.setNewToken("another_token") +        }, +        ({ instance, management, admin }) => { +          expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); + +          env.addRequestExpectation(API_CREATE_INSTANCE, { +            auth: "another_token", +            request: { +              id: "new_instance_id", +            } as MerchantBackend.Instances.InstanceConfigurationMessage, +          }); + +          admin.createInstance({ +            id: "new_instance_id", +          } as MerchantBackend.Instances.InstanceConfigurationMessage); + +        }, +      ], env.buildTestingContext()); + +    expect(hookBehavior).deep.eq({ result: "ok" }); +    expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); +  }); + +  it("should use new token after updating the instance token in the settings as admin", async () => { +    const env = new ApiMockEnvironment(); + +    const hookBehavior = await tests.hookBehaveLikeThis( +      () => { +        const instance = useInstanceAPI(); +        const management = useManagementAPI("default"); +        const admin = useAdminAPI(); + +        return { instance, management, admin }; +      }, +      {}, +      [ +        ({ instance, management, admin }) => { +          env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE_AUTH, { +            request: { +              method: "token", +              token: "another_token", +            }, +            response: { +              name: "instance_name", +            } as MerchantBackend.Instances.QueryInstancesResponse, +          }); +          instance.setNewToken("another_token"); +        }, +        ({ instance, management, admin }) => { +          expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); + +          env.addRequestExpectation(API_CREATE_INSTANCE, { +            auth: "another_token", +            request: { +              id: "new_instance_id", +            } as MerchantBackend.Instances.InstanceConfigurationMessage, +          }); + +          admin.createInstance({ +            id: "new_instance_id", +          } as MerchantBackend.Instances.InstanceConfigurationMessage); +        }, +      ], env.buildTestingContext()); + +    expect(hookBehavior).deep.eq({ result: "ok" }); +    expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); +  }); +}); diff --git a/packages/merchant-backoffice-ui/src/context/backend.ts b/packages/merchant-backoffice-ui/src/context/backend.ts index f8d1bc397..f7f8afea6 100644 --- a/packages/merchant-backoffice-ui/src/context/backend.ts +++ b/packages/merchant-backoffice-ui/src/context/backend.ts @@ -31,6 +31,7 @@ interface BackendContextType {    clearAllTokens: () => void;    addTokenCleaner: (c: () => void) => void;    updateLoginStatus: (url: string, token?: string) => void; +  updateToken: (token?: string) => void;  }  const BackendContext = createContext<BackendContextType>({ @@ -41,6 +42,7 @@ const BackendContext = createContext<BackendContextType>({    clearAllTokens: () => null,    addTokenCleaner: () => null,    updateLoginStatus: () => null, +  updateToken: () => null,  });  function useBackendContextState( @@ -87,6 +89,7 @@ function useBackendContextState(      updateLoginStatus,      resetBackend,      clearAllTokens, +    updateToken,      addTokenCleaner: addTokenCleanerMemo,    };  } | 
