I have the following jasmine test spec which triggers the mouse events in my directive. As you can see there is a lot of code duplication here, I am repeating the events here.
describe('Test for my module', () => {
let fixture: ComponentFixture<TestComponent>;
let de: DebugElement[];
let service: MyService = new MyService();
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [Move, TestComponent],
providers: [MyService]
});
fixture = TestBed.createComponent(TestComponent);
service = fixture.debugElement.injector.get(MyService);
fixture.detectChanges();
de = fixture.debugElement.queryAll(By.directive(Move));
});
// My test spec. This is where I need advice for removing the code duplication.
it('test the move event', inject([MyService], (service: MyService) => {
let object = spyOn(service.x, 'emit');
spyObj.and.callThrough();
de[0].triggerEventHandler('mousedown', { pageX: 10, pageY: 10 });
de[0].triggerEventHandler('mousemove', { pageX: 40, pageY: 20 });
expect(service.x.emit).toHaveBeenCalledWith({ x: 30, y: 10 });
object.calls.reset();
de[0].triggerEventHandler('mousemove', { pageX: 45, pageY: 25 });
expect(service.x.emit).toHaveBeenCalledWith({ x: 35, y: 15 });
object.calls.reset();
de[0].triggerEventHandler('mousemove', { pageX: 50, pageY: 30 });
expect(service.x.emit).toHaveBeenCalledWith({ x: 40, y: 20 });
}));
Could anyone help me by suggesting any way to avoid this duplication?