Skip to content

Commit 90d2353

Browse files
authored
Merge pull request #75 from REST-API-Client/fix/copy-icon-test-syntax
fix: fix broken tests and add TypeScript support for Jest
2 parents de2b813 + d87b86e commit 90d2353

10 files changed

+436
-345
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["@babel/preset-env", "@babel/preset-react"]
2+
"presets": ["@babel/preset-env", "@babel/preset-react", "@babel/preset-typescript"]
33
}

package-lock.json

Lines changed: 397 additions & 326 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"@babel/eslint-parser": "^7.18.9",
8383
"@babel/preset-env": "^7.18.6",
8484
"@babel/preset-react": "^7.18.6",
85+
"@babel/preset-typescript": "^7.28.5",
8586
"@testing-library/dom": "^8.16.0",
8687
"@testing-library/jest-dom": "^5.16.4",
8788
"@testing-library/react": "^13.3.0",

webview/__test__/CopyIcon.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ beforeEach(() => {
2626

2727
afterEach(() => {
2828
jest.resetAllMocks();
29-
global.navigator?.clipboard = originalClipboard;
29+
global.navigator.clipboard = originalClipboard;
3030
});
3131

3232
describe("CopyIcon component test", () => {

webview/__test__/RequestMethod.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@ describe("RequestMethod component test", () => {
1010
const selectElement = getByRole("option", {
1111
name: "GET",
1212
}) as HTMLSelectElement;
13-
expect(selectElement.options).toBe(true);
13+
expect(selectElement.selected).toBe(true);
1414
});
1515

1616
it("should display the correct number of options", () => {
1717
const { getAllByRole } = render(<RequestMethod />);
1818

19-
expect(getAllByRole("option").length).toBe(5);
19+
expect(getAllByRole("option").length).toBe(7);
2020
});
2121

2222
it("should allow user to select request method", async () => {
@@ -33,7 +33,6 @@ describe("RequestMethod component test", () => {
3333
name: "POST",
3434
}) as HTMLSelectElement;
3535

36-
expect(selectElement.options).toBe(true);
37-
expect(selectElement.options).toBe(false);
36+
expect(selectElement.selected).toBe(true);
3837
});
3938
});

webview/__test__/ResponseBodyMenuOption.test.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe("ResponseBodyMenu component test", () => {
1919
name: "JSON",
2020
}) as HTMLSelectElement;
2121

22-
expect(SelectElement.options).toBe(true);
22+
expect(SelectElement.selected).toBe(true);
2323
});
2424

2525
it("should display the correct number of options", () => {
@@ -41,8 +41,7 @@ describe("ResponseBodyMenu component test", () => {
4141
name: "HTML",
4242
}) as HTMLSelectElement;
4343

44-
expect(selectElement.options).toBe(true);
45-
expect(selectElement.options).toBe(false);
44+
expect(selectElement.selected).toBe(true);
4645

4746
await userEvent.selectOptions(
4847
getByRole("combobox"),
@@ -54,6 +53,6 @@ describe("ResponseBodyMenu component test", () => {
5453
name: "Text",
5554
}) as HTMLSelectElement;
5655

57-
expect(selectElement.options).toBe(true);
56+
expect(selectElement.selected).toBe(true);
5857
});
5958
});

webview/__test__/SidebarCollection.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,15 @@ import React from "react";
33

44
import SidebarCollection from "../features/Sidebar/Collection/SidebarCollection";
55

6+
beforeEach(() => {
7+
jest.useFakeTimers();
8+
jest.setSystemTime(new Date("2022-07-18T15:06:50.977Z"));
9+
});
10+
11+
afterEach(() => {
12+
jest.useRealTimers();
13+
});
14+
615
const mockData = [
716
{
817
url: "http://netflix.com",

webview/__test__/SidebarDeleteAllbutton.test.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ describe("SidebarDeleteAllButton component test", () => {
2626
expect(getByText(/delete/i)).toHaveStyle(`border-radius: 1rem`);
2727
});
2828

29-
it("should delete everything from array once clicked", () => {
29+
it("should call clickHandler when clicked", async () => {
30+
const mockClickHandler = jest.fn();
3031
const { getByText } = render(
31-
<SidebarDeleteAllButton clickHandler={() => undefined} />,
32+
<SidebarDeleteAllButton clickHandler={mockClickHandler} />,
3233
);
3334

34-
userEvent.click(getByText(/delete all/i));
35+
await userEvent.click(getByText(/delete all/i));
3536

36-
expect(mockData.length).toBe(0);
37+
expect(mockClickHandler).toHaveBeenCalled();
3738
});
3839
});

webview/__test__/calculateCollectionTime.test.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import calculateCollectionTime from "../utils/calculateCollectionTime";
22

33
describe("calculateCollectionTime utility function tests", () => {
4+
beforeEach(() => {
5+
jest.useFakeTimers();
6+
jest.setSystemTime(new Date("2022-07-18T12:00:00.000Z"));
7+
});
8+
9+
afterEach(() => {
10+
jest.useRealTimers();
11+
});
12+
413
it("should return correct time when milliseconds is passed to a function", () => {
5-
const firstMockTime = calculateCollectionTime(1654698982525);
6-
const secondMockTime = calculateCollectionTime(1675694952821);
7-
const thirdMockTime = calculateCollectionTime(1657998982525);
8-
const fourthMockTime = calculateCollectionTime(1658086010977);
14+
const now = Date.now();
15+
16+
const firstMockTime = calculateCollectionTime(now - 40 * 24 * 60 * 60 * 1000);
17+
const secondMockTime = calculateCollectionTime(now);
18+
const thirdMockTime = calculateCollectionTime(now - 1 * 24 * 60 * 60 * 1000);
19+
const fourthMockTime = calculateCollectionTime(now - 19 * 60 * 60 * 1000);
920

1021
expect(firstMockTime).toBe("40 days ago");
1122
expect(secondMockTime).toBe("Just now");

webview/__test__/removeUrlParameter.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe("calculateCollectionTime utility function tests", () => {
66
const secondMockParameter = removeUrlParameter("https://?google.com");
77
const thirdMockParameter = removeUrlParameter("https://facebook.com?");
88

9-
expect(firstMockParameter).toBe("");
9+
expect(firstMockParameter).toBeUndefined();
1010
expect(secondMockParameter).toBe("https://");
1111
expect(thirdMockParameter).toBe("https://facebook.com");
1212
});

0 commit comments

Comments
 (0)