Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitfa5eae5

Browse files
committed
test: Add suspend error case
1 parent1f113e8 commitfa5eae5

File tree

4 files changed

+86
-29
lines changed

4 files changed

+86
-29
lines changed

‎site/src/components/GlobalSnackbar/utils.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import{displaySuccess,isNotificationTextPrefixed,MsgType,NotificationMsg}from"./utils"
1+
import{
2+
displayError,
3+
displaySuccess,
4+
isNotificationTextPrefixed,
5+
MsgType,
6+
NotificationMsg,
7+
SnackbarEventType,
8+
}from"./utils"
29

310
describe("Snackbar",()=>{
411
describe("isNotificationTextPrefixed",()=>{
@@ -76,4 +83,18 @@ describe("Snackbar", () => {
7683
expect(extractNotificationEvent(dispatchEventMock)).toStrictEqual(expected)
7784
})
7885
})
86+
87+
describe("displayError",()=>{
88+
it("shows the title and the message",(done)=>{
89+
constmessage="Some error happened"
90+
91+
window.addEventListener(SnackbarEventType,(event)=>{
92+
constnotificationEvent=eventasCustomEvent<NotificationMsg>
93+
expect(notificationEvent.detail.msg).toEqual(message)
94+
done()
95+
})
96+
97+
displayError(message)
98+
})
99+
})
79100
})

‎site/src/components/GlobalSnackbar/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,7 @@ export const displayMsg = (msg: string, additionalMsg?: string): void => {
6060
exportconstdisplaySuccess=(msg:string,additionalMsg?:string):void=>{
6161
dispatchNotificationEvent(MsgType.Success,msg,additionalMsg ?[additionalMsg] :undefined)
6262
}
63+
64+
exportconstdisplayError=(msg:string,additionalMsg?:string):void=>{
65+
dispatchNotificationEvent(MsgType.Error,msg,additionalMsg ?[additionalMsg] :undefined)
66+
}

‎site/src/pages/UsersPage/UsersPage.test.tsx

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,33 @@ import { MockUser, MockUser2, render } from "../../testHelpers"
77
import{LanguageasusersXServiceLanguage}from"../../xServices/users/usersXService"
88
import{LanguageasUsersPageLanguage,UsersPage}from"./UsersPage"
99

10+
constsuspendUser=async(setupActionSpies:()=>void)=>{
11+
// Get the first user in the table
12+
constusers=awaitscreen.findAllByText(/.*@coder.com/)
13+
constfirstUserRow=users[0].closest("tr")
14+
if(!firstUserRow){
15+
thrownewError("Error on get the first user row")
16+
}
17+
18+
// Click on the "more" button to display the "Suspend" option
19+
constmoreButton=within(firstUserRow).getByLabelText("more")
20+
fireEvent.click(moreButton)
21+
constmenu=screen.getByRole("menu")
22+
constsuspendButton=within(menu).getByText(UsersTableLanguage.suspendMenuItem)
23+
fireEvent.click(suspendButton)
24+
25+
// Check if the confirm message is displayed
26+
constconfirmDialog=screen.getByRole("dialog")
27+
expect(confirmDialog).toHaveTextContent("Do you want to suspend the user TestUser?")
28+
29+
// Setup spies to check the actions after
30+
setupActionSpies()
31+
32+
// Click on the "Confirm" button
33+
constconfirmButton=within(confirmDialog).getByText(UsersPageLanguage.suspendDialogAction)
34+
fireEvent.click(confirmButton)
35+
}
36+
1037
describe("Users Page",()=>{
1138
it("shows users",async()=>{
1239
render(<UsersPage/>)
@@ -24,41 +51,42 @@ describe("Users Page", () => {
2451
</>,
2552
)
2653

27-
// Get the first user in the table
28-
constusers=awaitscreen.findAllByText(/.*@coder.com/)
29-
constfirstUserRow=users[0].closest("tr")
30-
if(!firstUserRow){
31-
thrownewError("Error on get the first user row")
32-
}
54+
awaitsuspendUser(()=>{
55+
jest.spyOn(API,"suspendUser").mockResolvedValueOnce(MockUser)
56+
jest.spyOn(API,"getUsers").mockImplementationOnce(()=>Promise.resolve([MockUser,MockUser2]))
57+
})
3358

34-
// Click on the "more" button to display the "Suspend" option
35-
constmoreButton=within(firstUserRow).getByLabelText("more")
36-
fireEvent.click(moreButton)
37-
constmenu=screen.getByRole("menu")
38-
constsuspendButton=within(menu).getByText(UsersTableLanguage.suspendMenuItem)
39-
fireEvent.click(suspendButton)
59+
// Check if the success message is displayed
60+
awaitscreen.findByText(usersXServiceLanguage.suspendUserSuccess)
4061

41-
// Check if the confirm message is displayed
42-
constconfirmDialog=screen.getByRole("dialog")
43-
expect(confirmDialog).toHaveTextContent("Do you want to suspend the user TestUser?")
62+
// Check if the API was called correctly
63+
expect(API.suspendUser).toBeCalledTimes(1)
64+
expect(API.suspendUser).toBeCalledWith(MockUser.id)
65+
66+
// Check if the users list was reload
67+
awaitwaitFor(()=>expect(API.getUsers).toBeCalledTimes(1))
68+
})
69+
})
4470

45-
// Setup spies to check the actions after
46-
jest.spyOn(API,"suspendUser").mockResolvedValueOnce(MockUser)
47-
jest.spyOn(API,"getUsers").mockImplementationOnce(()=>Promise.resolve([MockUser,MockUser2]))
71+
describe("when it fails",()=>{
72+
it("shows an error message",async()=>{
73+
render(
74+
<>
75+
<UsersPage/>
76+
<GlobalSnackbar/>
77+
</>,
78+
)
4879

49-
// Click on the "Confirm" button
50-
constconfirmButton=within(confirmDialog).getByText(UsersPageLanguage.suspendDialogAction)
51-
fireEvent.click(confirmButton)
80+
awaitsuspendUser(()=>{
81+
jest.spyOn(API,"suspendUser").mockRejectedValueOnce({})
82+
})
5283

53-
// Check if the success message
54-
awaitscreen.findByText(usersXServiceLanguage.suspendUserSuccess)
84+
// Check if the success message is displayed
85+
awaitscreen.findByText(usersXServiceLanguage.suspendUserError)
5586

5687
// Check if the API was called correctly
5788
expect(API.suspendUser).toBeCalledTimes(1)
5889
expect(API.suspendUser).toBeCalledWith(MockUser.id)
59-
60-
// Check if the users list was refetched
61-
awaitwaitFor(()=>expect(API.getUsers).toBeCalledTimes(1))
6290
})
6391
})
6492
})

