-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add basic test cases for UserStore
- Loading branch information
1 parent
712f916
commit 0cd8a28
Showing
3 changed files
with
107 additions
and
10 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
101 changes: 101 additions & 0 deletions
101
editor.planx.uk/src/pages/FlowEditor/lib/__tests__/user.test.ts
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,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); | ||
}); | ||
}); |
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