Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bulk edit file creation #920

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProjectDetailsRequest>;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -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<BulkEditRow>) {
const result: BulkProjectTable<BulkEditRow> = {
headers: [
"Project ID",
"Project status",
"Actual opening date",
"Local authority"
],
rows: rows,
};

return result;
}

function createCsv(rows: Array<BulkEditRow>): 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<BulkEditRow>): 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<BulkEditRow>): 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<BulkEditRow>): 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;
}
});

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down