diff --git a/cypress/fixtures/img.jpeg b/cypress/fixtures/img.jpeg new file mode 100644 index 0000000..03bbe52 Binary files /dev/null and b/cypress/fixtures/img.jpeg differ diff --git a/cypress/integration/visitorCanRequestQuotes.feature.js b/cypress/integration/visitorCanRequestQuotes.feature.js index 6d7b07f..be295f7 100644 --- a/cypress/integration/visitorCanRequestQuotes.feature.js +++ b/cypress/integration/visitorCanRequestQuotes.feature.js @@ -51,6 +51,7 @@ describe("Visitor can request quotes", () => { cy.get('[name="energy_consumption"]').type("10000"); cy.get("[data-cy=button]").contains("Submit").click(); }); + cy.file_upload("img.jpeg", "#image-upload", "image/jpeg"); cy.get("[data-cy=message]").should( "contain", "Form successfully submitted." diff --git a/cypress/support/commands.js b/cypress/support/commands.js index ca4d256..53b6fee 100644 --- a/cypress/support/commands.js +++ b/cypress/support/commands.js @@ -1,25 +1,21 @@ -// *********************************************** -// This example commands.js shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add("login", (email, password) => { ... }) -// -// -// -- This is a child command -- -// Cypress.Commands.add("drag", { prevSubject: 'element'}, (subject, options) => { ... }) -// -// -// -- This is a dual command -- -// Cypress.Commands.add("dismiss", { prevSubject: 'optional'}, (subject, options) => { ... }) -// -// -// -- This will overwrite an existing command -- -// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... }) +import 'cypress-file-upload' + +Cypress.Commands.add("file_upload", (file, element, type) => { + const selector = element; + const fixturePath = file; + cy.get(selector).then(subject => + cy.window().then(win => + cy + .fixture(fixturePath, "base64") + .then(Cypress.Blob.base64StringToBlob) + .then(blob => { + const el = subject[0] + const testFile = new win.File([blob], name, {type}) + const dataTransfer = new win.DataTransfer() + dataTransfer.items.add(testFile); + el.files = dataTransfer.files; + cy.wrap(subject).trigger("change", { force: true }) + }) + ) + ) +}) \ No newline at end of file diff --git a/package.json b/package.json index 9cada11..f5d8ef5 100644 --- a/package.json +++ b/package.json @@ -7,12 +7,14 @@ "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "axios": "^0.20.0", + "cypress-file-upload": "^4.1.1", "final-form": "^4.20.1", "j-tockauth": "^1.2.7", "react": "^16.13.1", "react-async-script-loader": "^0.3.0", "react-dom": "^16.13.1", "react-final-form": "^6.5.2", + "react-images-upload": "^1.2.8", "react-places-autocomplete": "^7.3.0", "react-redux": "^7.2.1", "react-responsive": "^8.1.0", diff --git a/src/components/QuotesPage.jsx b/src/components/QuotesPage.jsx index f55fb98..d654e8d 100644 --- a/src/components/QuotesPage.jsx +++ b/src/components/QuotesPage.jsx @@ -5,9 +5,19 @@ import TelephoneQuestion from "./mandatoryQuestions/TelephoneQuestion"; import AddressQuestion from "./mandatoryQuestions/AddressQuestion"; import axios from "axios"; import { Form } from "react-final-form"; +import ImageUploader from "react-images-upload"; const QuotesPage = () => { const [message, setMessage] = useState(""); + const [photos, setPhotos] = useState([]); + + const toBase64 = (file) => + new Promise((resolve, reject) => { + const reader = new FileReader(); + reader.readAsDataURL(file); + reader.onload = () => resolve(reader.result); + reader.onerror = (error) => reject(error); + }); const onSubmit = async (event) => { event.preventDefault(); @@ -49,6 +59,7 @@ const QuotesPage = () => { roof_length: roof_length.value, fuse_size: fuse_size.value, energy_consumption: energy_consumption.value, + images: photos, }; response = await axios.post("http://localhost:3000/api/v1/quotes", { @@ -63,6 +74,15 @@ const QuotesPage = () => { } }; + const onDrop = async (photo) => { + const encodedImages = []; + for (let image of photo) { + const encodedImage = await toBase64(image); + encodedImages.push(encodedImage); + } + setPhotos(encodedImages); + }; + return (