Skip to content

Commit 3220564

Browse files
authored
chore: remove ts-ignore from code when possible (#30)
1 parent f12239e commit 3220564

File tree

6 files changed

+46
-56
lines changed

6 files changed

+46
-56
lines changed

src/main/js/dashboard-frontend/src/__tests__/ComponentTable.test.tsx

+8-8
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { render } from "@testing-library/react";
2222
import ComponentTable from "../components/ComponentTable";
2323
import { SERVICE_ROUTE_HREF_PREFIX } from "../util/constNames";
2424

25-
let componentTable: ElementWrapper;
25+
let componentTable: ElementWrapper<Element>;
2626

2727
beforeEach(async () => {
2828
let { container } = render(
@@ -34,18 +34,18 @@ beforeEach(async () => {
3434
});
3535

3636
test("Table fetches and displays data from the server", () => {
37-
// @ts-ignore
38-
let rows = componentTable.findTable().findRows();
37+
let rows = componentTable.findTable()!.findRows();
3938
expect(rows.length).toEqual(4); // user-defined elements from fullRangeList
4039
});
4140

4241
test("Service item points to the right href", () => {
4342
expect(
44-
//@ts-ignore
45-
componentTable
46-
.findTable()
47-
.findBodyCell(1, 2)
48-
.getElement().firstElementChild.getAttribute("href")
43+
componentTable
44+
.findTable()!
45+
.findBodyCell(1, 2)!
46+
.getElement()!
47+
.firstElementChild!
48+
.getAttribute("href")
4949
).toEqual(SERVICE_ROUTE_HREF_PREFIX + fullRangeList[0].name);
5050
});
5151

src/main/js/dashboard-frontend/src/__tests__/ConfigEditor.test.tsx

+8-16
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,16 @@ test("If config get is successful then config is editable", (done) => {
2929
let configEditor = wrapper(container);
3030
setTimeout(() => {
3131
expect(
32-
// @ts-ignore
33-
configEditor.findButton().getElement()
32+
configEditor.findButton()!.getElement()
3433
).not.toBeDisabled();
3534
try {
36-
// @ts-ignore
37-
configEditor.findFlashbar().findItems()[0].findContent().getElement()
35+
configEditor.findFlashbar()!.findItems()[0].findContent()!.getElement()
3836
.innerHTML;
3937
fail("Flashbar contains items even when config load is successful");
4038
} catch (e) {}
4139
expect(
42-
// @ts-ignore
4340
configEditor
44-
.find(".ace_text-input")
41+
.find(".ace_text-input")!
4542
.getElement()
4643
.getAttributeNames()
4744
.includes("readonly")
@@ -60,18 +57,15 @@ test("If config get is unsuccessful then an error is displayed", (done) => {
6057
let configEditor = wrapper(container);
6158
setTimeout(() => {
6259
expect(
63-
// @ts-ignore
64-
configEditor.findButton().getElement()
60+
configEditor.findButton()!.getElement()
6561
).toBeDisabled();
6662
expect(
67-
// @ts-ignore
68-
configEditor.findFlashbar().findItems()[0].findContent().getElement()
63+
configEditor.findFlashbar()!.findItems()[0].findContent()!.getElement()
6964
.innerHTML
7065
).toContain(mockConfigError);
7166
expect(
72-
// @ts-ignore
7367
configEditor
74-
.find(".ace_text-input")
68+
.find(".ace_text-input")!
7569
.getElement()
7670
.getAttributeNames()
7771
.includes("readonly")
@@ -90,12 +84,10 @@ test("If config update is successful then flashbar displays new message", (done)
9084
let configEditor = wrapper(container);
9185

9286
setTimeout(() => {
93-
// @ts-ignore
94-
configEditor.findButton().click();
87+
configEditor.findButton()!.click();
9588
setTimeout(() => {
9689
expect(
97-
// @ts-ignore
98-
configEditor.findFlashbar().findItems()[0].getElement().innerHTML
90+
configEditor.findFlashbar()!.findItems()[0].getElement().innerHTML
9991
).toContain("updated successfully");
10092
done();
10193
}, 50);

src/main/js/dashboard-frontend/src/__tests__/DetailHeader.test.tsx

+14-20
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,18 @@ import wrapper, {ElementWrapper,} from "@cloudscape-design/components/test-utils
1111
import {render} from "@testing-library/react";
1212
import 'mutationobserver-shim';
1313

14-
import {SERVER} from "../index";
14+
import {SERVER as mockServer} from "../index";
1515
import {multipleComponentUpdates} from "../communication/__mocks__/mockedData";
16+
import {default as ServerEndpointMock} from "../communication/__mocks__/ServerEndpoint";
1617
import DetailHeader from "../components/details/DetailHeader";
1718
import {APICall} from "../util/CommUtils";
1819

1920
jest.mock("../communication/ServerEndpoint");
2021
jest.mock("../index");
2122

22-
let detailHeader: ElementWrapper;
23+
const SERVER = mockServer as unknown as ServerEndpointMock;
24+
25+
let detailHeader: ElementWrapper<Element>;
2326

2427
beforeEach(async () => {
2528
let { container } = render(
@@ -31,44 +34,35 @@ beforeEach(async () => {
3134
test("Buttons are enabled based on service status", () => {
3235
SERVER.pushComponentUpdate(0);
3336
expect(
34-
// @ts-ignore
35-
detailHeader.findButton("[data-testid=\"start-button\"]").getElement()
37+
detailHeader.findButton("[data-testid=\"start-button\"]")!.getElement()
3638
).not.toBeDisabled();
3739
expect(
38-
// @ts-ignore
39-
detailHeader.findButton("[data-testid=\"stop-button\"]").getElement()
40+
detailHeader.findButton("[data-testid=\"stop-button\"]")!.getElement()
4041
).not.toBeDisabled();
4142

4243
SERVER.pushComponentUpdate(1);
4344
expect(
44-
// @ts-ignore
45-
detailHeader.findButton("[data-testid=\"start-button\"]").getElement()
45+
detailHeader.findButton("[data-testid=\"start-button\"]")!.getElement()
4646
).toBeDisabled();
4747
expect(
48-
// @ts-ignore
49-
detailHeader.findButton("[data-testid=\"stop-button\"]").getElement()
48+
detailHeader.findButton("[data-testid=\"stop-button\"]")!.getElement()
5049
).not.toBeDisabled();
5150

5251
SERVER.pushComponentUpdate(2);
5352
expect(
54-
// @ts-ignore
55-
detailHeader.findButton("[data-testid=\"start-button\"]").getElement()
53+
detailHeader.findButton("[data-testid=\"start-button\"]")!.getElement()
5654
).not.toBeDisabled();
5755
expect(
58-
// @ts-ignore
59-
detailHeader.findButton("[data-testid=\"stop-button\"]").getElement()
56+
detailHeader.findButton("[data-testid=\"stop-button\"]")!.getElement()
6057
).toBeDisabled();
6158
});
6259

6360
test("Buttons function properly", (done) => {
6461
SERVER.pushComponentUpdate(0);
6562
const reqSpy = jest.spyOn(ServerEndpoint.prototype, "sendRequest");
66-
// @ts-ignore
67-
detailHeader.findButton("[data-testid=\"start-button\"]").click();
68-
// @ts-ignore
69-
detailHeader.findButton("[data-testid=\"stop-button\"]").click();
70-
// @ts-ignore
71-
detailHeader.findButton("[data-testid=\"reinstall-button\"]").click();
63+
detailHeader.findButton("[data-testid=\"start-button\"]")!.click();
64+
detailHeader.findButton("[data-testid=\"stop-button\"]")!.click();
65+
detailHeader.findButton("[data-testid=\"reinstall-button\"]")!.click();
7266
setTimeout(() => {
7367
expect(reqSpy).toHaveBeenNthCalledWith(1, {
7468
call: APICall.startComponent,

src/main/js/dashboard-frontend/src/components/details/ConfigEditor.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
Header,
2020
Spinner,
2121
} from "@cloudscape-design/components";
22+
import generateUniqueId from "../../util/generateUniqueId";
2223

2324
interface ConfigEditorProps {
2425
dark: boolean;
@@ -29,7 +30,6 @@ interface ConfigEditorState {
2930
saving: boolean;
3031
value: string;
3132
flashItems: FlashbarProps.MessageDefinition[];
32-
flashCnt: number;
3333
}
3434
export class ConfigEditor extends React.Component<
3535
ConfigEditorProps,
@@ -40,7 +40,6 @@ export class ConfigEditor extends React.Component<
4040
saving: false,
4141
value: "",
4242
flashItems: [] as FlashbarProps.MessageDefinition[],
43-
flashCnt: 0,
4443
};
4544

4645
onChange(newValue: string) {
@@ -66,24 +65,22 @@ export class ConfigEditor extends React.Component<
6665
this.updateFlashbar(false, reason);
6766
});
6867
};
69-
removeItem = (id: number) => {
68+
removeItem = (id: string) => {
7069
this.setState(prevState => ({
7170
flashItems: prevState.flashItems.filter((item) => {
72-
//@ts-ignore
73-
return item.num !== id;
71+
return item.id !== id;
7472
})
7573
}));
7674
};
7775

7876
updateFlashbar = (success: boolean, errorMsg?: string) => {
79-
const itemID = this.state.flashCnt;
77+
const itemID = generateUniqueId();
8078
if (success) {
8179
this.setState(prevState => ({
8280
flashItems:
8381
prevState.flashItems.concat([
8482
{
85-
// @ts-ignore
86-
num: itemID,
83+
id: itemID,
8784
type: "success",
8885
content: "Config updated successfully.",
8986
dismissible: true,
@@ -96,8 +93,7 @@ export class ConfigEditor extends React.Component<
9693
flashItems:
9794
prevState.flashItems.concat([
9895
{
99-
// @ts-ignore
100-
num: itemID,
96+
id: itemID,
10197
type: "error",
10298
content: `Unable to update config. ${errorMsg}`,
10399
dismissible: true,
@@ -106,7 +102,6 @@ export class ConfigEditor extends React.Component<
106102
]),
107103
}));
108104
}
109-
this.setState(prevState => ({ flashCnt: prevState.flashCnt + 1 }));
110105
};
111106

112107
async componentDidUpdate(

src/main/js/dashboard-frontend/src/navigation/NavSideBar.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,4 @@ function NavSideBar() {
5353
);
5454
}
5555

56-
// @ts-ignore
5756
export default withRouter(NavSideBar);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
let counter = 0;
7+
8+
export default function generateUniqueId(prefix?: string): string {
9+
return `${prefix ? prefix : ''}${counter++}-${Date.now()}-${Math.round(Math.random() * 10000)}`;
10+
}

0 commit comments

Comments
 (0)