Skip to content

Commit

Permalink
test: 🚨 added Reminders e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
singhAmandeep007 committed Jul 7, 2024
1 parent 0e78865 commit e5ddf26
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 30 deletions.
103 changes: 103 additions & 0 deletions cypress/pages/Reminders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,109 @@ export class RemindersElements extends LayoutElements {
get root() {
return this.content.findByTestId("reminders");
}

getReminderGroupItemByText(text: string) {
return this.root.findByTestId(`reminder-group-item-${text}`).closest("li");
}

getReminderGroupMenuBtnByText(text: string) {
return this.getReminderGroupItemByText(text).findByTestId("reminder-group-item-menu-btn");
}

get reminderGroupItemMenu() {
return cy.findByTestId("reminder-group-item-menu");
}

get createReminderGroupBtn() {
return this.root.findByTestId("reminder-group-create-btn");
}

get reminderGroupCreateSaveBtn() {
return this.root.findByTestId("reminder-group-create-save");
}

get reminderGroupCreateCancelBtn() {
return this.root.findByTestId("reminder-group-create-cancel");
}

get reminderGroupCreateTextInput() {
return this.root.findByTestId("reminder-group-create-text");
}

createReminderGroup(text: string) {
this.createReminderGroupBtn.click();

this.reminderGroupCreateTextInput.type(text);

this.reminderGroupCreateSaveBtn.click({ force: true });
}

get reminderListTitle() {
return this.root.findByTestId("reminder-list-title");
}
get createReminderBtn() {
return this.root.findByTestId("reminder-create-btn");
}

get reminderCreateSaveBtn() {
return this.root.findByTestId("reminder-item-create-save");
}

get reminderCreateCancelBtn() {
return this.root.findByTestId("reminder-item-create-cancel");
}

get reminderCreateTextInput() {
return this.root.findByTestId("reminder-item-create-text");
}

createReminder(text: string) {
this.createReminderBtn.click();

this.reminderCreateTextInput.type(text);

this.reminderCreateSaveBtn.click({ force: true });
}

getReminderItemByText(text: string) {
return this.root.findByTestId(`reminder-item-${text}`).closest("li");
}

getReminderMenuBtnByText(text: string) {
return this.getReminderItemByText(text).findByTestId("reminder-item-menu-btn");
}

get reminderItemMenu() {
return cy.findByTestId("reminder-item-menu");
}

get deleteReminderMenuItem() {
return this.reminderItemMenu.findByTestId("reminder-item-menuitem-delete");
}

get editReminderMenuItem() {
return this.reminderItemMenu.findByTestId("reminder-item-menuitem-edit");
}

get reminderDueDateMenuItem() {
return this.reminderItemMenu.findByTestId("reminder-item-menuitem-due-date");
}

get pinReminderMenuItem() {
return this.reminderItemMenu.findByTestId("reminder-item-menuitem-pin");
}

get saveReminderDueDateBtn() {
return cy.findByTestId("reminder-item-save-due-date-btn");
}

get reminderDueDateDialog() {
return cy.findByTestId("reminder-item-due-date-dialog");
}

get reminderDueDateTimeInput() {
return this.reminderDueDateDialog.findByTestId("select-time-input");
}
}

export const remindersElements = new RemindersElements();
61 changes: 55 additions & 6 deletions cypress/specs/reminders.cy.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
import { http, HttpResponse } from "msw";
import { format } from "date-fns";

import { urlPrefix } from "services/mocker/msw";
import { buildScenarios, urlPrefix } from "services/mocker/msw";

import { remindersElements } from "../pages";

