Skip to content

Commit

Permalink
add TaskCheckbox tests
Browse files Browse the repository at this point in the history
  • Loading branch information
narthur committed Nov 20, 2024
1 parent 688177a commit f8496ca
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/components/TaskCheckbox.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { describe, it, expect, vi } from 'vitest';
import { updateTask } from '@taskratchet/sdk';

vi.mock('@taskratchet/sdk', () => ({
updateTask: vi.fn(),
}));

describe('TaskCheckbox', () => {
it('should update task when toggled', async () => {
const task: TaskType = {
id: '123',
task: 'test task',
due: '2024-01-01T00:00:00',
cents: 100,
complete: false,
status: 'pending',
timezone: 'UTC'
};

vi.setSystemTime(new Date('2023-12-01'));
const event = new Event('change');

await toggleComplete(event, task);

expect(updateTask).toHaveBeenCalledWith('123', { complete: true });
});
});

async function toggleComplete(event: Event, task: TaskType) {
if (!task.id) return;

const taskDue = new Date(task.due);
const now = new Date();
const isPastDue = taskDue < now;

if (isPastDue && task.complete) {
const confirmed = confirm(
'This task is past due. Marking it incomplete will require contacting support to undo. Continue?'
);
if (!confirmed) return;
}

await updateTask(task.id, { complete: !task.complete });
task.complete = !task.complete;
task.status = task.complete ? 'complete' : 'pending';
}

0 comments on commit f8496ca

Please sign in to comment.