Skip to content

Commit 4e88c8b

Browse files
committed
standardizing mockServer responses and reporting back failure reasons
Signed-off-by: David Slear <david_slear@yahoo.com>
1 parent 093f43c commit 4e88c8b

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

internal/lookoutui/src/pages/jobs/components/PreemptDialog.test.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ describe("PreemptDialog", () => {
136136
mockServer.setPostJobsResponse([jobs[0]])
137137
const { getByRole, findByText } = renderComponent()
138138

139-
mockServer.setPreemptJobsResponse()
139+
mockServer.setPreemptJobsResponse([jobs[0].jobId], [])
140140

141141
await enterPreemptReason("Reason for preemption")
142142

@@ -170,7 +170,7 @@ describe("PreemptDialog", () => {
170170
mockServer.setPostJobsResponse([jobs[0]])
171171
const { getByRole, findByText, findByRole } = renderComponent()
172172

173-
mockServer.setPreemptJobsResponse(500, "Internal Server Error")
173+
mockServer.setPreemptJobsResponse([], [{ jobId: jobs[0].jobId, errorReason: "Internal Server Error" }])
174174

175175
await enterPreemptReason("Reason for preemption")
176176

internal/lookoutui/src/pages/jobs/components/PreemptDialog.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ import { usePreemptJobs } from "../../../services/lookout/usePreemptJobs"
2626
import dialogStyles from "./DialogStyles.module.css"
2727
import { JobStatusTable } from "./JobStatusTable"
2828

29+
const MAX_JOB_IDS_TO_DISPLAY = 4
30+
2931
interface PreemptDialogProps {
3032
onClose: () => void
3133
selectedItemFilters: JobFiltersWithExcludes[]
@@ -73,13 +75,12 @@ export const PreemptDialog = ({ onClose, selectedItemFilters }: PreemptDialogPro
7375

7476
if (response.failedJobIds.length === 0) {
7577
const ids = response.successfulJobIds
76-
const MAX_DISPLAY = 4
77-
if (ids.length <= MAX_DISPLAY) {
78+
if (ids.length <= MAX_JOB_IDS_TO_DISPLAY) {
7879
openSnackbar(`Successfully requested preemption for: ${ids.join(", ")}`, "success")
7980
} else {
80-
const displayed = ids.slice(0, MAX_DISPLAY).join(", ")
81+
const displayed = ids.slice(0, MAX_JOB_IDS_TO_DISPLAY).join(", ")
8182
openSnackbar(
82-
`Successfully requested preemption for ${ids.length} jobs: ${displayed}, and ${ids.length - MAX_DISPLAY} more`,
83+
`Successfully requested preemption for ${ids.length} jobs: ${displayed}, and ${ids.length - MAX_JOB_IDS_TO_DISPLAY} more`,
8384
"success",
8485
)
8586
}
@@ -89,6 +90,9 @@ export const PreemptDialog = ({ onClose, selectedItemFilters }: PreemptDialogPro
8990

9091
const newResponseStatus = { ...jobIdsToPreemptResponses }
9192

93+
response.successfulJobIds.forEach((jobId) => {newResponseStatus[jobId] = "Success"})
94+
response.failedJobIds.forEach(({ jobId, errorReason }) => {newResponseStatus[jobId] = errorReason})
95+
9296
setJobIdsToPreemptResponses(newResponseStatus)
9397
setHasAttemptedPreempt(true)
9498
} finally {

internal/lookoutui/src/services/lookout/mocks/mockServer.ts

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -150,26 +150,19 @@ export class MockServer {
150150
)
151151
}
152152

153-
setPreemptJobsResponse(status_code: number = 200, status_text: string = "") {
153+
setPreemptJobsResponse(successfulJobIds: JobId[], failedJobIds: { jobId: JobId; errorReason: string }[] = []) {
154154
this.server.use(
155155
http.post(PREEMPT_JOBS_ENDPOINT, async (req) => {
156-
if (status_code !== 200) {
157-
return HttpResponse.json(
158-
{
159-
code: 500,
160-
message: "Internal server error",
161-
details: [],
162-
},
163-
{ status: status_code, statusText: status_text },
164-
)
165-
}
166-
const reqJson = (await req.request.json()) as { jobIds?: string[] }
167-
const jobIds = reqJson.jobIds ?? []
168-
const preemptionResults: Record<string, string> = {}
169-
for (const jobId of jobIds) {
156+
const preemptionResults: Record<JobId, string> = {}
157+
for (const jobId of successfulJobIds) {
170158
preemptionResults[jobId] = ""
171159
}
172-
return HttpResponse.json({ preemptionResults })
160+
for (const { jobId, errorReason } of failedJobIds) {
161+
preemptionResults[jobId] = errorReason
162+
}
163+
return HttpResponse.json({
164+
preemptionResults,
165+
})
173166
}),
174167
)
175168
}

0 commit comments

Comments
 (0)