Skip to content

Commit

Permalink
[#202] Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
palagdan committed Sep 5, 2024
1 parent c94b109 commit 4ca36b8
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 44 deletions.
4 changes: 2 additions & 2 deletions src/components/user/UserController.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ import {
} from "../../actions/UserActions";
import * as UserFactory from "../../utils/EntityFactory";
import omit from "lodash/omit";
import { getRole } from "../../utils/Utils";
import { isUsingOidcAuth, userProfileLink } from "../../utils/OidcUtils";
import { isAdmin } from "../../utils/SecurityUtils";
import PropTypes from "prop-types";
import { generateRandomUsername } from "../../utils/Utils.js";

class UserController extends React.Component {
constructor(props) {
Expand Down Expand Up @@ -131,7 +131,7 @@ class UserController extends React.Component {
};

_generateUsername = () => {
this.props.generateUsername(getRole(this.state.user).toLowerCase());
this.props.generateUsername(generateRandomUsername().toLowerCase());
};

_getPayload() {
Expand Down
26 changes: 11 additions & 15 deletions src/utils/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@ export function generatePassword() {
return pass;
}

export function generateRandomUsername(length = 8) {
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let username = "";

for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
username += characters[randomIndex];
}

return username;
}
/**
* Checks whether the currently logged in user can view patient records of the specified institutions.
*
Expand All @@ -289,21 +300,6 @@ export function deviceIsMobile() {
);
}

export function getRole(user) {
const userToTest = user;
if (!userToTest) {
return undefined;
}
if (userToTest.types) {
if (userToTest.types.indexOf(Vocabulary.ADMIN_TYPE) !== -1) {
return ROLE.ADMIN;
} else {
return ROLE.DOCTOR;
}
}
return undefined;
}

export function processInstitutions(institutions) {
return institutions.map((item) => {
return { label: item.name, value: item.uri };
Expand Down
2 changes: 1 addition & 1 deletion tests/__tests__/actions/UserActions.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ describe("User asynchronous actions", function () {
currentPassword: "1234",
},
currentUserAdmin = {
role: ROLE.ADMIN,
roles: [ROLE.ADMIN],
},
usernamePrefix = "doctor";

Expand Down
4 changes: 2 additions & 2 deletions tests/__tests__/components/Institution.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ describe("Institution", function () {

user = {
username: "doctor",
role: ROLE.DOCTOR,
roles: [ROLE.DOCTOR],
};
admin = {
username: "admin",
role: ROLE.ADMIN,
roles: [ROLE.ADMIN],
};

institution = {
Expand Down
4 changes: 2 additions & 2 deletions tests/__tests__/components/InstitutionMembers.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ describe("InstitutionMembers", function () {

user = {
username: "user",
role: ROLE.DOCTOR,
roles: [ROLE.DOCTOR],
};

admin = {
username: "admin",
role: ROLE.ADMIN,
roles: [ROLE.ADMIN],
};

userDeleted = {
Expand Down
2 changes: 1 addition & 1 deletion tests/__tests__/components/Records.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ describe("Records", function () {
handlers;
admin = {
username: "admin",
role: ROLE.ADMIN,
roles: [ROLE.ADMIN],
};
records = [
{
Expand Down
28 changes: 9 additions & 19 deletions tests/__tests__/components/User.spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ describe("User", function () {

currentUser = {
username: "test",
role: ROLE.DOCTOR,
roles: [ROLE.DOCTOR],
};
currentUserAdmin = {
username: "test",
role: ROLE.ADMIN,
roles: [ROLE.ADMIN],
};
newUser = {
...newUser,
Expand Down Expand Up @@ -162,7 +162,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(5);
expect(result.length).toEqual(7);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -189,9 +189,7 @@ describe("User", function () {
}
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(2);
expect(selects[1].value).toEqual(ROLE.DOCTOR);

expect(selects.length).toEqual(1);
const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "button-random");
expect(randomButton.length).toEqual(1);
});
Expand Down Expand Up @@ -295,7 +293,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(4);
expect(result.length).toEqual(6);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -318,10 +316,8 @@ describe("User", function () {
}
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(2);
expect(selects.length).toEqual(1);
expect(selects[0].disabled).toBeFalsy();
expect(selects[1].disabled).toBeFalsy();

const randomButton = TestUtils.scryRenderedDOMComponentsWithClass(tree, "glyphicon");
expect(randomButton.length).toEqual(0);
});
Expand All @@ -342,7 +338,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(4);
expect(result.length).toEqual(7);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -365,10 +361,8 @@ describe("User", function () {
}
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(2);
expect(selects[1].value).toEqual(ROLE.ADMIN);
expect(selects.length).toEqual(1);
expect(selects[0].disabled).toBeFalsy();
expect(selects[1].disabled).toBeFalsy();
});

it("renders filled user's form", function () {
Expand All @@ -387,7 +381,7 @@ describe("User", function () {
</IntlProvider>,
);
const result = TestUtils.scryRenderedDOMComponentsWithTag(tree, "input");
expect(result.length).toEqual(4);
expect(result.length).toEqual(5);
for (let input of result) {
switch (input.name) {
case "firstName":
Expand All @@ -409,10 +403,6 @@ describe("User", function () {
break;
}
}
const selects = TestUtils.scryRenderedDOMComponentsWithTag(tree, "select");
expect(selects.length).toEqual(1);
expect(selects[0].value).toEqual(ROLE.ADMIN);
expect(selects[0].disabled).toBeTruthy();
});

it('renders "Cancel" button and click on it', function () {
Expand Down
4 changes: 2 additions & 2 deletions tests/__tests__/reducers/AuthReducer.spec.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";
import * as ActionConstants from "../../../src/constants/ActionConstants";
import { ACTION_STATUS } from "../../../src/constants/DefaultConstants";
import { getRole } from "../../../src/utils/Utils";
import AuthReducer from "../../../src/reducers/AuthReducer";
import UserReducer from "../../../src/reducers/UserReducer";
import { describe, expect, it } from "vitest";
import { getRoles } from "../../../src/utils/SecurityUtils.js";

describe("AuthReducer", function () {
const user = {
Expand Down Expand Up @@ -127,7 +127,7 @@ describe("AuthReducer", function () {
status: ACTION_STATUS.SUCCESS,
user: {
...user,
role: getRole(user),
roles: getRoles(user),
},
testEntry: initialState.testEntry,
};
Expand Down

0 comments on commit 4ca36b8

Please sign in to comment.