Skip to content

Commit

Permalink
test: fix cypress tests
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Mar 4, 2024
1 parent 997cab4 commit 76b6d12
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,42 @@ import { Given, When, Then, defineStep as And } from '@badeball/cypress-cucumber
import '../sharedSteps';
import '../../sharedSteps';

const changeEnrollmentAndEventsStatus = () => (
cy.buildApiUrl(
'tracker',
'trackedEntities/bj4UmUpqaSp?program=qDkgAbB5Jlk&fields=enrollments[enrollment,events,orgUnit,program,enrolledAt,trackedEntity]',
)
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'FZAa7j0muDj');
const eventToDelete = enrollment.events.find(e => e.programStage === 'eHvTba5ijAh');
const { events, ...rest } = enrollment;
const enrollmentToUpdate = { ...rest, status: 'ACTIVE' };

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
if (eventToDelete) {
cy.buildApiUrl('events', eventToDelete.event)
.then((eventUrl) => {
cy.request('DELETE', eventUrl);
}).then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
} else {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
}
});
})
);

const showAllEventsInProgramStage = () => {
cy.get('[data-test="dhis2-uicore-tablefoot"]')
.then(($footer) => {
Expand Down Expand Up @@ -159,39 +195,7 @@ Then('the user clicks the first second antenatal care visit event', () => {
});

And('the enrollment status is active', () => {
cy.buildApiUrl(
'tracker',
'trackedEntities/bj4UmUpqaSp?program=qDkgAbB5Jlk&fields=enrollments[enrollment,events,orgUnit,program,enrolledAt,trackedEntity]',
)
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'FZAa7j0muDj');
const eventToDelete = enrollment.events.find(e => e.programStage === 'eHvTba5ijAh');
const { events, ...rest } = enrollment;
const enrollmentToUpdate = { ...rest, status: 'ACTIVE' };

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
if (eventToDelete) {
cy.buildApiUrl('events', eventToDelete.event)
.then((eventUrl) => {
cy.request('DELETE', eventUrl);
}).then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
} else {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
}
});
});
changeEnrollmentAndEventsStatus();
});

And('the user completes the event', () => {
Expand Down Expand Up @@ -225,4 +229,5 @@ Then('the user sees the enrollment status and recently added event in Case outco
.within(() => {
cy.get('[data-test="dhis2-uicore-tag-text"]').contains('Completed').should('exist');
});
changeEnrollmentAndEventsStatus();
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,33 @@ import { Given, When, Then, defineStep as And } from '@badeball/cypress-cucumber
import { getCurrentYear } from '../../../support/date';
import '../../sharedSteps';

const changeEnrollmentAndEventsStatus = () => (
cy.buildApiUrl(
'tracker',
'trackedEntities/JM29jwvw8Ub?program=qDkgAbB5Jlk&fields=enrollments[enrollment,events,orgUnit,program,enrolledAt,trackedEntity]',
)
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'C4iB0VTbfrK');
const eventToUpdate = enrollment.events.find((e => e.programStage === 'eHvTba5ijAh'));
const enrollmentToUpdate = {
...enrollment,
status: 'ACTIVE',
events: [{ ...eventToUpdate, status: 'ACTIVE' }],
};

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
})
);

Given(/^you land on the enrollment event page with selected (.*) by having typed (.*)$/, (tet, url) => {
cy.visit(url);
cy.get('[data-test="scope-selector"]').contains(`${tet}`);
Expand Down Expand Up @@ -97,30 +124,7 @@ Then(/^the user see the schedule date field with tooltip: (.*)$/, (tooltipConten
});

And('the enrollment status is active', () => {
cy.buildApiUrl(
'tracker',
'trackedEntities/JM29jwvw8Ub?program=qDkgAbB5Jlk&fields=enrollments[enrollment,events,orgUnit,program,enrolledAt,trackedEntity]',
)
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'C4iB0VTbfrK');
const eventToUpdate = enrollment.events.find((e => e.programStage === 'eHvTba5ijAh'));
const enrollmentToUpdate = {
...enrollment,
status: 'ACTIVE',
events: [{ ...eventToUpdate, status: 'ACTIVE' }],
};

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
});
changeEnrollmentAndEventsStatus();
});

And('the user completes the event', () => {
Expand Down Expand Up @@ -161,5 +165,6 @@ Then('the user sees the enrollment status and recently edited event in Case outc
.within(() => {
cy.get('[data-test="dhis2-uicore-tag-text"]').contains('Completed').should('exist');
});
changeEnrollmentAndEventsStatus();
});

42 changes: 22 additions & 20 deletions cypress/e2e/WidgetsForEnrollmentPages/WidgetEnrollment/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import { When, Then } from '@badeball/cypress-cucumber-preprocessor';
import { getCurrentYear } from '../../../support/date';

const changeEnrollmentAndEventsStatus = () => (
cy.buildApiUrl('tracker', 'trackedEntities/osF4RF4EiqP?program=IpHINAT79UW&fields=enrollments')
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'qyx7tscVpVB');
const eventsToUpdate = enrollment.events.map(e => ({ ...e, status: 'ACTIVE' }));
const enrollmentToUpdate = { ...enrollment, status: 'ACTIVE', events: eventsToUpdate };

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
})
);

When('you click the enrollment widget toggle open close button', () => {
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-open-close-toggle-button"]').click();
Expand Down Expand Up @@ -118,26 +138,7 @@ Then(/^the user sees the delete enrollment modal/, () =>
);

Then('the user sees the enrollment status and the Baby Postnatal event status is active', () => {
cy.buildApiUrl('tracker', 'trackedEntities/osF4RF4EiqP?program=IpHINAT79UW&fields=enrollments')
.then(url => cy.request(url))
.then(({ body }) => {
const enrollment = body.enrollments && body.enrollments.find(e => e.enrollment === 'qyx7tscVpVB');
const eventsToUpdate = enrollment.events.reduce(
(acc, e) => [...acc, e.programStage === 'ZzYYXq4fJie' ? { ...e, status: 'ACTIVE' } : e],
[],
);
const enrollmentToUpdate = { ...enrollment, status: 'ACTIVE', events: eventsToUpdate };

return cy
.buildApiUrl('tracker?async=false&importStrategy=UPDATE')
.then(enrollmentUrl => cy.request('POST', enrollmentUrl, { enrollments: [enrollmentToUpdate] }))
.then(() => {
cy.reload();
cy.get('[data-test="widget-enrollment"]').within(() => {
cy.get('[data-test="widget-enrollment-status"]').contains('Active').should('exist');
});
});
});
changeEnrollmentAndEventsStatus();
});

Then('the user sees the enrollment status and the Baby Postnatal event status is completed', () => {
Expand All @@ -151,6 +152,7 @@ Then('the user sees the enrollment status and the Baby Postnatal event status is
.within(() => {
cy.get('[data-test="dhis2-uicore-tag-text"]').contains('Completed').should('exist');
});
changeEnrollmentAndEventsStatus();
});

When('the user completes the enrollment and the active events', () => {
Expand Down

0 comments on commit 76b6d12

Please sign in to comment.