Skip to content

Instantly share code, notes, and snippets.

@lydemann
Last active October 28, 2018 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lydemann/3e3e80ae13716d0ac5a4a4ea3d9487f7 to your computer and use it in GitHub Desktop.
Save lydemann/3e3e80ae13716d0ac5a4a4ea3d9487f7 to your computer and use it in GitHub Desktop.
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