'How to mock jquery global $ with jasmine typescript?
I want to test a method in a service that uses the global $ as jquery reference like this:
CLASSES_TO_FIND = ['highlight', 'normal'];
fetchHighlightedTags(): Array<HTMLElement> {
const selectors = this.CLASSES_TO_FIND.map(s => '.' + s).join(', ');
// keep only element not the jquery result Object
const tags: Array<HTMLElement> = [];
const elementsList = $(selectors); //part to mock
elementsList.each(i => {
tags.push(elementsList.get(i));
});
return tags;
}
I would like to mock the $() method, note that it is a scoped variable so there is no import.
describe('DocumentKeywordNavigatorService', () => {
let serviceToTest: ServiceToTest;
const jqueryMock = () => {
console.log("I'm using the mock'");
}
beforeEach(() => {
serviceToTest = new ServiceToTest();
$ = jqueryMock;
})
it('test method', () => {
results = serviceToTest.fetchHighlightedTags();
//assert stuff
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
