-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(useStepsNavigation): extract code from handleStepNext functi…
…on to handleNext.js
- Loading branch information
1 parent
4666dbd
commit 5829d47
Showing
6 changed files
with
89 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |