-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(pdf-zoom): added tests for pdf zoom and rotate
- Loading branch information
1 parent
dd0e492
commit e1170ed
Showing
5 changed files
with
128 additions
and
8 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
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
116 changes: 116 additions & 0 deletions
116
app/assets/javascripts/Components/__tests__/pdf_viewer.test.jsx
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,116 @@ | ||
import React from "react"; | ||
import {render, screen, fireEvent} from "@testing-library/react"; | ||
import {PDFViewer} from "../Result/pdf_viewer"; | ||
|
||
describe("PDFViewer", () => { | ||
let mockPdfViewer; | ||
let mockAnnotationManager; | ||
|
||
beforeEach(() => { | ||
mockPdfViewer = { | ||
setDocument: jest.fn(), | ||
pagesRotation: 0, | ||
currentScaleValue: "page-width", | ||
}; | ||
|
||
mockAnnotationManager = { | ||
rotateClockwise90: jest.fn(), | ||
}; | ||
|
||
global.pdfjsViewer = { | ||
EventBus: class { | ||
on = jest.fn(); | ||
}, | ||
PDFViewer: jest.fn(() => mockPdfViewer), | ||
}; | ||
|
||
global.annotation_manager = mockAnnotationManager; | ||
|
||
render(<PDFViewer resultView={true} annotations={[]} />); | ||
}); | ||
|
||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
delete global.pdfjsViewer; | ||
delete global.annotation_manager; | ||
}); | ||
|
||
describe("rotation", () => { | ||
let rotateButton; | ||
|
||
beforeEach(() => { | ||
rotateButton = screen.getByText(I18n.t("results.rotate_image")); | ||
}); | ||
|
||
it("initially has a rotation of 0", async () => { | ||
expect(mockPdfViewer.pagesRotation).toBe(0); | ||
}); | ||
|
||
it("rotates to 90 degrees when rotate button is clicked once", () => { | ||
fireEvent.click(rotateButton); | ||
|
||
expect(mockAnnotationManager.rotateClockwise90).toHaveBeenCalledTimes(1); | ||
expect(mockPdfViewer.pagesRotation).toBe(90); | ||
}); | ||
|
||
it("rotates back to 0 degrees when rotate button is clicked four times", () => { | ||
for (let i = 0; i < 4; i++) { | ||
fireEvent.click(rotateButton); | ||
} | ||
|
||
expect(mockAnnotationManager.rotateClockwise90).toHaveBeenCalledTimes(4); | ||
expect(mockPdfViewer.pagesRotation).toBe(0); | ||
}); | ||
}); | ||
|
||
describe("zoom", () => { | ||
it("has default zoom 'page-width' on initial render", () => { | ||
expect(mockPdfViewer.currentScaleValue).toBe("page-width"); | ||
}); | ||
|
||
it("updates zoom to 100% (1.0) when the option is selected from dropdown", () => { | ||
const dropdown = screen.getByTestId("dropdown"); | ||
fireEvent.click(dropdown); | ||
|
||
const option100 = screen.getByText("100 %"); | ||
fireEvent.click(option100); | ||
|
||
expect(mockPdfViewer.currentScaleValue).toBe("1.0"); | ||
}); | ||
|
||
it("updates zoom to 90% (0.9) when the option is selected from dropdown", () => { | ||
const dropdown = screen.getByTestId("dropdown"); | ||
fireEvent.click(dropdown); | ||
|
||
const option110 = screen.getByText("90 %"); | ||
fireEvent.click(option110); | ||
|
||
expect(mockPdfViewer.currentScaleValue).toBe("0.9"); | ||
}); | ||
|
||
it("updates zoom to 120% (1.2) when the option is selected from dropdown", () => { | ||
const dropdown = screen.getByTestId("dropdown"); | ||
fireEvent.click(dropdown); | ||
|
||
const option120 = screen.getByText("120 %"); | ||
fireEvent.click(option120); | ||
|
||
expect(mockPdfViewer.currentScaleValue).toBe("1.2"); | ||
}); | ||
|
||
it("resets zoom to 'page-width' when the option is selected after selecting another zoom", () => { | ||
// set some arbitrary zoom first | ||
const dropdown = screen.getByTestId("dropdown"); | ||
fireEvent.click(dropdown); | ||
const option120 = screen.getByText("120 %"); | ||
fireEvent.click(option120); | ||
|
||
// now put it back to page width | ||
fireEvent.click(dropdown); | ||
const fitToPageWidthOption = screen.getByText(I18n.t("results.fit_to_page_width")); | ||
fireEvent.click(fitToPageWidthOption); | ||
|
||
expect(mockPdfViewer.currentScaleValue).toBe("page-width"); | ||
}); | ||
}); | ||
}); |
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