Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit 87b0afe

Browse files
committed
test(image+avatar+avatargroup): mock onload implementation
1 parent 511f5ae commit 87b0afe

File tree

8 files changed

+22
-10
lines changed

8 files changed

+22
-10
lines changed

packages/chakra-ui-core/src/CAvatar/CAvatar.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ const CAvatar = {
249249
h: '100%',
250250
rounded: 'full',
251251
objectFit: 'cover',
252-
alt: this.name
252+
alt: this.name,
253+
src: this.src
253254
},
254255
domProps: {
255256
src: this.src

packages/chakra-ui-core/src/CAvatar/tests/CAvatar.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ beforeAll(() => {
1212
// eslint-disable-next-line accessor-pairs
1313
Object.defineProperty(global.Image.prototype, 'src', {
1414
set (src) {
15+
// Mock the Image.prototype.onload/onerror implementations
16+
this.onload = jest.fn().mockImplementation(fn => fn)
17+
this.onerror = jest.fn().mockImplementation(err => err)
1518
if (src === LOAD_FAILURE_SRC) {
1619
setTimeout(() => this.onerror(new Error('mocked error')))
1720
} else if (src === LOAD_SUCCESS_SRC) {

packages/chakra-ui-core/src/CAvatar/tests/__snapshots__/CAvatar.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ exports[`Avatar with AvatarBadge renders correctly 1`] = `
88
>
99
<img
1010
alt="Mesut Koca"
11-
class="css-58rle2"
11+
class="css-6qjm3j"
1212
data-chakra-component="CBox"
1313
src="LOAD_SUCCESS_SRC"
1414
/>
1515
<div
16-
class="css-mwkd9l"
16+
class="css-bruuz9"
1717
data-chakra-component="CAvatarBadge"
1818
/>
1919
</div>
@@ -27,13 +27,13 @@ exports[`renders an image 1`] = `<DocumentFragment />`;
2727
exports[`should render correctly 1`] = `
2828
<DocumentFragment>
2929
<div
30-
class="css-2sxxyf"
30+
class="css-1sovlky"
3131
data-chakra-component="CAvatar"
3232
data-testid="avatar"
3333
>
3434
<img
3535
alt="Mesut Koca"
36-
class="css-58rle2"
36+
class="css-6qjm3j"
3737
data-chakra-component="CBox"
3838
src="LOAD_SUCCESS_SRC"
3939
/>

packages/chakra-ui-core/src/CAvatarGroup/tests/CAvatarGroup.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ beforeAll(() => {
1313
// eslint-disable-next-line accessor-pairs
1414
Object.defineProperty(global.Image.prototype, 'src', {
1515
set (src) {
16+
// Mock the Image.prototype.onload/onerror implementations
17+
this.onload = jest.fn().mockImplementation(fn => fn)
18+
this.onerror = jest.fn().mockImplementation(err => err)
1619
if (src === LOAD_FAILURE_SRC) {
1720
setTimeout(() => this.onerror(new Error('mocked error')))
1821
} else if (src === LOAD_SUCCESS_SRC) {

packages/chakra-ui-core/src/CAvatarGroup/tests/__snapshots__/CAvatarGroup.test.js.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ exports[`should render correctly 1`] = `
5454
>
5555
<img
5656
alt="Mesut Koca"
57-
class="css-58rle2"
57+
class="css-6qjm3j"
5858
data-chakra-component="CBox"
5959
src="LOAD_SUCCESS_SRC"
6060
/>
@@ -65,7 +65,7 @@ exports[`should render correctly 1`] = `
6565
>
6666
<img
6767
alt="Evan You"
68-
class="css-58rle2"
68+
class="css-6qjm3j"
6969
data-chakra-component="CBox"
7070
src="LOAD_SUCCESS_SRC"
7171
/>

packages/chakra-ui-core/src/CImage/CImage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ const CImage = {
7676
return h(CNoSsr, [
7777
h('img', {
7878
class: this.className,
79-
domProps: {
80-
...imageProps
81-
},
79+
// domProps: {
80+
// ...imageProps
81+
// },
8282
attrs: {
8383
...imageProps,
8484
...this.computedAttrs,

packages/chakra-ui-core/src/CImage/tests/CImage.test.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ beforeAll(() => {
1212
// eslint-disable-next-line accessor-pairs
1313
Object.defineProperty(global.Image.prototype, 'src', {
1414
set (src) {
15+
// Mock the Image.prototype.onload/onerror implementations
16+
this.onload = jest.fn().mockImplementation(fn => fn)
17+
this.onerror = jest.fn().mockImplementation(err => err)
1518
if (src === LOAD_FAILURE_SRC) {
1619
setTimeout(() => this.onerror(new Error('mocked error')))
1720
} else if (src === LOAD_SUCCESS_SRC) {

packages/chakra-ui-core/src/CImage/tests/__snapshots__/CImage.test.js.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`fallback src works 1`] = `<DocumentFragment />`;
4+
35
exports[`should render correctly 1`] = `
46
<DocumentFragment>
57
<img

0 commit comments

Comments
 (0)