Skip to content

Commit b7baea7

Browse files
authored
UserList. fix role update (#336)
1 parent dc0b57f commit b7baea7

File tree

5 files changed

+51
-15
lines changed

5 files changed

+51
-15
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
import { Locator } from "@playwright/test";
12
import { BasePage } from "./BasePage";
3+
import { type Role } from "~client/types";
24

3-
export class UserListPage extends BasePage {}
5+
export class UserListPage extends BasePage {
6+
private getRow(userId: string): Locator {
7+
return this.page.locator(`[data-id='${userId}']`);
8+
}
9+
10+
async changeRole(userId: string, role: keyof typeof Role) {
11+
await this.getRow(userId).locator("[data-field='role']").dblclick();
12+
await this.page.locator(`[data-value='${role}']`).click();
13+
14+
return this.page.keyboard.press("Enter");
15+
}
16+
}

integration_tests/test/userList.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { expect } from "@playwright/test";
12
import { test } from "fixtures";
23
import { mockGetProjects, mockGetUsers } from "utils/mocks";
34
import {
@@ -6,6 +7,7 @@ import {
67
TEST_PROJECT,
78
TEST_USER,
89
} from "~client/_test/test.data.helper";
10+
import { Role, User } from "~client/types";
911

1012
test.beforeEach(async ({ page }) => {
1113
await mockGetProjects(page, [TEST_PROJECT]);
@@ -16,3 +18,23 @@ test.beforeEach(async ({ page }) => {
1618
test("renders", async ({ userListPage, page, vrt }) => {
1719
await vrt.trackPage(page, "User list page");
1820
});
21+
22+
const assignRoleCases: [User, keyof typeof Role][] = [
23+
[EDITOR_USER, "admin"],
24+
[EDITOR_USER, "guest"],
25+
[GUEST_USER, "editor"],
26+
];
27+
28+
for (const [user, role] of assignRoleCases) {
29+
test(`can assign role ${role}`, async ({ userListPage, page }) => {
30+
const [request] = await Promise.all([
31+
page.waitForRequest("**/assignRole"),
32+
userListPage.changeRole(user.id, role),
33+
]);
34+
35+
expect(request.postDataJSON()).toEqual({
36+
id: user.id,
37+
role,
38+
});
39+
});
40+
}

playwright.config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export default defineConfig({
1010
command: "npm run start",
1111
url: baseURL,
1212
reuseExistingServer: true,
13-
timeout: 300 * 1000,
1413
},
1514
use: {
1615
headless: true,
@@ -20,8 +19,5 @@ export default defineConfig({
2019
trace: "retry-with-trace",
2120
},
2221
retries: process.env.CI ? 1 : 0,
23-
expect: {
24-
timeout: 10 * 1000,
25-
},
2622
forbidOnly: !!process.env.CI,
2723
});

src/components/UserList/index.tsx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
} from "@mui/x-data-grid";
1111
import { ActionButtons } from "./ActionButtons";
1212
import { usersService } from "../../services";
13-
import { Role } from "../../types";
13+
import { Role, User } from "../../types";
1414
import { useSnackbar } from "notistack";
1515
import { useUserDispatch, useUserState } from "../../contexts";
1616

@@ -61,15 +61,15 @@ const UserList = () => {
6161
userDispatch({
6262
type: "getAll",
6363
payload: users,
64-
}),
64+
})
6565
);
6666
}, [userDispatch]);
6767

68-
const handleEditCellChangeCommitted = React.useCallback(
69-
({ id, field, value }) => {
70-
if (field === "role") {
68+
const processRowUpdate = React.useCallback(
69+
(newState: User, oldState: User) => {
70+
if (newState.role !== oldState.role) {
7171
usersService
72-
.assignRole(id, value as Role)
72+
.assignRole(oldState.id, newState.role as Role)
7373
.then(() => {
7474
enqueueSnackbar("Updated", {
7575
variant: "success",
@@ -78,11 +78,12 @@ const UserList = () => {
7878
.catch((err) =>
7979
enqueueSnackbar(err, {
8080
variant: "error",
81-
}),
81+
})
8282
);
8383
}
84+
return newState;
8485
},
85-
[enqueueSnackbar],
86+
[enqueueSnackbar]
8687
);
8788

8889
const apiRef = useGridApiRef();
@@ -101,7 +102,7 @@ const UserList = () => {
101102
slots={{
102103
toolbar: DataGridCustomToolbar,
103104
}}
104-
onCellEditStop={handleEditCellChangeCommitted}
105+
processRowUpdate={processRowUpdate}
105106
/>
106107
);
107108
};

src/services/users.service.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ function register(
5959
});
6060
}
6161

62-
function update({ firstName, lastName, email }): Promise<User> {
62+
function update({
63+
firstName,
64+
lastName,
65+
email,
66+
}: Pick<User, "firstName" | "lastName" | "email">): Promise<User> {
6367
const requestOptions = {
6468
method: "PUT",
6569
headers: {

0 commit comments

Comments
 (0)