fix: fix dismiss update on desktop (#1515)

<!-- Please read
https://github.com/SableClient/Sable/blob/dev/CONTRIBUTING.md before
submitting your pull request -->

### Description

<!-- Please include a summary of the change. Please also include
relevant motivation and context. List any dependencies that are required
for this change. -->

Fixes #

#### Type of change

- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- [ ] This change requires a documentation update

### Checklist:

- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings

### AI disclosure:

- [ ] Partially AI assisted (clarify which code was AI assisted and
briefly explain what it does).
- [ ] Fully AI generated (explain what all the generated code does in
moderate detail).
<!-- Write any explanation required here, but do not generate the
explanation using AI!! You must prove you understand what the code in
this PR does. -->
This commit is contained in:
7w1 authored and GitHub committed 2026-07-30 19:36:22 +00:00
commit 706bf43681
2 files changed
+146 -7

No files matched your search

@@ -0,0 +1,145 @@
import type { ReactNode } from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import { useAtomValue, useSetAtom, Provider } from 'jotai';
import { globalBannersAtom } from '$state/globalBanners';
import { triggerUpdateCheckAtom } from '$state/desktopUpdate';
import { DesktopUpdatePill } from '$components/tauri/DesktopUpdatePill';
import { DesktopUpdater } from './DesktopUpdater';
const { checkFn } = vi.hoisted(() => ({ checkFn: vi.fn<() => Promise<unknown>>() }));
vi.mock('@tauri-apps/plugin-updater', () => ({ check: checkFn }));
vi.mock('$utils/platform', async (importOriginal) => {
const mod = (await importOriginal()) as Record<string, unknown>;
return { ...mod, isDesktopTauri: () => true };
});
vi.mock('$state/hooks/desktopSettings', async (importOriginal) => ({
...((await importOriginal()) as Record<string, unknown>),
useDesktopSetting: () => [true, vi.fn<() => void>()],
}));
vi.mock('$utils/tauriTitlebar', async (importOriginal) => {
const mod = (await importOriginal()) as Record<string, unknown>;
return { ...mod, hasCustomDesktopTitlebar: () => true };
});
// Make exit animations synchronous so hidden elements leave the DOM immediately.
vi.mock('framer-motion', async (importOriginal) => {
const mod = (await importOriginal()) as Record<string, unknown>;
return {
...mod,
AnimatePresence: ({ children }: { children?: ReactNode }) => <>{children}</>,
};
});
function BannersProbe() {
const banners = useAtomValue(globalBannersAtom);
return (
<div data-testid="banners">
{banners.map((b) => (
<section key={b.id} data-testid={`banner-${b.id}`}>
<span>{b.title}</span>
<button type="button" onClick={b.primaryAction.onClick}>
{b.primaryAction.label}
</button>
{b.secondaryAction && (
<button type="button" onClick={b.secondaryAction.onClick}>
{b.secondaryAction.label}
</button>
)}
</section>
))}
</div>
);
}
function CheckNowButton() {
const trigger = useSetAtom(triggerUpdateCheckAtom);
return (
<button type="button" onClick={() => trigger((c) => c + 1)}>
check
</button>
);
}
function makeUpdate(version: string) {
return {
version,
body: `changelog ${version}`,
download: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
install: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
close: vi.fn<() => Promise<void>>().mockResolvedValue(undefined),
};
}
beforeEach(() => {
localStorage.clear();
});
afterEach(() => {
vi.clearAllMocks();
});
describe('DesktopUpdater', () => {
it('reopens the update banner from the pill after dismissing it', async () => {
localStorage.setItem('sable_fake_desktop_update', '1');
render(
<Provider>
<DesktopUpdatePill />
<DesktopUpdater />
<BannersProbe />
</Provider>
);
const pill = await screen.findByRole('button', { name: 'Update Available' });
expect(screen.queryByTestId('banner-desktop-update-ready')).not.toBeInTheDocument();
fireEvent.click(pill);
await screen.findByTestId('banner-desktop-update-ready');
expect(screen.getByRole('button', { name: 'Download & Install' })).toBeInTheDocument();
fireEvent.click(screen.getByRole('button', { name: 'Later' }));
await waitFor(() => {
expect(screen.queryByTestId('banner-desktop-update-ready')).not.toBeInTheDocument();
});
const pillAgain = await screen.findByRole('button', { name: 'Update Available' });
fireEvent.click(pillAgain);
await screen.findByTestId('banner-desktop-update-ready');
expect(screen.getByRole('button', { name: 'Download & Install' })).toBeInTheDocument();
});
it('releases the previous update handle when a new check replaces it after a dismiss', async () => {
const update1 = makeUpdate('1.0.0');
const update2 = makeUpdate('2.0.0');
checkFn.mockResolvedValueOnce(update1).mockResolvedValueOnce(update2);
render(
<Provider>
<DesktopUpdatePill />
<DesktopUpdater />
<BannersProbe />
<CheckNowButton />
</Provider>
);
await waitFor(() => expect(checkFn).toHaveBeenCalledTimes(1));
const pill = await screen.findByRole('button', { name: 'Update Available' });
fireEvent.click(pill);
await screen.findByTestId('banner-desktop-update-ready');
fireEvent.click(screen.getByRole('button', { name: 'Later' }));
await waitFor(() => {
expect(screen.queryByTestId('banner-desktop-update-ready')).not.toBeInTheDocument();
});
expect(update1.close).not.toHaveBeenCalled();
fireEvent.click(screen.getByRole('button', { name: 'check' }));
await waitFor(() => expect(update1.close).toHaveBeenCalledTimes(1));
expect(update2.close).not.toHaveBeenCalled();
});
});
+1 -7
View File
@@ -206,13 +206,7 @@ export function DesktopUpdater() {
const handleDismiss = useCallback(() => {
setDismissed(true);
setBannerVisible(false);
if (!installStartedRef.current) {
closePendingUpdate(pendingUpdateRef.current);
pendingUpdateRef.current = null;
setUpdateInfo(null);
setIsDownloaded(false);
}
}, [closePendingUpdate, setBannerVisible]);
}, [setBannerVisible]);
const bannerData = useMemo<GlobalBanner | null>(() => {
if (!bannerVisible || !updateInfo || dismissed) return null;