From b3ae17963dd9917d79162f4bd348f87b3d266d8f Mon Sep 17 00:00:00 2001 From: fosterfarrell9 <28628554+fosterfarrell9@users.noreply.github.com> Date: Sat, 24 Aug 2024 17:24:46 +0200 Subject: [PATCH] Add more cypress tests for redemption of tutor vouchers --- .../cypress/user_creator_controller.rb | 1 + app/views/lectures/edit/_tutorials.html.erb | 1 + app/views/tutorials/_form.html.erb | 5 +- app/views/tutorials/_row.html.erb | 3 +- .../cypress/e2e/voucher_redemption_spec.cy.js | 130 +++++++++++++----- 5 files changed, 104 insertions(+), 36 deletions(-) diff --git a/app/controllers/cypress/user_creator_controller.rb b/app/controllers/cypress/user_creator_controller.rb index e611c2c02..9beb3abd3 100644 --- a/app/controllers/cypress/user_creator_controller.rb +++ b/app/controllers/cypress/user_creator_controller.rb @@ -14,6 +14,7 @@ def create is_admin = (role == "admin") user = User.create(name: "#{role} Cypress", email: "#{role}@mampf.cypress", + name_in_tutorials: "#{role} Real Cypress Name", password: CYPRESS_PASSWORD, consents: true, admin: is_admin, locale: I18n.default_locale) user.confirm diff --git a/app/views/lectures/edit/_tutorials.html.erb b/app/views/lectures/edit/_tutorials.html.erb index 62c8a7bc0..37ae6b55c 100644 --- a/app/views/lectures/edit/_tutorials.html.erb +++ b/app/views/lectures/edit/_tutorials.html.erb @@ -10,6 +10,7 @@ new_tutorial_path(params: { lecture_id: lecture.id }), class: 'btn btn-sm btn-primary', id: 'newTutorialButton', + data: { cy: 'new-tutorial-btn' }, remote: true %> diff --git a/app/views/tutorials/_form.html.erb b/app/views/tutorials/_form.html.erb index 80e038fe1..a770ff71f 100644 --- a/app/views/tutorials/_form.html.erb +++ b/app/views/tutorials/_form.html.erb @@ -1,7 +1,7 @@
- <%= form_with model: tutorial do |f| %> + <%= form_with model: tutorial, data: { cy: 'tutorial-form' } do |f| %>
<%= f.text_field :title, @@ -17,7 +17,8 @@ {}, { class: 'selectize', multiple: true, - id: "tutorial_tutor_ids_#{tutorial.id}" } %> + id: "tutorial_tutor_ids_#{tutorial.id}", + data: { cy: "tutor-select"} } %>
<%= f.submit t('buttons.save'), diff --git a/app/views/tutorials/_row.html.erb b/app/views/tutorials/_row.html.erb index dfda617fe..d1ad8a889 100644 --- a/app/views/tutorials/_row.html.erb +++ b/app/views/tutorials/_row.html.erb @@ -1,5 +1,6 @@
+ data-id="<%= tutorial.id.to_i %>" + data-cy="tutorial-row">
<%= tutorial.title %> diff --git a/spec/cypress/e2e/voucher_redemption_spec.cy.js b/spec/cypress/e2e/voucher_redemption_spec.cy.js index ef0857459..346933198 100644 --- a/spec/cypress/e2e/voucher_redemption_spec.cy.js +++ b/spec/cypress/e2e/voucher_redemption_spec.cy.js @@ -1,10 +1,11 @@ import FactoryBot from "../support/factorybot"; function createRedemptionScenario(context) { + cy.createUser("teacher").as("teacher"); cy.createUserAndLogin("generic").as("user"); cy.then(() => { - FactoryBot.create("lecture").as("lecture"); + FactoryBot.create("lecture", { teacher_id: context.teacher.id }).as("lecture"); }); cy.then(() => { @@ -23,7 +24,7 @@ function submitVoucher(voucher) { cy.getBySelector("verify-voucher-submit").click(); } -function verifyVoucherRedemption() { +function verifyVoucherRedemptionText() { cy.getBySelector("redeem-voucher-text").should("be.visible"); } @@ -44,6 +45,77 @@ function redeemVoucherToBecomeTutor(context) { }); } +function createTutorials(context) { + FactoryBot.create("tutorial", { lecture_id: context.lecture.id }).as("tutorial1"); + FactoryBot.create("tutorial", { lecture_id: context.lecture.id }).as("tutorial2"); + FactoryBot.create("tutorial", { lecture_id: context.lecture.id }).as("tutorial3"); +} + +function selectTutorialsAndSubmit(tutorialIds) { + const tutorialIdsAsStrings = tutorialIds.map(id => id.toString()); + + cy.getBySelector("claim-select").should("be.visible"); + cy.getBySelector("claim-select").select(tutorialIdsAsStrings, { force: true }); + cy.getBySelector("claim-submit").click(); + console.log(tutorialIds); + cy.getBySelector("flash-notice").should("be.visible"); +} + +function verifyTutorialRowsContainTutorName(context, tutorialIds) { + cy.getBySelector("tutorial-row").should("have.length", 3).each(($el) => { + const dataId = parseInt($el.attr("data-id"), 10); + console.log(tutorialIds); + console.log(dataId); + if (tutorialIds.includes(dataId)) { + cy.wrap($el).should("contain", context.user.name_in_tutorials); + } + else { + cy.wrap($el).should("not.contain", context.user.name_in_tutorials); + } + }); +} + +function loginAsTeacherAndVisitLectureEdit(context) { + cy.logout(); + cy.login(context.teacher); + cy.visit(`/lectures/${context.lecture.id}/edit`); + cy.getBySelector("people-tab-btn").click(); +} + +function runTutorialTest(tutorialCount) { + it(`allows the user to successfully submit ${tutorialCount} tutorial(s) and become their tutor`, function () { + createTutorials(this); + + let tutorialIds; + + cy.then(() => { + switch (tutorialCount) { + case 1: + tutorialIds = [this.tutorial1.id]; + break; + case 2: + tutorialIds = [this.tutorial1.id, this.tutorial2.id]; + break; + case 3: + tutorialIds = [this.tutorial1.id, this.tutorial2.id, this.tutorial3.id]; + break; + default: + throw new Error("Invalid tutorial count"); + } + submitVoucher(this.voucher); + selectTutorialsAndSubmit(tutorialIds); + }); + + cy.then(() => { + loginAsTeacherAndVisitLectureEdit(this); + }); + + cy.then(() => { + verifyTutorialRowsContainTutorName(this, tutorialIds); + }); + }); +} + describe("Profile page", () => { beforeEach(function () { createRedemptionScenario(this); @@ -54,49 +126,41 @@ describe("Profile page", () => { cy.getBySelector("verify-voucher-form").should("be.visible"); }); - describe("Verify voucher form", () => { - describe("for tutor vouchers", () => { - it("can submit a valid voucher", function () { - submitVoucher(this.voucher); - verifyVoucherRedemption(); - }); - }); - }); - describe("Tutor voucher redemption", () => { describe("if the lecture has no tutorials yet", () => { - it("shows a message that there are no tutorials and a redeem voucher button", function () { + it("allows redemption of voucher to successfully become tutor", function () { submitVoucher(this.voucher); + verifyVoucherRedemptionText(); verifyNoTutorialsYetMessage(this); - }); - it("allows redemption of voucher to become tutor", function () { - submitVoucher(this.voucher); cy.then(() => { redeemVoucherToBecomeTutor(this); }); + + cy.then(() => { + loginAsTeacherAndVisitLectureEdit(this); + }); + + cy.then(() => { + cy.getBySelector("tutorial-row").should("not.exist"); + cy.getBySelector("new-tutorial-btn").should("be.visible").click(); + }); + + cy.then(() => { + cy.getBySelector("tutorial-form").should("be.visible"); + cy.getBySelector("tutor-select").should("be.visible").within(() => { + cy.get("option").should("contain", this.user.name_in_tutorials) + .and("contain", this.user.email) + .and("not.contain", this.user.name); + }); + }); }); }); describe("if the lecture has tutorials", () => { - it("shows a message after submission that there are tutorials and a select form", - function () { - FactoryBot.create("tutorial", { lecture_id: this.lecture.id }) - .as("tutorial1"); - FactoryBot.create("tutorial", { lecture_id: this.lecture.id }) - .as("tutorial2"); - - cy.then(() => { - submitVoucher(this.voucher); - cy.getBySelector("claim-select").should("be.visible"); - cy.getBySelector("claim-select") - .select([this.tutorial1.id, this.tutorial2.id], { force: true }); - cy.getBySelector("claim-submit").click(); - cy.then(() => { - cy.getBySelector("flash-notice").should("be.visible"); - }); - }); - }); + runTutorialTest(1); + runTutorialTest(2); + runTutorialTest(3); }); }); });