Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 67 additions & 2 deletions src/renderer/updates.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import type { UpdateDownloadProgress, UpdateInfo } from '../shared/update';
import { updateCheckMessage, updateDownloadMessage } from './updates';
import type { UpdateDownloadProgress, UpdateInfo, UpdateState } from '../shared/update';
import { canDownloadUpdate, updateCheckMessage, updateDownloadMessage } from './updates';

describe('updateCheckMessage', () => {
it('formats not checked text as a terminal sentence', () => {
Expand Down Expand Up @@ -50,6 +50,71 @@ describe('updateDownloadMessage', () => {
});
});

describe('canDownloadUpdate', () => {
it('allows download when an update is available and download is idle', () => {
expect(
canDownloadUpdate(
state({
info: info({ status: 'update_available' }),
download: download({ status: 'idle' })
})
)
).toBe(true);
});

it('allows retry when an update is available and download failed', () => {
expect(
canDownloadUpdate(
state({
info: info({ status: 'update_available' }),
download: download({ status: 'download_failed' })
})
)
).toBe(true);
});

it('does not allow download while downloading', () => {
expect(
canDownloadUpdate(
state({
info: info({ status: 'update_available' }),
download: download({ status: 'downloading' })
})
)
).toBe(false);
});

it('does not allow download after update is downloaded', () => {
expect(
canDownloadUpdate(
state({
info: info({ status: 'update_available' }),
download: download({ status: 'downloaded' })
})
)
).toBe(false);
});

it('does not allow download when no update is available', () => {
expect(
canDownloadUpdate(
state({
info: info({ status: 'up_to_date' }),
download: download({ status: 'idle' })
})
)
).toBe(false);
});
});

function state(overrides: Partial<UpdateState>): UpdateState {
return {
info: info({}),
download: download({}),
...overrides
};
}

function info(overrides: Partial<UpdateInfo>): UpdateInfo {
return {
currentVersion: '0.1.0',
Expand Down
5 changes: 4 additions & 1 deletion src/renderer/updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,8 @@ export function updateDownloadMessage(download: UpdateDownloadProgress | null):
}

export function canDownloadUpdate(state: UpdateState | null): boolean {
return state?.info.status === 'update_available' && state.download.status !== 'downloading';
return (
state?.info.status === 'update_available' &&
(state.download.status === 'idle' || state.download.status === 'download_failed')
);
}