Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a cypress test to assign a volunteer to a patient #9167

Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions cypress/e2e/patient_spec/PatientVolunteer.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import LoginPage from "../../pageobject/Login/LoginPage";
import { PatientConsultationPage } from "../../pageobject/Patient/PatientConsultation";
import { PatientPage } from "../../pageobject/Patient/PatientCreation";
import { PatientDetailsPage } from "../../pageobject/Patient/PatientDetails";

describe("Assign a volunteer to a patient - Multiple Tests", () => {
const loginPage = new LoginPage();
const patientPage = new PatientPage();
const patientConsultationPage = new PatientConsultationPage();
const patientDetailsPage = new PatientDetailsPage();
const patient = "Dummy Patient 16";
const volunteerName = "dummy volunteer";
const anotherVolunteerName = "Abhi Patil";

before(() => {
loginPage.loginByRole("districtAdmin");
cy.saveLocalStorage();
});

beforeEach(() => {
cy.restoreLocalStorage();
cy.clearLocalStorage(/filters--.+/);
cy.request("/patients").its("status").should("eq", 200);

cy.visit("/patients");
cy.wrap(null, { timeout: 10000 }).then(() => {
patientPage.visitPatient(patient);
});

cy.get("#patient-details").should("exist");
patientConsultationPage.clickPatientDetails();
});

it("should assign a new volunteer successfully", () => {
patientDetailsPage.clickAssignOrReassignVolunteer();
patientDetailsPage.selectAndAssignVolunteer(volunteerName);
});

it("should replace existing volunteer successfully", () => {
patientDetailsPage.clickAssignOrReassignVolunteer();
patientDetailsPage.selectAndAssignVolunteer(anotherVolunteerName);
});

it("should unassign volunteer successfully", () => {
patientDetailsPage.clickAssignOrReassignVolunteer();
patientDetailsPage.unassignAndPrepareForReassignment();
patientDetailsPage.verifyBannerIsRemovedAfterUnassign();
});

it("should handle volunteer not found in dropdown", () => {
patientDetailsPage.clickAssignOrReassignVolunteer();
patientDetailsPage.searchVolunteer("Non-existent Volunteer");
cy.get('[data-testid="no-results"]').should("be.visible");
});
});
57 changes: 57 additions & 0 deletions cypress/pageobject/Patient/PatientDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export class PatientDetailsPage {
clickAssignToVolunteer() {
cy.verifyAndClickElement("#assign-volunteer", "Assign to a Volunteer");
}

clickReassignToVolunteer() {
cy.verifyAndClickElement("#assign-volunteer", "Reassign Volunteer");
}

clickAssignOrReassignVolunteer() {
cy.get("#assign-volunteer")
.scrollIntoView()
.should("be.visible")
.should("be.enabled")
.invoke("text")
.then((text) => {
if (text.includes("Assign to a Volunteer")) {
cy.verifyAndClickElement(
"#assign-volunteer",
"Assign to a Volunteer",
);
} else if (text.includes("Reassign Volunteer")) {
cy.verifyAndClickElement("#assign-volunteer", "Reassign Volunteer");
} else {
throw new Error("Expected button text not found.");
}
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Simplify implementation and remove redundant scroll.

The current implementation can be simplified by:

  1. Removing redundant scrollIntoView as verifyAndClickElement handles visibility
  2. Using the existing methods instead of duplicating logic
  3. Improving error message clarity
 clickAssignOrReassignVolunteer() {
   cy.get("#assign-volunteer")
-    .scrollIntoView()
     .should("be.visible")
     .should("be.enabled")
     .invoke("text")
     .then((text) => {
       if (text.includes("Assign to a Volunteer")) {
-        cy.verifyAndClickElement(
-          "#assign-volunteer",
-          "Assign to a Volunteer",
-        );
+        this.clickAssignToVolunteer();
       } else if (text.includes("Reassign Volunteer")) {
-        cy.verifyAndClickElement("#assign-volunteer", "Reassign Volunteer");
+        this.clickReassignToVolunteer();
       } else {
-        throw new Error("Expected button text not found.");
+        throw new Error(
+          `Button text must be either "Assign to a Volunteer" or "Reassign Volunteer", but found: "${text}"`
+        );
       }
     });
 }

Committable suggestion skipped: line range outside the PR's diff.


selectAndAssignVolunteer(volunteerName: string) {
cy.clickAndSelectOption("#assign_volunteer", volunteerName);
cy.clickSubmitButton("Assign");
cy.wait(1000);
cy.verifyContentPresence("#assigned-volunteer", [volunteerName]);
}

unassignAndPrepareForReassignment() {
cy.get("#clear-button").should("be.visible").click();
cy.get("#dropdown-toggle").should("be.visible").click();
cy.clickSubmitButton("Unassign");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix inconsistency between method name and implementation.

The method name suggests preparation for reassignment, but it performs an unassignment:

  1. Method name implies reassignment preparation
  2. Implementation uses "Unassign" button text
  3. No preparation for reassignment is performed
-  unassignAndPrepareForReassignment() {
+  unassignVolunteer() {
     cy.get("#clear-button").should("be.visible").click();
     cy.get("#dropdown-toggle").should("be.visible").click();
     cy.clickSubmitButton("Unassign");
   }

Committable suggestion skipped: line range outside the PR's diff.


verifyBannerIsRemovedAfterUnassign() {
cy.get("#assigned-volunteer", { timeout: 10000 }).should("not.exist");
}

searchVolunteer(volunteerName: string) {
cy.get("#assign_volunteer")
.should("be.visible")
.click()
.type(volunteerName);

cy.get("[data-testid='volunteer-search-results']", {
timeout: 10000,
}).should("be.visible");
}
}
6 changes: 5 additions & 1 deletion src/components/Form/FormFields/Autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,7 @@ export const Autocomplete = <T, V>(props: AutocompleteProps<T, V>) => {

<DropdownTransition>
<ComboboxOptions
data-testid="volunteer-search-results"
modal={false}
as="ul"
className="cui-dropdown-base absolute z-10 mt-0.5 origin-top-right"
Expand All @@ -249,7 +250,10 @@ export const Autocomplete = <T, V>(props: AutocompleteProps<T, V>) => {
{`Please enter at least ${props.minQueryLength} characters to search`}
</div>
) : filteredOptions.length === 0 ? (
<div className="p-2 text-sm text-secondary-500">
<div
data-testid="no-results"
className="p-2 text-sm text-secondary-500"
>
No options found
</div>
) : (
Expand Down
2 changes: 1 addition & 1 deletion src/components/Patient/PatientHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ export const PatientHome = (props: {
)}

{patientData.assigned_to_object && (
<div>
<div id="assigned-volunteer">
<p className="text-xs font-normal leading-tight text-gray-600">
{t("assigned_volunteer")}:
</p>
Expand Down