-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.spec.ts
More file actions
57 lines (43 loc) · 1.85 KB
/
test.spec.ts
File metadata and controls
57 lines (43 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { TasksComponent } from './src/app/components/tasks/tasks.component';
import { TaskService } from './src/app/services/task.service';
import { of, throwError } from 'rxjs';
import { Task } from './src/app/Task';
describe('TasksComponent', () => {
let component: TasksComponent;
let fixture: ComponentFixture<TasksComponent>;
let mockTaskService:any;
beforeEach(async () => {
mockTaskService = jasmine.createSpyObj(['getTasks', 'deleteTask', 'updateTaskReminder', 'addTask']);
await TestBed.configureTestingModule({
declarations: [TasksComponent],
providers: [
{ provide: TaskService, useValue: mockTaskService },
],
}).compileComponents();
fixture = TestBed.createComponent(TasksComponent);
component = fixture.componentInstance;
});
it('should fetch tasks on init', () => {
const mockTasks: Task[] = [{ id: 1, text: 'Test Task',day:'day', reminder: false }];
mockTaskService.getTasks.and.returnValue(of(mockTasks));
component.ngOnInit();
expect(component.tasks).toEqual(mockTasks);
expect(mockTaskService.getTasks).toHaveBeenCalled();
});
it('should delete a task', () => {
const mockTask: Task = { id: 1, text: 'Test Task',day:'day', reminder: false };
component.tasks = [mockTask];
mockTaskService.deleteTask.and.returnValue(of(null));
component.deleteTask(mockTask);
expect(component.tasks).toEqual([]);
expect(mockTaskService.deleteTask).toHaveBeenCalledWith(mockTask);
});
it('should handle error on fetch tasks', () => {
spyOn(console, 'error');
mockTaskService.getTasks.and.returnValue(throwError('Error'));
component.ngOnInit();
expect(console.error).toHaveBeenCalledWith('Error fetching tasks:', 'Error');
});
// Add more tests for toggleReminder and addTask methods
});