diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/api/domain.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/api/domain.ts index 3338e997f..505bf306d 100644 --- a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/api/domain.ts +++ b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/api/domain.ts @@ -22,6 +22,13 @@ export type BulkProjectRow = { status?: string; }; +export type BulkEditRow = { + projectId?: string; + localAuthority?: string; + actualOpeningDate?: string; + status?: string; +}; + export type CreateProjectRequest = { projects: Array; }; diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/bulk-edit-project.cy.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/bulk-edit-project.cy.ts new file mode 100644 index 000000000..4cb8864df --- /dev/null +++ b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/bulk-edit-project.cy.ts @@ -0,0 +1,156 @@ +import { BulkProjectTable, BulkEditRow, ProjectDetailsRequest } from "cypress/api/domain"; +import bulkEditProjectPage from "cypress/pages/bulkEditProjectPage"; +import { ProjectRecordCreator } from "cypress/constants/cypressConstants"; +import { v4 } from "uuid"; +import { stringify } from "csv-stringify/sync"; +import { utils, write } from "xlsx/xlsx"; +import { RequestBuilder } from "cypress/api/requestBuilder"; +import projectApi from "cypress/api/projectApi"; +import { Logger } from "cypress/common/logger"; +import homePage from "cypress/pages/homePage"; + +describe("Bulk editing project", () => { + + let project: ProjectDetailsRequest; + let now: Date; + + beforeEach(() => { + cy.login({ role: ProjectRecordCreator }); + cy.visit('/'); + + now = new Date(); + + project = RequestBuilder.createProjectDetails(); + + projectApi + .post({ + projects: [project], + }) + homePage + .bulkEdit() + }); + + it("Should edit the valid CSV file and show success message", () => { + const emptyRow: BulkEditRow = { + projectId: v4(), + }; + + const csv = createCsv([emptyRow]); + + bulkEditProjectPage + .upload(csv, "file.csv").continue() + .recordsVisible() + .editProjects() + .successMessage() + }); + + it("Should validate when no csv or xlsx file is selected", () => { + bulkEditProjectPage + .continue() + .errorMessage('Select a file'); + }); + + it("Should validate if csv file has no data", () => { + const emptyRow: BulkEditRow = { + projectId: v4(), + }; + const csv = createEmptyCsv([]); + + bulkEditProjectPage + .upload(csv, "file.csv").continue() + .errorMessage('The selected file must have project data in it'); + }); + + it("Should validate if the csv file has incorrect data", () => { + const emptyRow: BulkEditRow = { + projectId: v4(), + }; + const csv = createCsvWithIncorrectData([]); + + bulkEditProjectPage + .upload(csv, "file.csv").continue() + .errorMessage('The enter data tab has 3 validation errors') + }); + + it.skip("Should validate an uploaded Excel file", () => { + Logger.log(" Navigate to update multiple fields card"); + cy.contains('Update multiple fields').should('be.visible').click() + const completeRow: BulkEditRow = { + projectId: "1mg9101rvi5tluigc0x3lv7s7", + localAuthority: "Luton", + actualOpeningDate: "27/09/2040", + status: "Pre-openingkjgkjgh" + }; + + const emptyRow: BulkEditRow = { + projectId: v4(), + }; + + const buffer = createExcel([completeRow, emptyRow]); + bulkEditProjectPage + .upload(buffer, "file.xlsx").continue() + .recordsVisible() + .editProjects() + + }); + + function createTable(rows: Array) { + const result: BulkProjectTable = { + headers: [ + "Project ID", + "Project status", + "Actual opening date", + "Local authority" + ], + rows: rows, + }; + + return result; + } + + function createCsv(rows: Array): Buffer { + const table = createTable(rows); + + const result = "Project ID,Project status,Actual opening date,Local authority\n" + + `${project.projectId},Pre-opening,27/09/2040` + + return Buffer.from(result); + } + + function createEmptyCsv(rows: Array): Buffer { + const table = createTable(rows); + + const result = "Project ID,Project status,Actual opening date,Local authority\n" + + return Buffer.from(result); + } + + function createCsvWithIncorrectData(rows: Array): Buffer { + const table = createTable(rows); + + const result = "Project ID,Project status,Actual opening date,Local authority\n" + + `${project.projectId},Preopening,27/13/2040,Test` + + return Buffer.from(result); + } + + function createExcel(rows: Array): Buffer { + const table = createTable(rows); + + const worksheet = utils.json_to_sheet(table.rows, { + header: table.headers, + }); + + const workbook = utils.book_new(); + utils.book_append_sheet(workbook, worksheet); + + const buffer = write(workbook, { + type: "buffer", + bookType: "xlsx", + compression: true, + }); + + Logger.log(JSON.stringify(table.rows)) + return buffer; + } +}); diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/create-bulk-project.cy.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/create-bulk-project.cy.ts deleted file mode 100644 index a59e58a5c..000000000 --- a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/e2e/create-bulk-project.cy.ts +++ /dev/null @@ -1,104 +0,0 @@ -import { BulkProjectTable, BulkProjectRow } from "cypress/api/domain"; -import bulkCreateProjectPage from "cypress/pages/bulkCreateProjectPage"; -import { v4 } from "uuid"; -import { stringify } from "csv-stringify/sync"; - -// Problem with importing the xlsx with the latest cypress -// if you want typings, just remove /xlsx, you will have to add it back in to work with cypress -// This is a webpack 5 issue, but the library itself is incompatible -// Tried to override the webpack config in cypress, but didn't have much luck -import { utils, write } from "xlsx/xlsx"; - -describe("Creating a bulk project", () => { - beforeEach(() => { - cy.login(); - cy.visit("/project/create/bulk"); - }); - - it("Should validate an uploaded CSV file", () => { - const completeRow: BulkProjectRow = { - projectId: v4(), - projectTitle: v4(), - trustName: v4(), - region: v4(), - localAuthority: v4(), - realisticOpeningDate: v4(), - status: v4(), - }; - - const emptyRow: BulkProjectRow = { - projectId: v4(), - }; - - const csv = createCsv([completeRow, emptyRow]); - - bulkCreateProjectPage.upload(csv, "file.csv").continue(); - }); - - it("Should validate an uploaded Excel file", () => { - const completeRow: BulkProjectRow = { - projectId: v4(), - projectTitle: v4(), - trustName: v4(), - region: v4(), - localAuthority: v4(), - realisticOpeningDate: v4(), - status: v4(), - }; - - const emptyRow: BulkProjectRow = { - projectId: v4(), - }; - - const buffer = createExcel([completeRow, emptyRow]); - - bulkCreateProjectPage.upload(buffer, "file.xlsx").continue(); - }); - - function createTable(rows: Array) { - const result: BulkProjectTable = { - headers: [ - "projectTitle", - "projectId", - "trustName", - "region", - "localAuthority", - "realisticOpeningDate", - "status", - ], - rows: rows, - }; - - return result; - } - - function createCsv(rows: Array): Buffer { - const table = createTable(rows); - - const result = stringify(table.rows, { - columns: table.headers, - header: true, - }); - - return Buffer.from(result); - } - - function createExcel(rows: Array): Buffer { - const table = createTable(rows); - - const worksheet = utils.json_to_sheet(table.rows, { - header: table.headers, - }); - - const workbook = utils.book_new(); - utils.book_append_sheet(workbook, worksheet); - - const buffer = write(workbook, { - type: "buffer", - bookType: "xlsx", - compression: true, - }); - - return buffer; - } -}); diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkCreateProjectPage.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkCreateProjectPage.ts deleted file mode 100644 index 5e118711d..000000000 --- a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkCreateProjectPage.ts +++ /dev/null @@ -1,20 +0,0 @@ -class BulkCreateProjectPage { - public upload(file: Buffer, filename: string): this { - cy.getByTestId("upload").selectFile({ - contents: file, - fileName: filename, - lastModified: Date.now(), - }); - return this; - } - - public continue(): this { - cy.getByTestId("continue").click(); - - return this; - } -} - -const bulkCreateProjectPage = new BulkCreateProjectPage(); - -export default bulkCreateProjectPage; diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkEditProjectPage.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkEditProjectPage.ts new file mode 100644 index 000000000..dffa27185 --- /dev/null +++ b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/bulkEditProjectPage.ts @@ -0,0 +1,53 @@ +class BulkEditProjectPage { + public upload(file: Buffer, filename: string): this { + cy.get('input[name="upload"]').selectFile({ + contents: file, + fileName: filename, + lastModified: Date.now(), + }); + return this; + } + + public errorMessage(error: string): this { + cy.get('.govuk-error-summary').should('contains.text', error) + return this + } + + public continue(): this { + cy.contains('button','Upload').click() + return this; + } + + public chooseFile(): this { + cy.get('input[name="upload"]').click(); + return this; + } + + public editProjects(): this { + cy.contains('button','Edit projects').click() + return this; + } + + public checkYourAnswersHeading(string): this { + cy.get('.govuk-heading-xl').should('contain.text', string) + return this; + } + + public recordsVisible(): this { + cy.get('h1').contains('Check your answers') + cy.get('.govuk-summary-card__content').should('have.length', 1) + return this; + } + + public successMessage(): this { + cy.url().should('contains', 'count') + cy.get('.govuk-panel').should('be.visible') + cy.contains('a','Go back to the projects listing page').should('be.visible') + return this; + } + +} + +const bulkEditProjectPage = new BulkEditProjectPage(); + +export default bulkEditProjectPage; diff --git a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/homePage.ts b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/homePage.ts index 2c4194aeb..33644e981 100644 --- a/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/homePage.ts +++ b/Dfe.ManageFreeSchoolProjects/Dfe.ManageFreeSchoolProjects.CypressTests/cypress/pages/homePage.ts @@ -12,6 +12,12 @@ class HomePage { return this; } + public bulkEdit(): this{ + cy.contains('Update multiple fields').should('be.visible').click(); + + return this; + } + public deleteProject(): this { cy.getByTestId("delete-project-button").click();