Skip to content

Commit

Permalink
Merge pull request #589 from ohtutraininghub/#566/feat/e2e-tests-for-557
Browse files Browse the repository at this point in the history
#566/feat/e2e tests for 557
  • Loading branch information
rauhja authored Apr 18, 2024
2 parents 0126e71 + 97db826 commit 2588122
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 12 deletions.
4 changes: 4 additions & 0 deletions cypress/e2e/adminView.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ describe('Admin view', () => {
cy.get('h1').contains('Admin dashboard');
});

it('export course statistics should be accessible', () => {
cy.get('h2').contains('Export course statistics');
});

it('titlelist should be accessible', () => {
cy.get('h2').contains('Titles');
});
Expand Down
40 changes: 40 additions & 0 deletions cypress/e2e/courseStatistics.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Role } from '@prisma/client';

const adminUser = {
name: 'Anna Admin',
email: '[email protected]',
role: Role.ADMIN,
profileCompleted: true,
};

const fromDate = '2100-09-10';
const toDate = '2100-09-20';

describe('Exporting Course Statistics', () => {
beforeEach(() => {
cy.task('clearDatabase');
cy.task('seedDatabase');
cy.login(adminUser.email, adminUser.role);

cy.get('[aria-label^="SpeedDial"]').click();
cy.getCy('dashboard').click();
});

it('should export csv file when correct dateframe is given', () => {
cy.getCy('exportFormFromDate').type(fromDate);
cy.getCy('exportFormToDate').type(toDate);
cy.getCy('exportStatsButton').click();
cy.contains('Download started');
cy.readFile(`cypress/downloads/training-data_${fromDate}_${toDate}.csv`);
});

it('should not export csv file when incorrect dateframe is given', () => {
cy.getCy('exportFormFromDate').type(toDate);
cy.getCy('exportFormToDate').type(fromDate);
cy.getCy('exportStatsButton').click();
cy.contains('Invalid date range');
cy.readFile(
`cypress/downloads/training-data_${toDate}_${fromDate}.csv`
).should('not.exist');
});
});
1 change: 1 addition & 0 deletions src/components/ExportStats/ExportForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default function ExportForm({ lang }: Props) {
</FormControl>
<Button
type="submit"
data-testid="exportStatsButton"
disabled={isSubmitting}
variant="contained"
color="primary"
Expand Down
38 changes: 26 additions & 12 deletions src/lib/prisma/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,29 @@ export const getCoursesForCsv = async (fromDate: Date, toDate: Date) => {
name: true,
},
},
students: {
participations: {
select: {
name: true,
country: {
user: {
select: {
name: true,
country: {
select: {
name: true,
},
},
title: {
select: {
name: true,
},
},
},
},
title: {
select: {
name: true,
},
},
orderBy: {
user: {
name: 'asc',
},
},
orderBy: [{ name: 'asc' }],
},
},
orderBy: [{ startDate: 'asc' }, { name: 'asc' }],
Expand All @@ -124,10 +132,16 @@ export const getCoursesForCsv = async (fromDate: Date, toDate: Date) => {
const formattedCourses = courses.map((course) => ({
name: course.name,
createdBy: { name: course.createdBy ? course.createdBy.name : null },
students: course.students.map((student) => ({
name: student.name,
country: { name: student.country ? student.country.name : null },
title: { name: student.title ? student.title.name : null },
students: course.participations.map((participation) => ({
name: participation.user.name,
country: {
name: participation.user.country
? participation.user.country.name
: null,
},
title: {
name: participation.user.title ? participation.user.title.name : null,
},
})),
startDate: course.startDate,
endDate: course.endDate,
Expand Down

0 comments on commit 2588122

Please sign in to comment.