Skip to content

Commit

Permalink
fix: consider valueOf in the reactive methods of SvelteDate (#14227)
Browse files Browse the repository at this point in the history
  • Loading branch information
paoloricciuti authored Nov 8, 2024
1 parent f0c2d4c commit 1060bea
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/cyan-cooks-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': patch
---

fix: consider `valueOf` in the reactive methods of `SvelteDate`
2 changes: 1 addition & 1 deletion packages/svelte/src/reactivity/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class SvelteDate extends Date {
);

for (const method of methods) {
if (method.startsWith('get') || method.startsWith('to')) {
if (method.startsWith('get') || method.startsWith('to') || method === 'valueOf') {
// @ts-ignore
proto[method] = function (...args) {
// don't memoize if there are arguments
Expand Down
24 changes: 24 additions & 0 deletions packages/svelte/src/reactivity/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,30 @@ test('Date.toLocaleString', () => {
cleanup();
});

test('Date.valueOf', () => {
const date = new SvelteDate(initial_date);

const log: any = [];

const cleanup = effect_root(() => {
render_effect(() => {
log.push(date.valueOf());
});
});

flushSync();

assert.deepEqual(log, [initial_date.valueOf()]);

flushSync(() => {
date.setTime(date.getTime() + 10);
});

assert.deepEqual(log, [initial_date.valueOf(), new Date(initial_date.getTime() + 10).valueOf()]);

cleanup();
});

test('Date.instanceOf', () => {
assert.equal(new SvelteDate() instanceof Date, true);
});
Expand Down

0 comments on commit 1060bea

Please sign in to comment.