I have a method
private processWriteNextChunk(filePath: string): void {
const nextChunk = this.data.get(filePath)?.shift();
if (!nextChunk) return;
appendFile(filePath, nextChunk.buffer, () => {
nextChunk?.last ? this.data.delete(filePath) : this.emitWriteNextChunk(filePath);
});
}
And I'm testing it like
it('should call appendFile when chunk array is not empty and delete data pair if it was last chunk',() => {
let appendFileCallback;
const appendFileSpy = jest.spyOn(fs, 'appendFile').mockImplementation((path, data, callback) => {
appendFileCallback = callback;
});
const data: IBufferChunk = { ...MOCK_DATA, last: true };
service.data.set(MOCK_PATH, [data]);
service.processWriteNextChunk(MOCK_PATH);
expect(appendFileSpy).toHaveBeenCalledWith(MOCK_PATH, data.buffer, appendFileCallback);
expect(service.data.size).toBe(0);
});
test fails with
It happens because this.data.delete(filePath) doesn't have time to work, how I can fix this?