Reputation: 1334
I have imported file-saver library in my component inside an Angular project.
import { saveAs } from 'file-saver';
How can I unit test this 'saveAs' function inside my component? Here is how I am using this function.
private downloadFile(filepath: string): void {
this.downloadFileService.downloadFile(filepath).subscribe(result => {
const fileName = 'testfile.txt';
if (result && result.blob()) {
saveAs(result.blob(), fileName);
}
});
}
Upvotes: 1
Views: 665
Reputation: 57116
The idea of unit testing is that you don't test external dependencies. You just test your own code as its own unit in its own right.
The usual approach in your scenario would be to provide a mock function in your unit tests using a Jasmine spy or stub.
It is not your job to unit test a 3rd party lib. The 3rd party lib's interaction with your app will be tested during end to end (protractor) testing.
Upvotes: 1