Skip to content

Commit

Permalink
clean cypress tests and add auxiliary functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ritosilva committed Feb 9, 2024
1 parent 04fdea5 commit ab6fafa
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 47 deletions.
77 changes: 30 additions & 47 deletions frontend/tests/e2e/specs/activity/activity.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
describe('Activity', () => {
beforeEach(() => {
cy.deleteAllButArs()
cy.deleteAllButArs();
cy.createDemoEntities();
});

afterEach(() => {
cy.deleteAllButArs()
cy.deleteAllButArs();
});

it('create activities', () => {
Expand Down Expand Up @@ -46,21 +47,15 @@ describe('Activity', () => {
cy.get('[data-cy="memberActivitiesTable"] tbody tr')
.should('have.length', 1)
.eq(0)
.then(($row) => {
cy.wrap($row).children()
.should('have.length', 11)
.each(($column, columnIndex) => {
if (columnIndex === 0) {
cy.wrap($column).invoke('text').should('equal', NAME);
} else if (columnIndex === 1) {
cy.wrap($column).invoke('text').should('equal', REGION);
} else if (columnIndex === 2) {
cy.wrap($column).invoke('text').should('equal', NUMBER);
} else if (columnIndex === 4) {
cy.wrap($column).invoke('text').should('equal', DESCRIPTION);
}
});
})
.children()
.should('have.length', 11)
.eq(0).should('contain', NAME)
.parent().children()
.eq(1).should('contain', REGION)
.parent().children()
.eq(2).should('contain', NUMBER)
.parent().children()
.eq(4).should('contain', DESCRIPTION);
cy.logout();

cy.demoVolunteerLogin();
Expand All @@ -74,21 +69,15 @@ describe('Activity', () => {
cy.get('[data-cy="volunteerActivitiesTable"] tbody tr')
.should('have.length', 1)
.eq(0)
.then(($row) => {
cy.wrap($row).children()
.should('have.length', 10)
.each(($column, columnIndex) => {
if (columnIndex === 0) {
cy.wrap($column).invoke('text').should('equal', NAME);
} else if (columnIndex === 1) {
cy.wrap($column).invoke('text').should('equal', REGION);
} else if (columnIndex === 2) {
cy.wrap($column).invoke('text').should('equal', NUMBER);
} else if (columnIndex === 4) {
cy.wrap($column).invoke('text').should('equal', DESCRIPTION);
}
});
})
.children()
.should('have.length', 10)
.eq(0).should('contain', NAME)
.parent().children()
.eq(1).should('contain', REGION)
.parent().children()
.eq(2).should('contain', NUMBER)
.parent().children()
.eq(4).should('contain', DESCRIPTION);
cy.logout();

cy.demoAdminLogin();
Expand All @@ -108,21 +97,15 @@ describe('Activity', () => {
cy.get('[data-cy="adminActivitiesTable"] tbody tr')
.should('have.length', 1)
.eq(0)
.then(($row) => {
cy.wrap($row).children()
.should('have.length', 14)
.each(($column, columnIndex) => {
if (columnIndex === 1) {
cy.wrap($column).invoke('text').should('equal', NAME);
} else if (columnIndex === 2) {
cy.wrap($column).invoke('text').should('equal', REGION);
} else if (columnIndex === 3) {
cy.wrap($column).invoke('text').should('equal', NUMBER);
} else if (columnIndex === 4) {
cy.wrap($column).invoke('text').should('equal', DESCRIPTION);
}
});
})
.children()
.should('have.length', 14)
.eq(1).should('contain', NAME)
.parent().children()
.eq(2).should('contain', REGION)
.parent().children()
.eq(3).should('contain', NUMBER)
.parent().children()
.eq(4).should('contain', DESCRIPTION);
cy.logout();
});
});
63 changes: 63 additions & 0 deletions frontend/tests/e2e/support/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ const credentials = {
port: Cypress.env('psql_db_port'),
};

const INSTITUTION_COLUMNS = "institutions (id, active, confirmation_token, creation_date, email, name, nif, token_generation_date)";
const USER_COLUMNS = "users (user_type, id, creation_date, name, role, state, institution_id)";
const AUTH_USERS_COLUMNS = "auth_users (auth_type, id, active, email, username, user_id)";

const now = new Date();
const tomorrow = new Date(now);
tomorrow.setDate(now.getDate() + 1);
const dayAfterTomorrow = new Date(now);
dayAfterTomorrow.setDate(now.getDate() + 2);
const yesterday = new Date(now);
yesterday.setDate(now.getDate() - 1);
const dayBeforeYesterday = new Date(now);
dayBeforeYesterday.setDate(now.getDate() - 2);

Cypress.Commands.add('deleteAllButArs', () => {
cy.task('queryDatabase', {
query: "DELETE FROM ACTIVITY",
Expand All @@ -19,4 +33,53 @@ Cypress.Commands.add('deleteAllButArs', () => {
query: "DELETE FROM USERS WHERE NOT (name = 'ars')",
credentials: credentials,
});
cy.task('queryDatabase', {
query: "DELETE FROM INSTITUTIONS",
credentials: credentials,
});
});

Cypress.Commands.add('createDemoEntities', () => {
cy.task('queryDatabase', {
query: "INSERT INTO " + INSTITUTION_COLUMNS + generateInstitutionTuple(1),
credentials: credentials,
})
cy.task('queryDatabase', {
query: "INSERT INTO " + USER_COLUMNS + generateUserTuple(2, "MEMBER","DEMO-MEMBER", "MEMBER", 1),
credentials: credentials,
})
cy.task('queryDatabase', {
query: "INSERT INTO " + AUTH_USERS_COLUMNS + generateAuthUserTuple(2, "DEMO", "demo-member", 2),
credentials: credentials,
})
cy.task('queryDatabase', {
query: "INSERT INTO " + USER_COLUMNS + generateUserTuple(3, "VOLUNTEER","DEMO-VOLUNTEER", "VOLUNTEER", "NULL"),
credentials: credentials,
})
cy.task('queryDatabase', {
query: "INSERT INTO " + AUTH_USERS_COLUMNS + generateAuthUserTuple(3, "DEMO", "demo-volunteer", 3),
credentials: credentials,
})
});

function generateAuthUserTuple(id, authType, username, userId) {
return "VALUES ('"
+ authType + "', '"
+ id + "', 't', '[email protected]','"
+ username + "', '"
+ userId + "')"
}

function generateUserTuple(id, userType, name, role, institutionId) {
return "VALUES ('"
+ userType + "', '"
+ id + "', '2022-02-06 17:58:21.419878', '"
+ name + "', '"
+ role + "', 'ACTIVE', "
+ institutionId + ")";
}

function generateInstitutionTuple(id) {
return "VALUES ('"
+ id + "', 't', 'abca428c09862e89', '2022-08-06 17:58:21.402146','[email protected]', 'DEMO INSTITUTION', '000000000', '2024-02-06 17:58:21.402134')";
}

0 comments on commit ab6fafa

Please sign in to comment.