Skip to content

Commit

Permalink
Add test for JPEG conversion with WebDriver BiDi and Firefox
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Oct 7, 2024
1 parent 37adac6 commit d4cc2fc
Show file tree
Hide file tree
Showing 2 changed files with 148 additions and 2 deletions.
1 change: 1 addition & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ commands:
condition: << parameters.browser >>
steps:
- browser-tools/install-chrome
- browser-tools/install-firefox

lint:
steps:
Expand Down
149 changes: 147 additions & 2 deletions test/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('Converter', () => {
let themeSet: ThemeSet

beforeEach(async () => {
browserManager = new BrowserManager({ timeout })
browserManager = new BrowserManager({
finders: ['chrome', 'edge'],
protocol: 'cdp',
timeout,
})
themeSet = await ThemeSet.initialize([])
})

Expand Down Expand Up @@ -910,6 +914,32 @@ describe('Converter', () => {
).rejects.toThrow(TimeoutError)
})
})

describe('with Firefox browser', () => {
it('outputs warning about incompatibility', async () => {
const warn = jest.spyOn(console, 'warn').mockImplementation()

await using browserManager = new BrowserManager({
finders: ['firefox'],
})

await pdfInstance({
browserManager,
output: 'test.pdf',
}).convertFile(new File(onePath))

expect(warn).toHaveBeenCalledWith(
expect.stringContaining(
'The output may include some incompatible renderings'
)
)
expect(fs.promises.writeFile).toHaveBeenCalled()

const [lastCall] = writeFileSpy.mock.calls.slice(-1)
expect(lastCall[0]).toBe('test.pdf')
expect(lastCall[1]).toBeInstanceOf(Buffer)
})
})
})

describe('when convert type is PPTX', () => {
Expand Down Expand Up @@ -1088,6 +1118,34 @@ describe('Converter', () => {
},
timeout
)

describe('with Firefox browser', () => {
it(
'applies the scale factor to PNG',
async () => {
await using browserManager = new BrowserManager({
finders: ['firefox'],
})

await instance({
browserManager,
output: 'a.png',
type: ConvertType.png,
imageScale: 0.5,
}).convertFile(new File(onePath))

const [lastCall] = writeFileSpy.mock.calls.slice(-1)
expect(lastCall[1]).toBeInstanceOf(Buffer)

const png = lastCall[1] as Buffer
const { width, height } = imageSize(png)

expect(width).toBe(640)
expect(height).toBe(360)
},
timeout
)
})
})
})

Expand Down Expand Up @@ -1140,7 +1198,7 @@ describe('Converter', () => {

describe('with imageScale option', () => {
it(
'applies the scale factor to PNG',
'applies the scale factor to JPEG',
async () => {
await instance({
output: 'b.jpg',
Expand All @@ -1159,6 +1217,93 @@ describe('Converter', () => {
timeout
)
})

describe('with WebDriver BiDi protocol', () => {
it(
'applies the scale factor to JPEG',
async () => {
await using browserManager = new BrowserManager({
finders: ['chrome', 'edge'],
protocol: 'webDriverBiDi',
})

await instance({
browserManager,
output: 'b.jpg',
type: ConvertType.jpeg,
imageScale: 0.5,
}).convertFile(new File(onePath))

const [lastCall] = writeFileSpy.mock.calls.slice(-1)
expect(lastCall[1]).toBeInstanceOf(Buffer)

const jpeg = lastCall[1] as Buffer
const { width, height } = imageSize(jpeg)

expect(width).toBe(640)
expect(height).toBe(360)

// Check JPEG quality is working
writeFileSpy.mockClear()

await instance({
browserManager,
output: 'b.jpg',
type: ConvertType.jpeg,
imageScale: 0.5,
jpegQuality: 1,
}).convertFile(new File(onePath))

const [secondCall] = writeFileSpy.mock.calls.slice(-1)
expect(secondCall[1]).toBeInstanceOf(Buffer)
expect((secondCall[1] as Buffer).length).toBeLessThan(jpeg.length)
},
timeout
)
})

describe('with Firefox browser', () => {
it(
'applies the scale factor to JPEG',
async () => {
await using browserManager = new BrowserManager({
finders: ['firefox'],
})

await instance({
browserManager,
output: 'b.jpg',
type: ConvertType.jpeg,
imageScale: 0.5,
}).convertFile(new File(onePath))

const [lastCall] = writeFileSpy.mock.calls.slice(-1)
expect(lastCall[1]).toBeInstanceOf(Buffer)

const jpeg = lastCall[1] as Buffer
const { width, height } = imageSize(jpeg)

expect(width).toBe(640)
expect(height).toBe(360)

// Check JPEG quality is working
writeFileSpy.mockClear()

await instance({
browserManager,
output: 'b.jpg',
type: ConvertType.jpeg,
imageScale: 0.5,
jpegQuality: 1,
}).convertFile(new File(onePath))

const [secondCall] = writeFileSpy.mock.calls.slice(-1)
expect(secondCall[1]).toBeInstanceOf(Buffer)
expect((secondCall[1] as Buffer).length).toBeLessThan(jpeg.length)
},
timeout
)
})
})

describe('when pages option is true', () => {
Expand Down

0 comments on commit d4cc2fc

Please sign in to comment.