In my code I have a method that returns a part object, however it returns the object inside a setTimeout
method. When I attempt to test this I'm running into the issue that the test does not wait until the timeout has completed before it evaluates the response for the method. How can I go about telling the test to wait for the timeout to complete before it checks the expectation?
As you can see the getIntersection
method returns this.part inside of a timeout.
public getIntersection(payload: DropPayload): Part {
let rect: ClientRect = this.element.nativeElement.getBoundingClientRect();
if (rect.left < payload.event.pageX && rect.right > payload.event.pageX && rect.top < payload.event.pageY && rect.bottom > payload.event.pageY) {
setTimeout(() => {
this.changeRef.detectChanges();
return this.part;
});
}
return null;
}
Here's a few things I've tried based on researching this issue, none of them work:
1:
it('return the part if the payload coordinates are inside the bounding box', async((done) => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
done();
});
}));
2:
it('return the part if the payload coordinates are inside the bounding box', async(() => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
});
}));
3: (this one throws errors that runs
isn't defined. I believe this is an Angular 1 solution)
it('return the part if the payload coordinates are inside the bounding box', () => {
let done: boolean = false;
let result: any;
runs(() => {
partInstance.part = Clone.create(mockPart);
result = partInstance.getIntersection(mockPayload);
setTimeout(() => {
done = true;
});
});
waitsFor(() => {
return done;
});
runs(() => {
expect(result).toEqual(mockPart);
});
});
4:
it('return the part if the payload coordinates are inside the bounding box', () => {
partInstance.part = Clone.create(mockPart);
let result: any = partInstance.getIntersection(mockPayload);
setTimeout(() => {
expect(result).toEqual(mockPart);
});
});