Skip to content

Commit

Permalink
Add missing tests for preview
Browse files Browse the repository at this point in the history
  • Loading branch information
yhatt committed Sep 27, 2024
1 parent 81bf08e commit 36c95af
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
23 changes: 19 additions & 4 deletions src/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,11 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve

if (url.searchParams.get('__marp_cli_id') === id) {
debugPreview('Found a target window with id: %s', id)

pptr.off('targetcreated', idMatcher)
;(async () => res((await target.page())!))()

void (async () => {
res((await target.page()) ?? (await target.asPage()))
})()
}
}

Expand All @@ -187,8 +189,21 @@ export class Preview extends (EventEmitter as new () => TypedEmitter<Preview.Eve
`window.open('about:blank?__marp_cli_id=${id}', '', 'width=${this.options.width},height=${this.options.height}')`
)
})()
}).then((page) => {
debugPreview('Created new window!: %s', page.url())
}).then(async (page) => {
const sizeCorrection = await page.evaluate(
([w, h]) => {
const nw = w - window.innerWidth + w
const nh = h - window.innerHeight + h

window.resizeTo(nw, nh)
return [nw, nh]

Check warning on line 199 in src/preview.ts

View check run for this annotation

Codecov / codecov/patch

src/preview.ts#L195-L199

Added lines #L195 - L199 were not covered by tests
},
[this.options.width, this.options.height]
)

debugPreview('Apply window size correction: %o', sizeCorrection)
debugPreview('Created new window: %s', page.url())

return page
})
)
Expand Down
14 changes: 12 additions & 2 deletions test/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ describe('Preview', () => {
width: window.innerWidth,
}))

expect(height).toBeLessThanOrEqual(600)
expect(width).toBeLessThanOrEqual(400)
expect(height).toBe(600)
expect(width).toBe(400)
})
})

Expand Down Expand Up @@ -134,6 +134,16 @@ describe('Preview', () => {
))
})
})

describe('when opening data URI', () => {
it('opens data URI converted Blob URL to avoid URL length limit', async () => {
const instance = preview()
const win = await instance.open('data:text/html,<html></html>')

expect(await instance.puppeteer?.pages()).toHaveLength(1)
expect(win.page.url()).toStrictEqual(expect.stringMatching(/^blob:/))
})
})
})
})

Expand Down

0 comments on commit 36c95af

Please sign in to comment.