Skip to content

Commit

Permalink
Add more cypress tests for redemption of tutor vouchers
Browse files Browse the repository at this point in the history
  • Loading branch information
fosterfarrell9 committed Aug 24, 2024
1 parent cec8dc6 commit b3ae179
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 36 deletions.
1 change: 1 addition & 0 deletions app/controllers/cypress/user_creator_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/views/lectures/edit/_tutorials.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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 %>
</div>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/views/tutorials/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<div class="list-group-item tutorialRow"
data-id="<%= tutorial.id.to_i %>">
<%= form_with model: tutorial do |f| %>
<%= form_with model: tutorial, data: { cy: 'tutorial-form' } do |f| %>
<div class="row">
<div class="form-group col-4">
<%= f.text_field :title,
Expand All @@ -17,7 +17,8 @@
{},
{ class: 'selectize',
multiple: true,
id: "tutorial_tutor_ids_#{tutorial.id}" } %>
id: "tutorial_tutor_ids_#{tutorial.id}",
data: { cy: "tutor-select"} } %>
</div>
<div class="form-group col-4">
<%= f.submit t('buttons.save'),
Expand Down
3 changes: 2 additions & 1 deletion app/views/tutorials/_row.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<div class="list-group-item tutorialRow"
data-id="<%= tutorial.id.to_i %>">
data-id="<%= tutorial.id.to_i %>"
data-cy="tutorial-row">
<div class="row">
<div class="col-4">
<%= tutorial.title %>
Expand Down
130 changes: 97 additions & 33 deletions spec/cypress/e2e/voucher_redemption_spec.cy.js
Original file line number Diff line number Diff line change
@@ -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(() => {
Expand All @@ -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");
}

Expand All @@ -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);
Expand All @@ -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);
});
});
});

0 comments on commit b3ae179

Please sign in to comment.