-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WD-11589 - refactor: remove nested components in StatusGroup (#1765)
- Loading branch information
1 parent
f5d2751
commit 16a7c52
Showing
5 changed files
with
195 additions
and
111 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
117 changes: 117 additions & 0 deletions
117
src/components/ModelTableList/StatusGroup/WarningMessage/WarningMessage.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,117 @@ | ||
import { screen, act, within } from "@testing-library/react"; | ||
import userEvent from "@testing-library/user-event"; | ||
import type { UserEvent } from "@testing-library/user-event"; | ||
import { vi } from "vitest"; | ||
|
||
import { detailedStatusFactory } from "testing/factories/juju/ClientV6"; | ||
import { | ||
modelDataApplicationFactory, | ||
modelDataFactory, | ||
modelDataUnitFactory, | ||
} from "testing/factories/juju/juju"; | ||
import { renderComponent } from "testing/utils"; | ||
|
||
import WarningMessage from "./WarningMessage"; | ||
|
||
describe("WarningMessage", () => { | ||
let userEventWithTimers: UserEvent; | ||
|
||
beforeEach(() => { | ||
vi.useFakeTimers(); | ||
userEventWithTimers = userEvent.setup({ | ||
advanceTimers: vi.advanceTimersByTime, | ||
}); | ||
}); | ||
|
||
afterEach(() => { | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it("should display nothing if there are no messages", () => { | ||
const model = modelDataFactory.build(); | ||
renderComponent(<WarningMessage model={model} />); | ||
expect(screen.queryByRole("link")).not.toBeInTheDocument(); | ||
}); | ||
|
||
it("should display links to blocked apps and units", async () => { | ||
const model = modelDataFactory.build({ | ||
applications: { | ||
calico: modelDataApplicationFactory.build({ | ||
status: detailedStatusFactory.build({ | ||
info: "app blocked", | ||
status: "blocked", | ||
}), | ||
}), | ||
etcd: modelDataApplicationFactory.build({ | ||
units: { | ||
"etcd/0": modelDataUnitFactory.build({ | ||
"agent-status": detailedStatusFactory.build({ | ||
info: "unit blocked", | ||
status: "lost", | ||
}), | ||
}), | ||
}, | ||
}), | ||
}, | ||
}); | ||
renderComponent(<WarningMessage model={model} />); | ||
const error = screen.getByRole("link", { name: "app blocked" }); | ||
expect(error).toHaveAttribute("href", "/models/eggman@external/sub-test"); | ||
await act(async () => { | ||
await userEventWithTimers.hover(error); | ||
vi.runAllTimers(); | ||
}); | ||
const tooltip = screen.getAllByRole("tooltip")[0]; | ||
expect(error).toHaveAttribute("href", "/models/eggman@external/sub-test"); | ||
const appError = within(tooltip).getByRole("link", { | ||
name: "app blocked", | ||
}); | ||
expect(appError).toHaveAttribute( | ||
"href", | ||
"/models/eggman@external/sub-test/app/calico", | ||
); | ||
const unitError = within(tooltip).getByRole("link", { | ||
name: "unit blocked", | ||
}); | ||
expect(unitError).toHaveAttribute( | ||
"href", | ||
"/models/eggman@external/sub-test/app/etcd/unit/etcd-0", | ||
); | ||
}); | ||
|
||
it("should display tooltip correctly when more than 5 messages are present", async () => { | ||
const model = modelDataFactory.build({ | ||
applications: [1, 2, 3, 4, 5, 6].reduce( | ||
(applications, index) => ({ | ||
...applications, | ||
[`app${index}`]: modelDataApplicationFactory.build({ | ||
status: detailedStatusFactory.build({ | ||
info: `app${index} blocked`, | ||
status: "blocked", | ||
}), | ||
}), | ||
}), | ||
{}, | ||
), | ||
}); | ||
renderComponent(<WarningMessage model={model} />); | ||
const error = screen.getByRole("link", { name: "app1 blocked" }); | ||
expect(error).toHaveAttribute("href", "/models/eggman@external/sub-test"); | ||
await act(async () => { | ||
await userEventWithTimers.hover(error); | ||
vi.runAllTimers(); | ||
}); | ||
const tooltip = screen.getAllByRole("tooltip")[0]; | ||
expect(error).toHaveAttribute("href", "/models/eggman@external/sub-test"); | ||
[1, 2, 3, 4, 5].forEach((index) => { | ||
const appError = within(tooltip).getByRole("link", { | ||
name: `app${index} blocked`, | ||
}); | ||
expect(appError).toHaveAttribute( | ||
"href", | ||
`/models/eggman@external/sub-test/app/app${index}`, | ||
); | ||
}); | ||
expect(within(tooltip).getByText("+1 more...")).toBeInTheDocument(); | ||
}); | ||
}); |
71 changes: 71 additions & 0 deletions
71
src/components/ModelTableList/StatusGroup/WarningMessage/WarningMessage.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,71 @@ | ||
import { List, Tooltip } from "@canonical/react-components"; | ||
import type { ListItem } from "@canonical/react-components/dist/components/List/List"; | ||
import { Link } from "react-router-dom"; | ||
|
||
import ModelDetailsLink from "components/ModelDetailsLink"; | ||
import type { ModelData } from "store/juju/types"; | ||
import { getModelStatusGroupData } from "store/juju/utils/models"; | ||
import urls from "urls"; | ||
import { getUserName } from "utils"; | ||
|
||
type Props = { | ||
/** | ||
* The full model data. | ||
*/ | ||
model: ModelData; | ||
}; | ||
|
||
/** | ||
Warning message for the model name cell. | ||
@return The react component for the warning message. | ||
*/ | ||
const WarningMessage = ({ model }: Props) => { | ||
const { messages } = getModelStatusGroupData(model); | ||
if (!messages.length) { | ||
return null; | ||
} | ||
const ownerTag = model?.info?.["owner-tag"] ?? ""; | ||
const userName = getUserName(ownerTag); | ||
const modelName = model.model.name; | ||
const link = ( | ||
<ModelDetailsLink modelName={modelName} ownerTag={ownerTag}> | ||
{messages[0].message} | ||
</ModelDetailsLink> | ||
); | ||
const list: ListItem[] = messages.slice(0, 5).map((message) => { | ||
const unitId = message.unitId ? message.unitId.replace("/", "-") : null; | ||
const appName = message.appName; | ||
return { | ||
className: "u-truncate", | ||
content: ( | ||
<> | ||
{unitId || appName}:{" "} | ||
<Link | ||
to={ | ||
unitId | ||
? urls.model.unit({ userName, modelName, appName, unitId }) | ||
: urls.model.app.index({ userName, modelName, appName }) | ||
} | ||
> | ||
{message.message} | ||
</Link> | ||
</> | ||
), | ||
}; | ||
}); | ||
const remainder = messages.slice(5); | ||
if (remainder.length) { | ||
list.push(`+${remainder.length} more...`); | ||
} | ||
return ( | ||
<Tooltip | ||
positionElementClassName="p-tooltip__position-element--inline" | ||
tooltipClassName="p-tooltip--constrain-width" | ||
message={<List className="u-no-margin--bottom" items={list} />} | ||
> | ||
<span className="model-table-list_error-message">{link}</span> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
export default WarningMessage; |
1 change: 1 addition & 0 deletions
1
src/components/ModelTableList/StatusGroup/WarningMessage/index.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 @@ | ||
export { default } from "./WarningMessage"; |