-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feature/group-invite-link
- Loading branch information
Showing
140 changed files
with
1,975 additions
and
819 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,20 @@ | ||
module.exports = { | ||
preset: "ts-jest", | ||
testEnvironment: "jsdom", | ||
moduleNameMapper: { | ||
"^@/(.*)$": "<rootDir>/src/$1", | ||
}, | ||
transform: { | ||
"^.+\\.(js|jsx|ts|tsx)$": "ts-jest", | ||
}, | ||
testMatch: [ | ||
"<rootDir>/src/**/__tests__/**/*.{js,jsx,ts,tsx}", | ||
"<rootDir>/src/**/*.{spec,test}.{js,jsx,ts,tsx}", | ||
], | ||
moduleFileExtensions: ["js", "jsx", "ts", "tsx"], | ||
moduleNameMapper: { | ||
"^@mwdb-web/commons/(.*)$": "<rootDir>/src/commons/$1", | ||
"^@mwdb-web/plugins$": "<rootDir>/src/mocks/plugins.ts", | ||
}, | ||
resetMocks: true, | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
45 changes: 45 additions & 0 deletions
45
mwdb/web/src/__tests__/hooks/useCheckCapabilities.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { renderHook } from "@testing-library/react"; | ||
import { useCheckCapabilities } from "@mwdb-web/commons/hooks"; | ||
import { AuthContextValues, Capability } from "@mwdb-web/types/types"; | ||
import { AuthContext } from "@mwdb-web/commons/auth"; | ||
import { AuthProviderProps } from "@mwdb-web/types/props"; | ||
|
||
describe("useCheckCapabilities", () => { | ||
const authContextValue = { | ||
user: { | ||
capabilities: [ | ||
Capability.accessAllObjects, | ||
Capability.accessAllObjects, | ||
Capability.addingTags, | ||
], | ||
}, | ||
} as AuthContextValues; | ||
|
||
const wrapperWithInitValues = | ||
(initValues: AuthContextValues) => (props: AuthProviderProps) => | ||
( | ||
<AuthContext.Provider value={initValues}> | ||
{props.children} | ||
</AuthContext.Provider> | ||
); | ||
|
||
it("should return true if user has the capability", () => { | ||
const wrapper = wrapperWithInitValues(authContextValue); | ||
const { result } = renderHook(() => useCheckCapabilities(), { | ||
wrapper, | ||
}); | ||
const { userHasCapabilities } = result.current; | ||
|
||
expect(userHasCapabilities(Capability.accessAllObjects)).toBe(true); | ||
}); | ||
|
||
it("should return false if user doesn't have the capability", () => { | ||
const wrapper = wrapperWithInitValues(authContextValue); | ||
const { result } = renderHook(() => useCheckCapabilities(), { | ||
wrapper, | ||
}); | ||
const { userHasCapabilities } = result.current; | ||
|
||
expect(userHasCapabilities(Capability.removingKarton)).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import React from "react"; | ||
import { renderHook, act } from "@testing-library/react"; | ||
import { useComponentState } from "@mwdb-web/commons/hooks"; | ||
|
||
describe("useComponentState", () => { | ||
test("should initialize with React.Fragment as the default component", () => { | ||
const { result } = renderHook(() => useComponentState()); | ||
expect(result.current.Component).toBe(React.Fragment); | ||
}); | ||
|
||
test("should update the component when setComponent is called", () => { | ||
const { result } = renderHook(() => useComponentState()); | ||
const newComponent = () => <div>New Component</div>; | ||
act(() => { | ||
result.current.setComponent(newComponent); | ||
}); | ||
expect(result.current.Component).toBe(newComponent); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { useNavRedirect } from "@mwdb-web/commons/hooks"; | ||
import { renderHook, act } from "@testing-library/react"; | ||
|
||
const navigateMock = jest.fn(); | ||
const locationMock = { state: { prevLocation: "/" } }; | ||
|
||
jest.mock("react-router-dom", () => ({ | ||
useNavigate: () => navigateMock, | ||
useLocation: () => locationMock, | ||
})); | ||
|
||
describe("useNavRedirect", () => { | ||
beforeEach(() => { | ||
navigateMock.mockClear(); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
test("should call navigate with the provided URL", () => { | ||
const { result } = renderHook(() => useNavRedirect()); | ||
const url = "/redirect"; | ||
act(() => { | ||
result.current.redirectTo(url); | ||
}); | ||
expect(navigateMock).toHaveBeenCalledWith(url); | ||
}); | ||
|
||
test("should call navigate with the previous location", () => { | ||
const { result } = renderHook(() => useNavRedirect()); | ||
act(() => { | ||
result.current.goBackToPrevLocation(); | ||
}); | ||
expect(navigateMock).toHaveBeenCalledWith("/"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import { useViewAlert } from "@mwdb-web/commons/hooks"; | ||
import { | ||
RedirectToAlertProps, | ||
SetAlertProps, | ||
} from "@mwdb-web/commons/hooks/useViewAlert"; | ||
import { renderHook } from "@testing-library/react"; | ||
import { useNavigate, useLocation } from "react-router-dom"; | ||
import { toast } from "react-toastify"; | ||
|
||
jest.mock("react-router-dom", () => ({ | ||
...(jest.requireActual("react-router-dom") as any), | ||
useNavigate: jest.fn(), | ||
useLocation: jest.fn(), | ||
})); | ||
|
||
jest.mock("react-toastify", () => ({ | ||
toast: jest.fn(), | ||
})); | ||
|
||
const mockNavigate = jest.fn(); | ||
const mockLocation = { | ||
pathname: "/", | ||
search: "", | ||
state: {}, | ||
}; | ||
|
||
describe("useViewAlert", () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(useNavigate as jest.Mock).mockReturnValue(mockNavigate); | ||
(useLocation as jest.Mock).mockReturnValue(mockLocation); | ||
}); | ||
|
||
test("should set alert and navigate with state", () => { | ||
const { result } = renderHook(() => useViewAlert()); | ||
|
||
const setAlertProps: SetAlertProps = { | ||
success: "Success message", | ||
state: { additionalState: true }, | ||
}; | ||
|
||
result.current.setAlert(setAlertProps); | ||
|
||
expect(toast).toHaveBeenCalledWith("Success message", { | ||
type: "success", | ||
}); | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith( | ||
{ pathname: "/", search: "" }, | ||
{ replace: true, state: { additionalState: true } } | ||
); | ||
}); | ||
|
||
test("should set error alert and navigate without state", () => { | ||
const { result } = renderHook(() => useViewAlert()); | ||
|
||
const setAlertProps = { | ||
error: { | ||
response: { | ||
data: { | ||
message: "Error message", | ||
}, | ||
}, | ||
}, | ||
} as SetAlertProps; | ||
|
||
result.current.setAlert(setAlertProps); | ||
|
||
expect(toast).toHaveBeenCalledWith("Error message", { type: "error" }); | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith( | ||
{ pathname: "/", search: "" }, | ||
{ replace: true, state: {} } | ||
); | ||
}); | ||
|
||
test("should set error as a string alert", () => { | ||
const { result } = renderHook(() => useViewAlert()); | ||
|
||
const setAlertProps: SetAlertProps = { | ||
error: "String Error Message", | ||
}; | ||
|
||
result.current.setAlert(setAlertProps); | ||
|
||
expect(toast).toHaveBeenCalledWith("String Error Message", { | ||
type: "error", | ||
}); | ||
}); | ||
|
||
test("should redirect with alert and state", () => { | ||
const { result } = renderHook(() => useViewAlert()); | ||
|
||
const redirectToAlertProps: RedirectToAlertProps = { | ||
warning: "Warning message", | ||
target: "/target", | ||
state: { additionalState: true }, | ||
}; | ||
|
||
result.current.redirectToAlert(redirectToAlertProps); | ||
|
||
expect(toast).toHaveBeenCalledWith("Warning message", { | ||
type: "warning", | ||
}); | ||
|
||
expect(mockNavigate).toHaveBeenCalledWith("/target", { | ||
state: { additionalState: true }, | ||
}); | ||
}); | ||
}); |
31 changes: 2 additions & 29 deletions
31
mwdb/web/src/commons/auth/capabilities.jsx → mwdb/web/src/commons/auth/capabilities.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { AuthContextValues } from "@mwdb-web/types/types"; | ||
import React from "react"; | ||
|
||
export const AuthContext = React.createContext<AuthContextValues>( | ||
{} as AuthContextValues | ||
); |
2 changes: 1 addition & 1 deletion
2
mwdb/web/src/commons/auth/index.jsx → mwdb/web/src/commons/auth/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { AuthContext } from "./context"; | ||
export { AuthProvider, localStorageAuthKey } from "./provider"; | ||
export { Capability, capabilitiesList } from "./capabilities"; | ||
export { capabilitiesList } from "./capabilities"; |
Oops, something went wrong.