Instantly share code, notes, and snippets.

Embed
What would you like to do?
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
AddTodoComponent,
],
imports: [
FormsModule,
TranslateModule.forRoot(),
],
providers: [
SpyHelper.provideMagicalMock(TodoListService)
],
schemas: [NO_ERRORS_SCHEMA]
})
.compileComponents();
}));
let todoListServiceMock: jasmine.SpyObj<TodoListService>;
beforeEach(() => {
todoListServiceMock = TestBed.get(TodoListService);
fixture = TestBed.createComponent(AddTodoComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should update todo item when todo item is in todo list', () => {
// Arrange
const todoList = [
{ id: 'task1', title: 'Buy Milk', description: 'Remember to buy milk' },
{ id: 'task2', title: 'Go to the gym', description: 'Remember to work out' }
];
(todoListServiceMock as any).todoList = todoList;
todoListServiceMock.updateTodo.and.returnValue(of([]));
// Act
component.currentTODO = todoList[0];
const form = new NgForm([], []);
component.save(form);
// Assert
expect(todoListServiceMock.updateTodo).toHaveBeenCalledWith(component.currentTODO);
});
it('should add new todo item when todo item is not in todo list', () => {
// Arrange
const newTodo = { id: 'lala1', title: 'Buy Milk', description: 'Remember to buy milk' };
const todoList = [
{ id: 'task1', title: 'Buy Milk', description: 'Remember to buy milk' },
{ id: 'task2', title: 'Go to the gym', description: 'Remember to work out' }
];
(todoListServiceMock as any).todoList = todoList;
todoListServiceMock.addTodo.and.returnValue(of([]));
// Act
component.currentTODO = newTodo;
const form = new NgForm([], []);
debugger;
component.save(form);
// Assert
expect(todoListServiceMock.addTodo).toHaveBeenCalledWith(newTodo);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment