Skip to content

Commit

Permalink
test: Add basic test cases for UserStore
Browse files Browse the repository at this point in the history
  • Loading branch information
DafyddLlyr committed Oct 10, 2023
1 parent 712f916 commit 0cd8a28
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 10 deletions.
4 changes: 1 addition & 3 deletions editor.planx.uk/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -482,9 +482,7 @@ const EditorToolbar: React.FC<{
<ListItemText>
{user.isPlatformAdmin
? `All teams`
: user.teams
.map((team) => team.team.name)
.join(", ")}
: user.teams.map((team) => team.team.name).join(", ")}
</ListItemText>
</MenuItem>
)}
Expand Down
101 changes: 101 additions & 0 deletions editor.planx.uk/src/pages/FlowEditor/lib/__tests__/user.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { User } from "@opensystemslab/planx-core/types";

import { FullStore, vanillaStore } from "../store";

const { getState, setState } = vanillaStore;
const { canUserEditTeam } = getState();

const redUser: User = {
id: 1,
isPlatformAdmin: false,
firstName: "Red",
lastName: "Reddison",
email: "[email protected]",
teams: [
{
role: "teamEditor",
team: {
name: "Red Team",
slug: "red-team",
id: 1,
},
},
{
role: "teamViewer",
team: {
name: "Blue Team",
slug: "blue-team",
id: 1,
},
},
],
};

const blueUser: User = {
id: 2,
isPlatformAdmin: false,
firstName: "Blue",
lastName: "Bluey",
email: "[email protected]",
teams: [
{
role: "teamEditor",
team: {
name: "Blue Team",
slug: "blue-team",
id: 1,
},
},
],
};

const readOnlyUser: User = {
id: 3,
isPlatformAdmin: false,
firstName: "Read",
lastName: "Only",
email: "[email protected]",
teams: [],
};

const adminUser: User = {
id: 4,
isPlatformAdmin: true,
firstName: "Platform",
lastName: "Admin",
email: "[email protected]",
teams: [],
};

let initialState: FullStore;

beforeEach(() => {
initialState = getState();
});

afterEach(() => setState(initialState));

describe("canUserEditTeam helper function", () => {
it("returns true when a user has teamEditor permission for a team", () => {
setState({ user: redUser });
expect(canUserEditTeam("red-team")).toBe(true);
expect(canUserEditTeam("blue-team")).toBe(false);
});

it("returns false when a user does not have permission for a team", () => {
setState({ user: blueUser });
expect(canUserEditTeam("red-team")).toBe(false);
});

it("returns false when a user does not have any permissions", () => {
setState({ user: readOnlyUser });
expect(canUserEditTeam("red-team")).toBe(false);
expect(canUserEditTeam("blue-team")).toBe(false);
});

it("returns true when a user is has the platformAdmin role", () => {
setState({ user: adminUser });
expect(canUserEditTeam("red-team")).toBe(true);
expect(canUserEditTeam("blue-team")).toBe(true);
});
});
12 changes: 5 additions & 7 deletions editor.planx.uk/src/pages/FlowEditor/lib/store/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,13 @@ export const userStore: StateCreator<UserStore, [], [], UserStore> = (
getUser: () => get().user,

canUserEditTeam(teamSlug) {
const user = this.getUser();
const user = get().getUser();
if (!user) return false;

const hasTeamEditorRole = (team: UserTeams) => team.role === "teamEditor" && team.team.slug === teamSlug;

return (
user.isPlatformAdmin ||
user.teams.some(hasTeamEditorRole)
);
const hasTeamEditorRole = (team: UserTeams) =>
team.role === "teamEditor" && team.team.slug === teamSlug;

return user.isPlatformAdmin || user.teams.some(hasTeamEditorRole);
},

async initUserStore(jwt: string) {
Expand Down

0 comments on commit 0cd8a28

Please sign in to comment.