import { describe, it, expect, vi, beforeEach } from 'vitest'; import { ComponentFixture, TestBed } from '@angular/core/testing'; import { provideRouter } from '@angular/router'; import { TaskCreatePage } from '@app/features/tasks/feature/task-create-page/task-create-page'; import { TaskService } from '@app/features/tasks/data-access/services/task'; describe('TaskCreatePage', () => { let component: TaskCreatePage; let fixture: ComponentFixture; let taskService: TaskService; beforeEach(async () => { taskService = { create: vi.fn(), } as unknown as TaskService; await TestBed.configureTestingModule({ imports: [TaskCreatePage], providers: [ provideRouter([]), { provide: TaskService, useValue: taskService }, ], }).compileComponents(); fixture = TestBed.createComponent(TaskCreatePage); component = fixture.componentInstance; fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should have a form with title, description, dueDate and initialNote', () => { expect(component.form.get('title')).toBeTruthy(); expect(component.form.get('description')).toBeTruthy(); expect(component.form.get('dueDate')).toBeTruthy(); expect(component.form.get('initialNote')).toBeTruthy(); }); it('should have title as required', () => { const title = component.form.get('title'); title?.setValue(''); expect(title?.valid).toBe(false); }); it('should have initialNote as required', () => { const initialNote = component.form.get('initialNote'); initialNote?.setValue(''); expect(initialNote?.valid).toBe(false); }); it('should validate title max length', () => { const title = component.form.get('title'); const longTitle = 'a'.repeat(101); title?.setValue(longTitle); expect(title?.valid).toBe(false); }); it('should accept valid title', () => { const title = component.form.get('title'); title?.setValue('Valid Task Title'); expect(title?.valid).toBe(true); }); it('should return true from canDeactivate when form is pristine', () => { expect(component.canDeactivate()).toBe(true); }); it('should return false from canDeactivate when form is dirty', () => { component.form.markAsDirty(); expect(component.canDeactivate()).toBe(false); }); });