Skip to content

Commit

Permalink
refactor(useStepsNavigation): extract code from handleStepNext functi…
Browse files Browse the repository at this point in the history
…on to handleNext.js
  • Loading branch information
tuliooassis committed Jun 30, 2020
1 parent 4666dbd commit 5829d47
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 36 deletions.
30 changes: 30 additions & 0 deletions src/hooks/useReportingWizardSteps/handleNext.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

import { createParkingReport } from "../../services/parkings"

export const handleCarFrontPhoto = (parkingReportState) => {
const handleSuccess = ({ data }) => {
const uuid = data.data.uuid
const carPlate = data.data.parkings.car_plate

localStorage.setItem(`PARKING_REPORT`, uuid)

return {
uuid,
carPlate,
isCarFrontPhotoValid: true,
}
}

const handleError = (error) => {
localStorage.removeItem(`PARKING_REPORT`)

return {
isCarFrontPhotoValid: false,
}
}

const { carFrontPhotoPreviewUrl } = parkingReportState
return createParkingReport(carFrontPhotoPreviewUrl)
.then(handleSuccess)
.catch(handleError)
}
31 changes: 31 additions & 0 deletions src/hooks/useReportingWizardSteps/handleNext.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { handleCarFrontPhoto } from "./handleNext"

jest.mock("../../services/parkings");

describe("useStepsNavigation", () => {
const mockSetItem = jest.fn()
const mockRemoveItem = jest.fn()

beforeEach(() => {
jest.spyOn(window.localStorage.__proto__, 'setItem');
jest.spyOn(window.localStorage.__proto__, 'removeItem');

window.localStorage.__proto__.setItem = mockSetItem;
window.localStorage.__proto__.removeItem = mockRemoveItem;
})

it("should store uuid in localStorage", async () => {
const parkingReportState = {
carFrontPhotoPreviewUrl: 'url'
}

const response = await handleCarFrontPhoto(parkingReportState)

const { uuid } = response

expect(window.localStorage.__proto__.setItem).toHaveBeenCalledWith('PARKING_REPORT', uuid)
})


it.todo("should remove uuid from localStorage in case of invalid photo")
})
4 changes: 2 additions & 2 deletions src/hooks/useReportingWizardSteps/useReportingWizardSteps.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isCarFrontPhotoValid } from "../../validators/isCarFrontPhotoValid"
import { isCarPlateValid } from "../../validators/isCarPlateValid"
import { isCurrentPositionValid } from "../../validators/isCurrentPositionValid"

import { createParkingReport } from "../../services/parkings"
import { handleCarFrontPhoto } from "./handleNext"

export const useReportingWizardSteps = ({ toggles }) => {
let result = []
Expand All @@ -23,7 +23,7 @@ export const useReportingWizardSteps = ({ toggles }) => {
label: "Enviar foto da frente",
component: CarFrontPhotoStep,
validator: isCarFrontPhotoValid,
handleNext: createParkingReport,
handleNext: handleCarFrontPhoto,
},
{
label: "Confirmar placa",
Expand Down
40 changes: 7 additions & 33 deletions src/hooks/useStepsNavigation/useStepsNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,40 +23,14 @@ export const useStepsNavigation = (steps, maxSteps) => {
}

const handleStepNext = async () => {
const handleSuccess = ({ data }) => {
setLoading(false)

const uuid = data.data.uuid
const carPlate =
data.data.parkings.car_front_photo_recognition_data.results[0].plate

localStorage.setItem(`PARKING_REPORT`, uuid)

setParkingReportState((oldParkingReportState) => ({
...oldParkingReportState,
carPlate,
isCarFrontPhotoValid: true,
}))

handleDefaultNext()
}

const handleError = (error) => {
setLoading(false)

localStorage.removeItem(`PARKING_REPORT`)

setParkingReportState((oldParkingReportState) => ({
...oldParkingReportState,
isCarFrontPhotoValid: false,
}))
}

setLoading(true)
steps[activeStepIndex]
.handleNext(parkingReportState)
.then(handleSuccess)
.catch(handleError)
const newParkingReportState = await steps[activeStepIndex].handleNext(parkingReportState)

setParkingReportState((oldParkingReportState) => ({
...oldParkingReportState,
...newParkingReportState
}))
setLoading(false)
}

const handleDefaultNext = () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useStepsNavigation/useStepsNavigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe("useStepsNavigation", () => {
})

it("should go to the next step", () => {
mockHandleNext.mockImplementation(async () => {})
mockHandleNext.mockImplementation(async () => { })

const { handleNext } = useStepsNavigation(steps, maxSteps)

Expand Down
18 changes: 18 additions & 0 deletions src/services/__mocks__/parkings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const resolvedValue = {
status: 'success',
data: {
data: {
uuid: 'uuid',
parkings: {
'car_plate': 'car_plate'
}
}
}
}

const rejectedValue = {
status: 'error',
error: {}
}

export const createParkingReport = (parkingReportState, version = 1) => new Promise(resolve => resolve(resolvedValue), reject => reject(rejectedValue));

0 comments on commit 5829d47

Please sign in to comment.