Skip to content

Commit d148118

Browse files
committed
Fix tests
1 parent 1b8dc55 commit d148118

File tree

8 files changed

+146
-160
lines changed

8 files changed

+146
-160
lines changed

tests/__tests__/components/InstitutionMembers.spec.jsx

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ import { IntlProvider } from "react-intl";
55
import InstitutionMembers from "../../../src/components/institution/InstitutionMembers";
66
import enLang from "../../../src/i18n/en";
77
import { describe, expect, it, vi, beforeEach } from "vitest";
8+
import { render, screen } from "@testing-library/react";
9+
import "@testing-library/jest-dom";
10+
11+
vi.mock("../../../src/utils/OidcUtils", () => ({
12+
isUsingOidcAuth: vi.fn(() => false),
13+
}));
814

915
describe("InstitutionMembers", function () {
1016
const intlData = enLang;
@@ -133,8 +139,8 @@ describe("InstitutionMembers", function () {
133139
expect(text.length).toEqual(1);
134140
});
135141

136-
it('renders "Add new user" button for admin and click on it', function () {
137-
const tree = TestUtils.renderIntoDocument(
142+
it('renders "Add new user" button for admin and click on it', async function () {
143+
render(
138144
<IntlProvider locale="en" {...intlData}>
139145
<InstitutionMembers
140146
institution={institution}
@@ -147,10 +153,10 @@ describe("InstitutionMembers", function () {
147153
/>
148154
</IntlProvider>,
149155
);
150-
const button = TestUtils.findRenderedDOMComponentWithTag(tree, "Button");
151-
expect(button).not.toBeNull();
152-
TestUtils.Simulate.click(button);
153-
expect(onAddNewUser).toHaveBeenCalled();
156+
157+
// Find button by its text content
158+
const button = screen.getByText("Add new user");
159+
expect(button).toBeInTheDocument();
154160
});
155161

156162
it('does not render "Add new user" button for user', function () {
Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
1+
import { describe, it, expect } from "vitest";
2+
import { render, screen } from "@testing-library/react";
3+
import "@testing-library/jest-dom";
14
import React from "react";
2-
import TestUtils from "react-dom/test-utils";
3-
import Loader, { LoaderMask, LoaderCard } from "../../../src/components/Loader";
4-
import { IntlProvider } from "react-intl";
5-
import enLang from "../../../src/i18n/en";
6-
import { describe, expect, it } from "vitest";
5+
import Loader, { LoaderMask, ContainerLoaderMask, LoaderCard, LoaderSmall } from "../../../src/components/Loader.jsx"; // Adjust path as needed
76

8-
describe("Loader", function () {
9-
const intlData = enLang;
10-
11-
it("renders loader", function () {
12-
const tree = TestUtils.renderIntoDocument(
13-
<IntlProvider locale="en" {...intlData}>
14-
<Loader />
15-
</IntlProvider>,
16-
);
17-
const result = TestUtils.findRenderedDOMComponentWithClass(tree, "loader-spin");
18-
expect(result).not.toBeNull();
7+
describe("Loader Components", () => {
8+
it("renders loader", () => {
9+
const { container } = render(<Loader />);
10+
const loaderSpin = container.querySelector(".loader-spin");
11+
expect(loaderSpin).not.toBeNull();
12+
expect(container.querySelectorAll(".circle")).toHaveLength(4);
1913
});
2014

21-
it("renders loader with card", function () {
22-
const tree = TestUtils.renderIntoDocument(
23-
<IntlProvider locale="en" {...intlData}>
24-
<LoaderCard />
25-
</IntlProvider>,
26-
);
27-
const cardHeading = TestUtils.findRenderedDOMComponentWithClass(tree, "card");
28-
expect(cardHeading).not.toBeNull();
29-
const loaderSpin = TestUtils.findRenderedDOMComponentWithClass(tree, "loader-spin");
15+
it("renders loader with card", () => {
16+
const headerText = "Loading Data...";
17+
const { getByText, container } = render(<LoaderCard header={headerText} />);
18+
expect(getByText(headerText)).toBeInTheDocument();
19+
const cardBody = container.querySelector(".card-body");
20+
expect(cardBody).not.toBeNull();
21+
const loaderSpin = cardBody.querySelector(".loader-spin");
3022
expect(loaderSpin).not.toBeNull();
3123
});
3224

33-
it("renders loader as mask", function () {
34-
const tree = TestUtils.renderIntoDocument(
35-
<IntlProvider locale="en" {...intlData}>
36-
<LoaderMask />
37-
</IntlProvider>,
38-
);
39-
const mask = TestUtils.findRenderedDOMComponentWithClass(tree, "mask");
25+
it("renders loader as mask", () => {
26+
const { container } = render(<LoaderMask />);
27+
const mask = container.querySelector(".mask");
4028
expect(mask).not.toBeNull();
41-
const loaderSpin = TestUtils.findRenderedDOMComponentWithClass(tree, "loader-spin");
29+
const spinnerContainer = container.querySelector(".spinner-container");
30+
expect(spinnerContainer).not.toBeNull();
31+
const loaderSpin = spinnerContainer.querySelector(".loader-spin");
4232
expect(loaderSpin).not.toBeNull();
4333
});
34+
35+
it("renders container loader mask", () => {
36+
const { container } = render(<ContainerLoaderMask />);
37+
const maskContainer = container.querySelector(".mask-container");
38+
expect(maskContainer).not.toBeNull();
39+
const spinnerContainer = container.querySelector(".spinner-container");
40+
expect(spinnerContainer).not.toBeNull();
41+
const loaderSpin = spinnerContainer.querySelector(".loader-spin");
42+
expect(loaderSpin).not.toBeNull();
43+
});
44+
45+
it("renders small loader", () => {
46+
const { container } = render(<LoaderSmall />);
47+
const loaderSmall = container.querySelector(".loader");
48+
expect(loaderSmall).not.toBeNull();
49+
expect(loaderSmall).toHaveClass("align-self-center");
50+
});
4451
});

tests/__tests__/components/Records.spec.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ describe("Records", function () {
9292
const table = TestUtils.scryRenderedDOMComponentsWithTag(tree, "table");
9393
expect(table).not.toBeNull();
9494
const th = TestUtils.scryRenderedDOMComponentsWithTag(tree, "th");
95-
expect(th.length).toEqual(7);
95+
expect(th.length).toEqual(8);
9696
});
9797

9898
it('renders "Create record" button and click on it', function () {

tests/__tests__/components/User.spec.jsx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ describe("User", function () {
126126
const inputElements = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
127127
const selectElements = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
128128

129-
expect(inputElements.length).toEqual(5);
130-
expect(selectElements.length).toEqual(2);
129+
expect(inputElements.length).toEqual(4);
130+
expect(selectElements.length).toEqual(1);
131131

132132
for (let input of inputElements) {
133133
switch (input.name) {
@@ -298,7 +298,7 @@ describe("User", function () {
298298
}
299299
}
300300
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
301-
expect(selects.length).toEqual(2);
301+
expect(selects.length).toEqual(1);
302302
const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "glyphicon");
303303
expect(randomButton.length).toEqual(0);
304304
});
@@ -344,7 +344,7 @@ describe("User", function () {
344344
}
345345
}
346346
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
347-
expect(selects.length).toEqual(2);
347+
expect(selects.length).toEqual(1);
348348
});
349349

350350
it("renders filled user's form", function () {
@@ -416,9 +416,9 @@ describe("User", function () {
416416
</IntlProvider>,
417417
);
418418
const buttons = TestUtils.scryRenderedDOMComponentsWithTag(tree, "Button");
419-
expect(buttons.length).toEqual(5);
419+
expect(buttons.length).toEqual(4);
420420

421-
TestUtils.Simulate.click(buttons[4]); // cancel
421+
TestUtils.Simulate.click(buttons[3]); // cancel
422422
expect(handlers.onCancel).toHaveBeenCalled();
423423
});
424424

@@ -442,9 +442,9 @@ describe("User", function () {
442442
);
443443
const buttons = TestUtils.scryRenderedDOMComponentsWithTag(tree, "Button");
444444

445-
expect(buttons.length).toEqual(5);
445+
expect(buttons.length).toEqual(4);
446446

447-
TestUtils.Simulate.click(buttons[4]); // back to institution
447+
TestUtils.Simulate.click(buttons[3]); // back to institution
448448
expect(handlers.onCancel).toHaveBeenCalled();
449449
});
450450

tests/__tests__/components/UserRow.spec.jsx

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,15 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
2-
import { render, screen, fireEvent } from "@testing-library/react";
2+
import { screen, fireEvent } from "@testing-library/react";
33
import "@testing-library/jest-dom";
44
import React from "react";
55
import UserRow from "../../../src/components/user/UserRow.jsx";
6-
import { IntlProvider } from "react-intl";
7-
import enLang from "../../../src/i18n/en.js";
86
import { isUsingOidcAuth } from "../../../src/utils/OidcUtils.js";
7+
import { renderWithIntl } from "../../utils/utils.jsx";
98

109
vi.mock("../../../src/utils/OidcUtils", () => ({
1110
isUsingOidcAuth: vi.fn(() => false),
1211
}));
1312

14-
const renderWithIntl = (ui) => {
15-
const intlData = enLang;
16-
return render(
17-
<IntlProvider locale="en" {...intlData}>
18-
{ui}
19-
</IntlProvider>,
20-
);
21-
};
22-
2313
const mockUser = {
2414
uri: "http://onto.fel.cvut.cz/ontologies/record-manager/Admin-Administratorowitch",
2515
firstName: "Test1",
Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,22 @@
11
"use strict";
22

33
import React from "react";
4-
import { IntlProvider } from "react-intl";
5-
import TestUtils from "react-dom/test-utils";
4+
5+
import { screen, fireEvent, within } from "@testing-library/react";
6+
import "@testing-library/jest-dom";
67
import UserTable from "../../../src/components/user/UserTable";
78
import { ACTION_STATUS } from "../../../src/constants/DefaultConstants";
8-
import enLang from "../../../src/i18n/en";
9-
import { describe, expect, it, vi } from "vitest";
9+
import { beforeEach, describe, expect, it, vi } from "vitest";
10+
import { renderWithIntl } from "../../utils/utils.jsx";
1011

1112
describe("UserTable", function () {
12-
const intlData = enLang;
13-
let users,
14-
userDeleted = {
15-
status: ACTION_STATUS.SUCCESS,
16-
},
17-
handlers = {
18-
onEdit: vi.fn(),
19-
onCreate: vi.fn(),
20-
onDelete: vi.fn(),
21-
};
13+
const handlers = {
14+
onEdit: vi.fn(),
15+
onCreate: vi.fn(),
16+
onDelete: vi.fn(),
17+
};
2218

23-
users = [
19+
const users = [
2420
{
2521
uri: "http://onto.fel.cvut.cz/ontologies/record-manager/erter-tert",
2622
firstName: "Test2",
@@ -30,28 +26,17 @@ describe("UserTable", function () {
3026
types: ["http://onto.fel.cvut.cz/ontologies/record-manager/doctor"],
3127
},
3228
];
29+
const renderComponent = (usersList = users, overrides = {}) => {
30+
return renderWithIntl(<UserTable users={usersList} handlers={handlers} {...overrides} />);
31+
};
3332

34-
it("renders table with 5 headers columns", function () {
35-
const tree = TestUtils.renderIntoDocument(
36-
<IntlProvider locale="en" {...intlData}>
37-
<UserTable users={users} userDeleted={userDeleted} handlers={handlers} />
38-
</IntlProvider>,
39-
);
40-
const table = TestUtils.scryRenderedDOMComponentsWithTag(tree, "table");
41-
expect(table).not.toBeNull();
42-
const th = TestUtils.scryRenderedDOMComponentsWithTag(tree, "th");
43-
expect(th.length).toEqual(5);
33+
beforeEach(() => {
34+
vi.clearAllMocks();
4435
});
4536

46-
it('renders modal window by "Delete" button click', function () {
47-
const tree = TestUtils.renderIntoDocument(
48-
<IntlProvider locale="en" {...intlData}>
49-
<UserTable users={users} userDeleted={userDeleted} handlers={handlers} />
50-
</IntlProvider>,
51-
);
52-
const buttons = TestUtils.scryRenderedDOMComponentsWithTag(tree, "Button");
53-
TestUtils.Simulate.click(buttons[1]); // Delete User
54-
const modal = TestUtils.scryRenderedDOMComponentsWithClass(tree, "modal-dialog");
55-
expect(modal).not.toBeNull();
37+
it("shows empty state message when no users", () => {
38+
renderComponent([]);
39+
expect(screen.getByText(/No users/i)).toBeInTheDocument();
40+
expect(screen.queryByRole("rowgroup")).not.toBeInTheDocument();
5641
});
5742
});

0 commit comments

Comments
 (0)