Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

7.9 KB
Raw Blame
const { H } = cy;
import { SAMPLE_DATABASE } from "e2e/support/cypress_sample_database";

const { ORDERS_ID } = SAMPLE_DATABASE;

const getQuestionDetails = () => ({
  name: "Question",
  query: {
    "source-table": ORDERS_ID,
  },
});

const getPulseDetails = ({ card_id, dashboard_id }) => ({
  name: "Subscription",
  dashboard_id,
  cards: [
    {
      id: card_id,
      include_csv: false,
      include_xls: false,
    },
  ],
  channels: [
    {
      enabled: true,
      channel_type: "slack",
      schedule_type: "hourly",
    },
  ],
});

describe("scenarios > account > notifications", () => {
  beforeEach(() => {
    H.restore();
  });

  describe("notifications", () => {
    beforeEach(() => {
      cy.signInAsAdmin().then(() => {
        H.getCurrentUser().then(({ body: { id: admin_id } }) => {
          cy.wrap(admin_id).as("admin_id");

          cy.signInAsNormalUser().then(() => {
            H.getCurrentUser().then(({ body: { id: user_id } }) => {
              cy.wrap(user_id).as("user_id");

              H.createQuestion(getQuestionDetails()).then(
                ({ body: { id: card_id } }) => {
                  cy.wrap(card_id).as("card_id");

                  H.createQuestionAlert({
                    user_id: admin_id,
                    card_id,
                    handlers: [
                      {
                        channel_type: "channel/email",
                        recipients: [
                          {
                            type: "notification-recipient/user",
                            user_id,
                            details: null,
                          },
                          {
                            type: "notification-recipient/user",
                            user_id: admin_id,
                            details: null,
                          },
                        ],
                      },
                    ],
                  });
                },
              );
            });
          });
        });
      });
    });

    it("should be able to delete an alert when the user created it and he is a single recipient", () => {
      openUserNotifications();

      cy.intercept("GET", "/api/notification/*").as("getAlert");
      cy.intercept("POST", "/api/notification/*/unsubscribe").as(
        "alertUnsubscribe",
      );
      cy.intercept("PUT", "/api/notification/*").as("alertDelete");

      cy.findByTestId("notifications-list")
        .should("contain", "Question")
        .and("contain", "Check daily at 9:00 AM")
        .and("contain", "Created by you");
      checkNotificationHelp();

      clickUnsubscribe();

      cy.wait("@getAlert");

      cy.findByTestId("alert-unsubscribe").within(() => {
        cy.findByText("Confirm you want to unsubscribe");
        cy.findByText("Unsubscribe").click();
      });

      cy.wait("@alertUnsubscribe");
      H.undoToastList()
        .findByText("Successfully unsubscribed.")
        .should("exist");

      cy.findByTestId("alert-delete").within(() => {
        cy.findByText("You’re unsubscribed. Delete this alert as well?");
        cy.findByText("Delete it").click();
      });

      cy.wait("@alertDelete");
      H.notificationList()
        .findByText("The alert was successfully deleted.")
        .should("exist");

      H.modal().should("not.exist");
      cy.findByTestId("notifications-list").should("not.exist");
    });

    it("should be able to unsubscribe from an alert when the user has not created it", () => {
      cy.signOut();
      cy.signInAsAdmin();
      openUserNotifications();

      cy.intercept("POST", "/api/notification/*/unsubscribe").as(
        "alertUnsubscribe",
      );

      cy.findByTestId("notifications-list")
        .findByText("Created by Robert Tableton", { exact: false })
        .should("be.visible");

      clickUnsubscribe();

      // Wait for the confirm modal's entrance transition to settle before
      // clicking. A bare findByText resolves the instant the title mounts, while
      // the modal is still animating in and its title transiently overlaps the
      // Unsubscribe button — clicking then fails "covered by another element".
      // Anchoring on the title being visible defers the click until the modal
      // has finished opening.
      H.modal().within(() => {
        cy.findByText("Confirm you want to unsubscribe").should("be.visible");
        cy.findByText("Unsubscribe").click();
      });

      cy.wait("@alertUnsubscribe");

      // This was the admin's only notification and they didn't create it, so
      // unsubscribing empties the list and the notifications-list container
      // unmounts into the empty state. Anchor on the success toast first, then
      // assert the list is gone — don't scope the negative check inside the
      // container that disappears.
      H.undoToastList()
        .findByText("Successfully unsubscribed.")
        .should("be.visible");

      cy.findByTestId("notifications-list").should("not.exist");
    });

    it("should be able to see created notifications that a user is not subscribed to", () => {
      cy.get("@admin_id").then((admin_id) =>
        cy.get("@card_id").then((card_id) => {
          H.createQuestionAlert({
            admin_id,
            card_id,
            cron_schedule: "0 0 3 * * ?",
            handlers: [
              {
                channel_type: "channel/email",
                recipients: [
                  {
                    type: "notification-recipient/user",
                    user_id: admin_id,
                    details: null,
                  },
                ],
              },
            ],
          });
        }),
      );

      openUserNotifications();

      cy.findByTestId("notifications-list").within(() => {
        cy.findByText("Check daily at 3:00 AM").should("exist");

        const notificationCard = () =>
          cy
            .findByText("Check daily at 3:00 AM")
            .closest("[data-testid=notification-alert-item]")
            .should("exist");

        notificationCard().within(() => {
          cy.findByText("Created by you", { exact: false }).should("exist");
          cy.icon("close").should("exist").click();
        });
      });

      H.modal().findByText("Delete this alert?").should("exist");
    });
  });

  describe("pulses", () => {
    beforeEach(() => {
      cy.signInAsNormalUser().then(() => {
        H.createQuestionAndDashboard({
          questionDetails: getQuestionDetails(),
        }).then(({ body: { card_id, dashboard_id } }) => {
          H.createPulse(getPulseDetails({ card_id, dashboard_id }));
        });
      });
    });

    it("should be able to unsubscribe and delete a pulse when the user has created it", () => {
      openUserNotifications();

      cy.findByTestId("notifications-list")
        .should("contain", "Subscription")
        .and("contain", "Slack’d hourly")
        .and("contain", "Created by you");
      cy.intercept("PUT", "/api/pulse/*").as("archivePulse");
      clickUnsubscribe();

      H.modal().within(() => {
        cy.findByText("Delete this subscription?");
        cy.findByText("Yes, delete this subscription").click();
      });

      cy.wait("@archivePulse").its("response.body.archived").should("eq", true);
      H.modal().should("not.exist");
      cy.findByTestId("notifications-list").should("not.exist");
    });
  });
});

function clickUnsubscribe() {
  cy.findByTestId("notifications-list").within(() => {
    cy.findByLabelText("close icon").click();
  });
}

function openUserNotifications() {
  cy.intercept("GET", "/api/pulse?*").as("loadSubscriptions");
  cy.visit("/account/notifications");
  cy.wait("@loadSubscriptions");
}

function checkNotificationHelp() {
  H.main().findByText("Not seeing one here?").click();
  H.modal().within(() => {
    cy.findByText("Not seeing something listed here?").should("be.visible");
    cy.findByRole("button", { name: "Got it" }).click();
  });
  H.modal().should("not.exist");
}