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

98 lines
2.9 KiB
TypeScript
Raw Normal View History

import { describe, it, expect, vi, beforeEach } from 'vitest';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TaskCreateSidebar } from '@app/features/tasks/ui/task-create-sidebar/task-create-sidebar';
import { TaskService } from '@app/features/tasks/data-access/services/task';
import { of } from 'rxjs';
import { TaskBuilder } from '@tests/builders/task.builder';
describe('TaskCreateSidebar', () => {
let component: TaskCreateSidebar;
let fixture: ComponentFixture<TaskCreateSidebar>;
let taskService: TaskService;
beforeEach(async () => {
taskService = {
create: vi.fn().mockReturnValue(of(new TaskBuilder().build())),
addNote: vi.fn().mockReturnValue(of(new TaskBuilder().build())),
} as unknown as TaskService;
await TestBed.configureTestingModule({
imports: [TaskCreateSidebar],
providers: [
{ provide: TaskService, useValue: taskService },
],
}).compileComponents();
fixture = TestBed.createComponent(TaskCreateSidebar);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should render sidebar title', () => {
const title = fixture.nativeElement.querySelector('.sidebar__title');
expect(title?.textContent).toContain('New Task');
});
it('should have a form with title, description, dueDate and initialNote fields', () => {
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 initialNote as required', () => {
const initialNote = component.form.get('initialNote');
initialNote?.setValue('');
expect(initialNote?.valid).toBe(false);
});
it('should accept valid initialNote', () => {
const initialNote = component.form.get('initialNote');
initialNote?.setValue('This is a valid note');
expect(initialNote?.valid).toBe(true);
});
it('should emit close event', () => {
const spy = vi.fn();
component.close.subscribe(spy);
component.onClose();
expect(spy).toHaveBeenCalledOnce();
});
it('should not emit created when form is invalid', () => {
const spy = vi.fn();
component.created.subscribe(spy);
component.onSubmit();
expect(spy).not.toHaveBeenCalled();
});
it('should emit created when form is valid', () => {
const spy = vi.fn();
component.created.subscribe(spy);
component.form.patchValue({ title: 'New Task', initialNote: 'First note' });
component.onSubmit();
expect(spy).toHaveBeenCalledOnce();
});
it('should render initialNote field in template', () => {
const field = fixture.nativeElement.querySelector('#create-initialNote');
expect(field).toBeTruthy();
});
});