describe("Reminders Page", () => {
it("should render reminders page content", () => {
it("should be able to create a reminder under a reminder group and add a due date", () => {
const reminderTitle = "Learn Cypress";

cy.visit("/reminders");

remindersElements.root.should("exist");

remindersElements.createReminderGroup("Reminder group title");

remindersElements.root.findByText("Reminder group title").click();

remindersElements.reminderListTitle.should("have.text", "Reminder group title");

remindersElements.createReminder(reminderTitle);

remindersElements.getReminderItemByText(reminderTitle).should("exist");

remindersElements.getReminderMenuBtnByText(reminderTitle).click();

remindersElements.reminderDueDateMenuItem.click();

const dueDate = new Date();

remindersElements.reminderDueDateDialog.findByRole("gridcell", { name: dueDate.getDate().toString() }).click();

remindersElements.reminderDueDateTimeInput.type(format(dueDate, "HH:mm"));

remindersElements.saveReminderDueDateBtn.click();

remindersElements.getReminderItemByText(reminderTitle).should("not.contain", "Reminder group title");

remindersElements.getReminderItemByText(reminderTitle).contains(format(dueDate, "MMM d, yyyy, h:mm a"));
});

it("should handle negative scenario for create reminder", () => {
cy.interceptRequest(
http.get(
http.post(
urlPrefix("/reminders"),
() => {
return HttpResponse.json(null, { status: 500 });
},
{
once: false,
}
{ once: true }
)
);

cy.getMswDb().then((db) => {
buildScenarios(db)
.withReminders(5)
.withReminderGroups({ reminderGroups: ["Personal"], remindersPerGroup: 2 });
});

cy.visit("/reminders");

remindersElements.root.should("exist");

remindersElements.getReminderGroupItemByText("Personal").findByText("Personal").click();

remindersElements.createReminder("Learn Cypress");

cy.contains("Error creating reminder");

remindersElements.createReminder("Learn Cypress");

remindersElements.getReminderItemByText("Learn Cypress").should("exist");
});
});
10 changes: 8 additions & 2 deletions cypress/support/msw.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { setupWorker, type SetupWorker } from "msw/browser";
import { RequestHandler } from "msw";

import { handlers } from "services/mocker/msw";
import { setupHandlers, db } from "services/mocker/msw";

declare global {
namespace Cypress {
// eslint-disable-next-line
interface Chainable {
interceptRequest(...handlers: RequestHandler[]): void;

getMswDb(): Promise<typeof db>;
}
}
}

let worker: SetupWorker;

before(() => {
worker = setupWorker(...handlers);
worker = setupWorker(...setupHandlers(db));

cy.wrap(
worker.start({
Expand All @@ -38,3 +40,7 @@ Cypress.on("test:before:run", () => {
Cypress.Commands.add("interceptRequest", (...handlers: RequestHandler[]) => {
worker.use(...handlers);
});

Cypress.Commands.add("getMswDb", () => {
return Promise.resolve(db);
});
1 change: 1 addition & 0 deletions src/components/DateTimePicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const TimeField = ({
value={time}
onChange={handleChange}
disabled={disabled}
data-testid="select-time-input"
/>
</div>
</div>
Expand Down
9 changes: 5 additions & 4 deletions src/views/Reminders/ReminderGroupItem/ReminderGroupItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const ReminderGroupItem: FC<PropsWithChildren<TReminderGroupItemProps>> =
return (
<div
className={cn("flex cursor-pointer items-center justify-between gap-2 px-1 py-2")}
data-testid={`reminder-group-item-${reminderGroup ? reminderGroup.id : "all"}`}
data-testid={`reminder-group-item-${reminderGroup ? reminderGroup.name : "all"}`}
>
{renderItem}

Expand All @@ -80,7 +80,7 @@ export const ReminderGroupItem: FC<PropsWithChildren<TReminderGroupItemProps>> =
<Button
variant="outline"
size="icon"
data-testid={`reminder-group-item-menu-${reminderGroup.id}`}
data-testid={`reminder-group-item-menu-btn`}
disabled={isMenuDisabled}
>
{isDropdownOpen ? <ChevronUp className="icon" /> : <ChevronDown className="icon" />}
Expand All @@ -90,10 +90,11 @@ export const ReminderGroupItem: FC<PropsWithChildren<TReminderGroupItemProps>> =
align="end"
onInteractOutside={() => setIsDropdownOpen(false)}
className="min-w-min"
data-testid={`reminder-group-item-menu`}
>
<DropdownMenuItem
onClick={() => setIsUpdating(true)}
data-testid={`reminder-group-item-update-${reminderGroup.id}`}
data-testid={`reminder-group-item-update-${reminderGroup.name}`}
className="group"
>
<Pencil
Expand All @@ -105,7 +106,7 @@ export const ReminderGroupItem: FC<PropsWithChildren<TReminderGroupItemProps>> =
<DropdownMenuSeparator />
<DropdownMenuItem
onClick={() => handleOnDelete(reminderGroup.id)}
data-testid={`reminder-group-item-delete-${reminderGroup.id}`}
data-testid={`reminder-group-item-delete-${reminderGroup.name}`}
className="group"
>
<Trash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ const testMswServer = createTestMswServer();

describe("ReminderGroupsList", () => {
const setup = () => {
const getAddListBtn = () => screen.getByRole("button", { name: "Add List" });
const getSaveBtn = () => screen.getByTestId("reminder-group-item-create-save");
const getCancelBtn = () => screen.getByTestId("reminder-group-item-create-cancel");
const getTextInput = () => screen.getByTestId("reminder-group-item-create-text");
const getAddListBtn = () => screen.getByTestId("reminder-group-create-btn");
const getSaveBtn = () => screen.getByTestId("reminder-group-create-save");
const getCancelBtn = () => screen.getByTestId("reminder-group-create-cancel");
const getTextInput = () => screen.getByTestId("reminder-group-create-text");

return {
result: render(<ReminderGroupsList />),
Expand Down
7 changes: 4 additions & 3 deletions src/views/Reminders/ReminderGroupsList/ReminderGroupsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export const ReminderGroupsList: FC<PropsWithChildren<TReminderGroupsListProps>>
size={"full"}
onClick={() => setIsCreating((isCreating) => !isCreating)}
disabled={isCreating}
data-testid={"reminder-group-create-btn"}
>
Add List
</AddButton>
Expand Down Expand Up @@ -48,9 +49,9 @@ export const ReminderGroupsList: FC<PropsWithChildren<TReminderGroupsListProps>>
setIsCreating(false);
}}
testIds={{
cancel: `reminder-group-item-create-cancel`,
save: `reminder-group-item-create-save`,
text: `reminder-group-item-create-text`,
cancel: `reminder-group-create-cancel`,
save: `reminder-group-create-save`,
text: `reminder-group-create-text`,
}}
/>
)}
Expand Down
Loading

0 comments on commit e5ddf26

Please sign in to comment.