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 8 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
67 changes: 67 additions & 0 deletions cypress/e2e/patient_spec/PatientVolunteer.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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", () => {
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(() => {
cy.log("Logging in as district admin");
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cy.log("Logging in as district admin");
cy.log("Logging in as district admin");

remove unwanted logging

loginPage.loginAsDistrictAdmin();
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
loginPage.loginAsDistrictAdmin();
loginPage.loginByRole("districtAdmin");

cy.saveLocalStorage();
});

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

cy.visit("/patients").then(() => {
cy.log("Successfully navigated to patients page");
});

// Add timeout and retry strategy for patient search
cy.wrap(null, { timeout: 10000 }).then(() => {
patientPage.visitPatient(patient);
});

// Verify patient details page is accessible
cy.get("#patient-details").should("exist");
patientConsultationPage.clickPatientDetails();
});

describe("volunteer assignment workflow", () => {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
describe("volunteer assignment workflow", () => {
describe("volunteer assignment workflow", () => {

Combine everything into one single test

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

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

it("should unassign volunteer successfully", () => {
patientDetailsPage.clickAssignToVolunteer();
patientDetailsPage.unassignAndPrepareForReassignment();
patientDetailsPage.verifyBannerIsRemovedAfterUnassign();
});
});
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

Add test coverage for error states and edge cases

While the happy path scenarios are well covered, consider adding tests for error states and edge cases.

Add these test cases:

describe("error states and edge cases", () => {
  it("should handle volunteer not found in dropdown", () => {
    patientDetailsPage.clickAssignToVolunteer();
    patientDetailsPage.searchVolunteer("Non-existent Volunteer");
    cy.get('[data-testid="no-results"]').should('be.visible');
  });

  it("should handle assignment cancellation", () => {
    patientDetailsPage.clickAssignToVolunteer();
    patientDetailsPage.cancelAssignment();
    patientDetailsPage.verifyAssignmentModalClosed();
  });

  it("should prevent duplicate volunteer assignment", () => {
    // Assuming the volunteer is already assigned
    patientDetailsPage.clickAssignToVolunteer();
    patientDetailsPage.selectAndAssignVolunteer(volunteerName);
    cy.get('[data-testid="error-message"]')
      .should('contain', 'Volunteer already assigned');
  });
});

Also, verify the UI state between actions:

 it("should replace existing volunteer successfully", () => {
   patientDetailsPage.clickAssignToVolunteer();
+  // Verify current volunteer is shown as selected
+  cy.get('[data-testid="current-volunteer"]')
+    .should('contain', volunteerName);
   patientDetailsPage.selectAndAssignVolunteer(anotherVolunteerName);
+  // Verify loading state during assignment
+  cy.get('[data-testid="loading-indicator"]')
+    .should('exist')
+    .should('not.exist');
   patientDetailsPage.verifyVolunteerBannerIsUpdated(anotherVolunteerName);
 });


describe("error states and edge cases", () => {
it("should handle volunteer not found in dropdown", () => {
patientDetailsPage.clickAssignToVolunteer();
patientDetailsPage.searchVolunteer("Non-existent Volunteer");
cy.get('[data-testid="no-results"]').should("be.visible");
});
});
});
46 changes: 46 additions & 0 deletions cypress/pageobject/Patient/PatientDetails.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
export class PatientDetailsPage {
clickAssignToVolunteer() {
cy.contains("button", "Assign to a Volunteer")
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cy.contains("button", "Assign to a Volunteer")
cy.contains("button", "Assign to a Volunteer")

use proper Id for the button

Copy link
Member

Choose a reason for hiding this comment

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

check for cy.verifyAndClickElement() function after you give them the id

.scrollIntoView()
.should("be.visible")
.should("be.enabled")
.click();
}

selectAndAssignVolunteer(volunteerName: string) {
cy.clickAndSelectOption("#assign_volunteer", volunteerName);
cy.clickSubmitButton("Assign");
cy.get("#assigned-volunteer", { timeout: 10000 })
Copy link
Member

Choose a reason for hiding this comment

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

check for cy.verifyAndClickElement() function

.scrollIntoView()
.should("be.visible")
.should("contain.text", volunteerName);
}

verifyVolunteerBannerIsUpdated(volunteerName: string) {
cy.get("#assigned-volunteer").should(
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
cy.get("#assigned-volunteer").should(
cy.get("#assigned-volunteer").should(

check the function cy.verifyContentPresence

"contain.text",
`Assigned Volunteer:${volunteerName}`,
);
}

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

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 @@ -423,7 +423,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