-
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.
feat: add new editor - error handling and enhancements (#3543)
Co-authored-by: Dafydd Llŷr Pearson <[email protected]>
- Loading branch information
1 parent
68a7ca9
commit 0b6b010
Showing
11 changed files
with
218 additions
and
52 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
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
8 changes: 8 additions & 0 deletions
8
editor.planx.uk/src/pages/FlowEditor/components/Team/errors/addNewEditorErrors.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,8 @@ | ||
export const AddNewEditorErrors = { | ||
USER_ALREADY_EXISTS: { | ||
regex: /violates unique constraint "users_email_key"/i, | ||
errorMessage: "User already exists", | ||
}, | ||
}; | ||
export const isUserAlreadyExistsError = (error: string) => | ||
AddNewEditorErrors.USER_ALREADY_EXISTS.regex.test(error); |
44 changes: 44 additions & 0 deletions
44
...nx.uk/src/pages/FlowEditor/components/Team/tests/TeamMembers.addNewEditor.errors.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,44 @@ | ||
import { screen, within } from "@testing-library/react"; | ||
import { FullStore, useStore } from "pages/FlowEditor/lib/store"; | ||
import { vi } from "vitest"; | ||
|
||
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen"; | ||
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor"; | ||
import { mockTeamMembersData } from "./mocks/mockTeamMembersData"; | ||
import { alreadyExistingUser } from "./mocks/mockUsers"; | ||
|
||
vi.mock("lib/featureFlags.ts", () => ({ | ||
hasFeatureFlag: vi.fn().mockReturnValue(true), | ||
})); | ||
|
||
vi.mock( | ||
"pages/FlowEditor/components/Team/queries/createAndAddUserToTeam.tsx", | ||
() => ({ | ||
createAndAddUserToTeam: vi.fn().mockRejectedValue({ | ||
message: | ||
'Uniqueness violation. duplicate key value violates unique constraint "users_email_key"', | ||
}), | ||
}), | ||
); | ||
|
||
let initialState: FullStore; | ||
|
||
describe("when a user fills in the 'add a new editor' form correctly but the user already exists", () => { | ||
afterAll(() => useStore.setState(initialState)); | ||
beforeEach(async () => { | ||
useStore.setState({ | ||
teamMembers: [...mockTeamMembersData, alreadyExistingUser], | ||
}); | ||
|
||
const user = await setupTeamMembersScreen(); | ||
await userTriesToAddNewEditor(user); | ||
}); | ||
|
||
it("shows an appropriate error message", async () => { | ||
const addNewEditorModal = await screen.findByTestId("dialog-create-user"); | ||
|
||
expect( | ||
await within(addNewEditorModal).findByText(/User already exists/), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -3,7 +3,8 @@ import { FullStore, useStore } from "pages/FlowEditor/lib/store"; | |
import { vi } from "vitest"; | ||
|
||
import { setupTeamMembersScreen } from "./helpers/setupTeamMembersScreen"; | ||
import { userEntersInput } from "./helpers/userEntersInput"; | ||
import { userTriesToAddNewEditor } from "./helpers/userTriesToAddNewEditor"; | ||
import { mockTeamMembersData } from "./mocks/mockTeamMembersData"; | ||
|
||
vi.mock("lib/featureFlags.ts", () => ({ | ||
hasFeatureFlag: vi.fn().mockReturnValue(true), | ||
|
@@ -23,6 +24,7 @@ let initialState: FullStore; | |
|
||
describe("when a user with the ADD_NEW_EDITOR feature flag enabled presses 'add a new editor'", () => { | ||
beforeEach(async () => { | ||
useStore.setState({ teamMembers: mockTeamMembersData }); | ||
const user = await setupTeamMembersScreen(); | ||
|
||
const teamEditorsTable = screen.getByTestId("team-editors"); | ||
|
@@ -41,26 +43,9 @@ describe("when a user with the ADD_NEW_EDITOR feature flag enabled presses 'add | |
describe("when a user fills in the 'add a new editor' form correctly", () => { | ||
afterAll(() => useStore.setState(initialState)); | ||
beforeEach(async () => { | ||
useStore.setState({ teamMembers: mockTeamMembersData }); | ||
const user = await setupTeamMembersScreen(); | ||
const teamEditorsTable = screen.getByTestId("team-editors"); | ||
const addEditorButton = await within(teamEditorsTable).findByText( | ||
"Add a new editor", | ||
); | ||
user.click(addEditorButton); | ||
const addNewEditorModal = await screen.findByTestId("modal-create-user"); | ||
await userEntersInput("First name", "Mickey", addNewEditorModal); | ||
await userEntersInput("Last name", "Mouse", addNewEditorModal); | ||
await userEntersInput( | ||
"Email address", | ||
"[email protected]", | ||
addNewEditorModal, | ||
); | ||
|
||
const createUserButton = await screen.findByTestId( | ||
"modal-create-user-button", | ||
); | ||
|
||
user.click(createUserButton); | ||
await userTriesToAddNewEditor(user); | ||
}); | ||
|
||
it("adds the new user row to the Team Editors table", async () => { | ||
|
@@ -72,4 +57,16 @@ describe("when a user fills in the 'add a new editor' form correctly", () => { | |
).toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("closes the modal", async () => { | ||
await waitFor(() => { | ||
expect(screen.queryByTestId("modal-create-user")).not.toBeInTheDocument(); | ||
}); | ||
}); | ||
|
||
it("shows a success message", async () => { | ||
expect( | ||
await screen.findByText(/Successfully added a user/), | ||
).toBeInTheDocument(); | ||
}); | ||
}); |
4 changes: 0 additions & 4 deletions
4
...or.planx.uk/src/pages/FlowEditor/components/Team/tests/helpers/setupTeamMembersScreen.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
8 changes: 4 additions & 4 deletions
8
editor.planx.uk/src/pages/FlowEditor/components/Team/tests/helpers/userEntersInput.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,13 +1,13 @@ | ||
import { fireEvent, within } from "@testing-library/react"; | ||
import { within } from "@testing-library/react"; | ||
import { UserEvent } from "@testing-library/user-event/dist/types/setup/setup"; | ||
|
||
export const userEntersInput = async ( | ||
labelText: string, | ||
inputString: string, | ||
container: HTMLElement, | ||
user: UserEvent, | ||
) => { | ||
const inputField = await within(container).findByLabelText(labelText); | ||
|
||
fireEvent.change(inputField, { | ||
target: { value: inputString }, | ||
}); | ||
await user.type(inputField, inputString); | ||
}; |
27 changes: 27 additions & 0 deletions
27
...r.planx.uk/src/pages/FlowEditor/components/Team/tests/helpers/userTriesToAddNewEditor.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,27 @@ | ||
import { screen, within } from "@testing-library/react"; | ||
import { UserEvent } from "@testing-library/user-event/dist/types/setup/setup"; | ||
|
||
import { userEntersInput } from "./userEntersInput"; | ||
|
||
export const userTriesToAddNewEditor = async (user: UserEvent) => { | ||
const teamEditorsTable = screen.getByTestId("team-editors"); | ||
const addEditorButton = await within(teamEditorsTable).findByText( | ||
"Add a new editor", | ||
); | ||
user.click(addEditorButton); | ||
const addNewEditorModal = await screen.findByTestId("modal-create-user"); | ||
await userEntersInput("First name", "Mickey", addNewEditorModal, user); | ||
await userEntersInput("Last name", "Mouse", addNewEditorModal, user); | ||
await userEntersInput( | ||
"Email address", | ||
"[email protected]", | ||
addNewEditorModal, | ||
user, | ||
); | ||
|
||
const createUserButton = await screen.findByTestId( | ||
"modal-create-user-button", | ||
); | ||
|
||
user.click(createUserButton); | ||
}; |
18 changes: 18 additions & 0 deletions
18
editor.planx.uk/src/pages/FlowEditor/components/Team/tests/mocks/mockTeamMembersData.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,18 @@ | ||
import { TeamMember } from "../../types"; | ||
|
||
export const mockTeamMembersData: TeamMember[] = [ | ||
{ | ||
firstName: "Donella", | ||
lastName: "Meadows", | ||
email: "[email protected]", | ||
id: 1, | ||
role: "platformAdmin", | ||
}, | ||
{ | ||
firstName: "Bill", | ||
lastName: "Sharpe", | ||
email: "[email protected]", | ||
id: 2, | ||
role: "teamEditor", | ||
}, | ||
]; |
9 changes: 9 additions & 0 deletions
9
editor.planx.uk/src/pages/FlowEditor/components/Team/tests/mocks/mockUsers.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,9 @@ | ||
import { TeamMember } from "../../types"; | ||
|
||
export const alreadyExistingUser: TeamMember = { | ||
firstName: "Mickey", | ||
lastName: "Mouse", | ||
email: "[email protected]", | ||
id: 3, | ||
role: "teamEditor", | ||
}; |
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