Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow snapshot strategy to be configured #11

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ module.exports = {
await controller.finish();
},

async screenshot(page, handleOrLocator, { component, variant, ...rest }) {
async screenshot(
page,
handleOrLocator,
{ component, variant, snapshotStrategy = 'hoist', ...rest },
) {
if (!controller.isActive()) {
return;
}
Expand All @@ -57,11 +61,20 @@ module.exports = {
const elementHandle = handleOrLocator.elementHandle
? await handleOrLocator.elementHandle()
: handleOrLocator;

const snapshot = await page.evaluate(
(element) =>
window.happoTakeDOMSnapshot({ doc: element.ownerDocument, element }),
elementHandle,
({ element, strategy }) =>
window.happoTakeDOMSnapshot({
doc: element.ownerDocument,
element,
strategy,
}),
{
element: elementHandle,
strategy: snapshotStrategy,
},
);

await controller.registerSnapshot({
...snapshot,
component,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"devDependencies": {
"@playwright/test": "^1.46.0",
"eslint": "^8.2.0",
"happo-e2e": "^2.0.1",
"happo-e2e": "^3.3.1",
"happo.io": "^6.8.0",
"next": "^12.0.4",
"react": "^17.0.2",
Expand Down
8 changes: 8 additions & 0 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ export default function IndexPage() {
<h1>Hello world</h1>
<a href="/goodbye">goodbye</a>
<CanvasImage />

<div style={{ width: 200 }}>
I am 200px wide.
<div style={{ width: '100%' }} id="stretch-to-parent">
I stretch to 100% width of my parent, which is 200px wide.
</div>
</div>

<style>{`
h1 {
color: red;
Expand Down
14 changes: 14 additions & 0 deletions tests/app.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,20 @@ test('basic test', async ({ page }) => {
],
});

const stretchToParent = page.locator('#stretch-to-parent');

await happoPlaywright.screenshot(page, stretchToParent, {
component: 'StretchToParent',
variant: 'snapshotStrategy hoist',
snapshotStrategy: 'hoist',
});

await happoPlaywright.screenshot(page, stretchToParent, {
component: 'StretchToParent',
variant: 'snapshotStrategy hoist',
snapshotStrategy: 'clip',
});

await page.click('text=goodbye');

await expect(page.locator('text=Sad to see you go').first()).toBeVisible();
Expand Down
Loading
Loading