Skip to content

Commit

Permalink
test: add hydration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsjtu committed Mar 20, 2024
1 parent bea43ea commit 6b7f9c1
Show file tree
Hide file tree
Showing 54 changed files with 198 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default {
props: {
dynamicStyle: 'background-color: red; border-color: red;',
},
clientProps: {
dynamicStyle: 'background-color: red; border-color: red !important;',
},
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots, consoleCalls) {
const p = target.shadowRoot.querySelector('p');

expect(p).not.toBe(snapshots.p);
expect(p.getAttribute('style')).not.toBe(snapshots.style);
expect(p.getAttribute('style')).toBe(
'background-color: red; border-color: red !important;'
);

expect(consoleCalls.error).toHaveSize(2);
expect(consoleCalls.error[0][0].message).toContain(
'[LWC error]: Mismatch hydrating element <p>: attribute "style" has different values, expected "background-color: red; border-color: red !important;" but found "background-color: red; border-color: red;"'
);
expect(consoleCalls.error[1][0].message).toContain('Hydration completed with errors.');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Main extends LightningElement {
@api dynamicStyle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export default {
props: {
dynamicStyle: 'background-color: red; border-color: red;',
},
clientProps: {
dynamicStyle: 'background-color: red; border-color: red; margin: 1px;',
},
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots, consoleCalls) {
const p = target.shadowRoot.querySelector('p');

expect(p).not.toBe(snapshots.p);
expect(p.getAttribute('style')).not.toBe(snapshots.style);
expect(p.getAttribute('style')).toBe(
'background-color: red; border-color: red; margin: 1px;'
);

expect(consoleCalls.error).toHaveSize(2);
expect(consoleCalls.error[0][0].message).toContain(
'Mismatch hydrating element <p>: attribute "style" has different values, expected "background-color: red; border-color: red; margin: 1px;" but found "background-color: red; border-color: red;"'
);
expect(consoleCalls.error[1][0].message).toContain('Hydration completed with errors.');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Main extends LightningElement {
@api dynamicStyle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default {
props: {
dynamicStyle: 'background-color: red; border-color: red; margin: 1px;',
},
clientProps: {
dynamicStyle: 'background-color: red; border-color: red;',
},
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots, consoleCalls) {
const p = target.shadowRoot.querySelector('p');

expect(p).not.toBe(snapshots.p);
expect(p.getAttribute('style')).not.toBe(snapshots.style);
expect(p.getAttribute('style')).toBe('background-color: red; border-color: red;');

expect(consoleCalls.error).toHaveSize(2);
expect(consoleCalls.error[0][0].message).toContain(
'[LWC error]: Mismatch hydrating element <p>: attribute "style" has different values, expected "background-color: red; border-color: red;" but found "background-color: red; border-color: red; margin: 1px;"'
);
expect(consoleCalls.error[1][0].message).toContain('Hydration completed with errors.');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Main extends LightningElement {
@api dynamicStyle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export default {
props: {
dynamicStyle: 'background-color: red; border-color: red; margin: 1px;',
},
clientProps: {
dynamicStyle: 'margin: 1px; border-color: red; background-color: red;',
},
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots, consoleCalls) {
const p = target.shadowRoot.querySelector('p');

expect(p).not.toBe(snapshots.p);
expect(p.getAttribute('style')).toBe(
'margin: 1px; border-color: red; background-color: red;'
);

expect(consoleCalls.error).toHaveSize(2);
expect(consoleCalls.error[0][0].message).toContain(
'Mismatch hydrating element <p>: attribute "style" has different values, expected "margin: 1px; border-color: red; background-color: red;" but found "background-color: red; border-color: red; margin: 1px;"'
);
expect(consoleCalls.error[1][0].message).toContain('Hydration completed with errors.');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Main extends LightningElement {
@api dynamicStyle;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { LightningElement } from 'lwc';

export default class Main extends LightningElement {
// Note: extra spaces matters
dynamicStyle = 'background-color: red; border-color: red !important ; ';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<template>
<p style={dynamicStyle}>txt</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement, api } from 'lwc';

export default class Main extends LightningElement {
@api dynamicStyle = 'background-color: red; border-color: red; margin: 1px;';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export default {
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots) {
const p = target.shadowRoot.querySelector('p');

expect(p).toBe(snapshots.p);
expect(p.getAttribute('style')).toBe(snapshots.style);
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default {
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
style: p.getAttribute('style'),
};
},
test(target, snapshots, consoleCalls) {
const p = target.shadowRoot.querySelector('p');

expect(p).toBe(snapshots.p);
expect(p.getAttribute('style')).toBe(snapshots.style);

expect(consoleCalls.error).toHaveSize(0);
},
};

0 comments on commit 6b7f9c1

Please sign in to comment.