mirror of
https://github.com/jambonz/jambonz-webapp.git
synced 2026-01-25 02:08:19 +00:00
Provide component testing utils
This commit is contained in:
5
cypress/fixtures/userLogin.json
Normal file
5
cypress/fixtures/userLogin.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"token": "327a2d17-ce9b-45a7-b0ff-a556536d27fb",
|
||||
"user_sid": "78131ad5-f041-4d5d-821c-47b2d8c6d015",
|
||||
"force_change": false
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
import React from "react";
|
||||
import { H1 } from "jambonz-ui";
|
||||
|
||||
import { StateProvider } from "src/store";
|
||||
import { TestProvider } from "src/test";
|
||||
import { AccessControl } from "./access-control";
|
||||
|
||||
import type { ACLProps } from "./access-control";
|
||||
@@ -9,13 +9,13 @@ import type { ACLProps } from "./access-control";
|
||||
/** Wrapper to pass different ACLs */
|
||||
const AccessControlTestWrapper = (props: Partial<ACLProps>) => {
|
||||
return (
|
||||
<StateProvider>
|
||||
<TestProvider>
|
||||
<AccessControl acl={props.acl!}>
|
||||
<div className="acl-div">
|
||||
<H1>ACL: {props.acl}</H1>
|
||||
</div>
|
||||
</AccessControl>
|
||||
</StateProvider>
|
||||
</TestProvider>
|
||||
);
|
||||
};
|
||||
|
||||
|
||||
38
src/components/require-auth.cy.tsx
Normal file
38
src/components/require-auth.cy.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import React from "react";
|
||||
import { H1 } from "jambonz-ui";
|
||||
|
||||
import { TestProvider } from "src/test";
|
||||
import { RequireAuth } from "./require-auth";
|
||||
|
||||
import type { AuthStateContext } from "src/router/auth";
|
||||
|
||||
/** Wrapper to pass different auth contexts */
|
||||
const RequireAuthTestWrapper = (props: Partial<AuthStateContext>) => {
|
||||
return (
|
||||
<TestProvider {...(props as AuthStateContext)}>
|
||||
<RequireAuth>
|
||||
<div className="acl-div">
|
||||
<H1>Protected Route</H1>
|
||||
</div>
|
||||
</RequireAuth>
|
||||
</TestProvider>
|
||||
);
|
||||
};
|
||||
|
||||
describe("<RequireAuth>", () => {
|
||||
it("mounts", () => {
|
||||
cy.mount(<RequireAuthTestWrapper />);
|
||||
});
|
||||
|
||||
it("is not authorized", () => {
|
||||
cy.mount(<RequireAuthTestWrapper />);
|
||||
|
||||
cy.get(".acl-div").should("not.exist");
|
||||
});
|
||||
|
||||
it("is authorized", () => {
|
||||
cy.mount(<RequireAuthTestWrapper authorized />);
|
||||
|
||||
cy.get(".acl-div").should("exist");
|
||||
});
|
||||
});
|
||||
@@ -25,7 +25,7 @@ interface SignIn {
|
||||
(username: string, password: string): Promise<UserLogin>;
|
||||
}
|
||||
|
||||
interface AuthStateContext {
|
||||
export interface AuthStateContext {
|
||||
token: string;
|
||||
signin: SignIn;
|
||||
signout: () => void;
|
||||
|
||||
43
src/test/index.tsx
Normal file
43
src/test/index.tsx
Normal file
@@ -0,0 +1,43 @@
|
||||
import React from "react";
|
||||
import { BrowserRouter, Route, Routes } from "react-router-dom";
|
||||
|
||||
import { StateProvider } from "src/store";
|
||||
import { AuthContext } from "src/router/auth";
|
||||
import { MSG_SOMETHING_WRONG } from "src/constants";
|
||||
|
||||
import type { AuthStateContext } from "src/router/auth";
|
||||
|
||||
import userLogin from "../../cypress/fixtures/userLogin.json";
|
||||
|
||||
interface TestProviderProps extends Partial<AuthStateContext> {
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
export const signinError = () => Promise.reject(MSG_SOMETHING_WRONG);
|
||||
export const signinSuccess = () => Promise.resolve(userLogin);
|
||||
export const signout = () => undefined;
|
||||
export const authProps: AuthStateContext = {
|
||||
token: "",
|
||||
signin: signinSuccess,
|
||||
signout,
|
||||
authorized: false,
|
||||
};
|
||||
|
||||
export const TestProvider = ({ children, ...restProps }: TestProviderProps) => {
|
||||
return (
|
||||
<StateProvider>
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
...authProps,
|
||||
...restProps,
|
||||
}}
|
||||
>
|
||||
<BrowserRouter>
|
||||
<Routes>
|
||||
<Route path="*" element={children} />
|
||||
</Routes>
|
||||
</BrowserRouter>
|
||||
</AuthContext.Provider>
|
||||
</StateProvider>
|
||||
);
|
||||
};
|
||||
Reference in New Issue
Block a user