‎site/src/xServices/users/usersXService.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import * as API from "../../api"
33
import{ApiError,FieldErrors,isApiError,mapApiErrorToFieldErrors}from"../../api/errors"
44
import*asTypesfrom"../../api/types"
55
import*asTypesGenfrom"../../api/typesGenerated"
6-
import{displaySuccess}from"../../components/GlobalSnackbar/utils"
6+
import{displayError,displaySuccess}from"../../components/GlobalSnackbar/utils"
77

88
exportconstLanguage={
99
createUserSuccess:"Successfully created user.",
1010
suspendUserSuccess:"Successfully suspended the user.",
11+
suspendUserError:"Error on suspend the user",
1112
}
1213

1314
exportinterfaceUsersContext{
@@ -119,7 +120,7 @@ export const usersMachine = createMachine(
119120
},
120121
onError:{
121122
target:"error",
122-
actions:["assignSuspendUserError"],
123+
actions:["assignSuspendUserError","displaySuspendedErrorMessage"],
123124
},
124125
},
125126
},
@@ -185,6 +186,9 @@ export const usersMachine = createMachine(
185186
displaySuspendSuccess:()=>{
186187
displaySuccess(Language.suspendUserSuccess)
187188
},
189+
displaySuspendedErrorMessage:()=>{
190+
displayError(Language.suspendUserError)
191+
},
188192
},
189193
},
190194
)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp