Skip to content

Commit 149855b

Browse files
committed
test(Avatar.svelte): add test suite
1 parent f384bc2 commit 149855b

File tree

8 files changed

+461
-17
lines changed

8 files changed

+461
-17
lines changed

.changeset/clever-lizards-draw.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"melt": patch
3+
---
4+
5+
1. change avatar callbacks trigger order to fire before dom updates
6+
2. add test suite for avatar

docs/src/api.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1717,22 +1717,27 @@
17171717
"properties": [
17181718
{
17191719
"name": "src",
1720-
"type": "string",
1720+
"type": "string | undefined",
17211721
"description": ""
17221722
},
17231723
{
17241724
"name": "delayMs",
17251725
"type": "number",
17261726
"description": ""
17271727
},
1728+
{
1729+
"name": "id",
1730+
"type": "string",
1731+
"description": ""
1732+
},
17281733
{
17291734
"name": "loadingStatus",
17301735
"type": "ImageLoadingStatus",
17311736
"description": ""
17321737
},
17331738
{
17341739
"name": "image",
1735-
"type": "{\n readonly \"data-melt-avatar-image\": \"\"\n readonly src: string\n readonly style: `display: ${string}`\n readonly onload: () => (() => void) | undefined\n readonly onerror: () => void\n}",
1740+
"type": "{\n readonly \"data-melt-avatar-image\": \"\"\n readonly src: string | undefined\n readonly style: `display: ${string}`\n readonly onload: () => (() => void) | undefined\n readonly onerror: () => void\n}",
17361741
"description": ""
17371742
},
17381743
{

packages/melt/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"test:headless": "vitest --browser.headless",
4343
"sync": "svelte-kit sync",
4444
"build": "npm run package",
45+
"prepare": "svelte-kit sync || echo ''",
4546
"preview": "vite preview",
4647
"package": "svelte-kit sync && svelte-package && publint",
4748
"prepublishOnly": "npm run package",

packages/melt/src/lib/builders/Avatar.svelte.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { extract } from "$lib/utils/extract";
2-
import { createDataIds } from "$lib/utils/identifiers";
2+
import { createBuilderMetadata, } from "$lib/utils/identifiers";
33
import { styleAttr } from "$lib/utils/attribute";
44
import { inBrowser } from "$lib/utils/browser";
55
import type { MaybeGetter } from "$lib/types";
66
import { watch } from "runed";
77

8-
const identifiers = createDataIds("avatar", ["image", "fallback"]);
8+
const { dataAttrs, createIds } = createBuilderMetadata("avatar", ["image", "fallback"]);
99

1010
export type ImageLoadingStatus = "loading" | "loaded" | "error";
1111

@@ -29,27 +29,43 @@ export type AvatarProps = {
2929
};
3030

3131
export class Avatar {
32+
#ids = createIds();
3233
/* Props */
3334
#props!: AvatarProps;
34-
readonly src = $derived(extract(this.#props.src, ""));
35+
readonly src = $derived(extract(this.#props.src, undefined));
3536
readonly delayMs = $derived(extract(this.#props.delayMs, 0));
3637

3738
/* State */
3839
#loadingStatus: ImageLoadingStatus = $state("loading");
3940

4041
constructor(props: AvatarProps = {}) {
41-
$effect(() => {
42-
this.#props.onLoadingStatusChange?.(this.#loadingStatus);
43-
});
42+
// Should be defined at the top before the effects
43+
// when using $effect.pre or $watch.pre
44+
this.#props = props;
4445

45-
watch(
46+
// Run effects before dom updates
47+
// for provide handlers with some execution time
48+
watch.pre(
49+
() => this.#loadingStatus,
50+
() => {
51+
this.#props.onLoadingStatusChange?.(this.#loadingStatus);
52+
}
53+
);
54+
55+
watch.pre(
4656
() => this.src,
4757
() => {
48-
this.#loadingStatus = "loading";
58+
this.#loadingStatus = "loading";
4959
},
60+
{
61+
lazy: true
62+
}
5063
);
5164

52-
this.#props = props;
65+
}
66+
67+
get id() {
68+
return this.#ids.image;
5369
}
5470

5571
get loadingStatus() {
@@ -58,7 +74,7 @@ export class Avatar {
5874

5975
get image() {
6076
return {
61-
[identifiers.image]: "",
77+
[dataAttrs.image]: "",
6278
src: this.src,
6379
style: styleAttr({ display: this.#loadingStatus === "loaded" ? "block" : "none" }),
6480
onload: () => {
@@ -76,7 +92,7 @@ export class Avatar {
7692

7793
get fallback() {
7894
return {
79-
[identifiers.fallback]: "",
95+
[dataAttrs.fallback]: "",
8096
style: this.#loadingStatus === "loaded" ? styleAttr({ display: "none" }) : undefined,
8197
hidden: this.#loadingStatus === "loaded" ? true : undefined,
8298
} as const;

0 commit comments

Comments
 (0)