emi-challenge-fe/tests/app/features/tasks/ui/task-form/task-form.spec.ts

81 lines
2.1 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { TaskForm } from '@app/features/tasks/ui/task-form/task-form';
describe('TaskForm', () => {
let component: TaskForm;
let fixture: ComponentFixture<TaskForm>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TaskForm, ReactiveFormsModule],
}).compileComponents();
fixture = TestBed.createComponent(TaskForm);
component = fixture.componentInstance;
const form = new FormGroup({
title: new FormControl('Test Task', [Validators.required]),
description: new FormControl('Test Description'),
dueDate: new FormControl('2026-12-01'),
});
fixture.componentRef.setInput('form', form);
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should have default submit label', () => {
expect(component.submitLabel()).toBe('Save');
});
it('should have isEditing false by default', () => {
expect(component.isEditing()).toBe(false);
});
it('should have loading false by default', () => {
expect(component.loading()).toBe(false);
});
it('should emit submitted with form values when form is valid', () => {
const spy = vi.fn();
component.submitted.subscribe(spy);
component.onSubmit();
expect(spy).toHaveBeenCalledWith({
title: 'Test Task',
description: 'Test Description',
dueDate: '2026-12-01',
});
});
it('should not emit submitted when form is invalid', () => {
const spy = vi.fn();
component.submitted.subscribe(spy);
const form = new FormGroup({
title: new FormControl('', [Validators.required]),
});
fixture.componentRef.setInput('form', form);
fixture.detectChanges();
component.onSubmit();
expect(spy).not.toHaveBeenCalled();
});
it('should emit cancelled on cancel', () => {
const spy = vi.fn();
component.cancelled.subscribe(spy);
component.onCancel();
expect(spy).toHaveBeenCalledOnce();
});
});