I have a JS script where using jQuery with multiple class selectors and a callback function
function MainPage(){
function pageFunction(){
$(".page").on("shown.bs.collapse, hidden.bs.collapse", '.module.content', function (e) {
return null
});
}
return {
page: pageFunction()
}
}
module.exports = MainPage
I tried to trigger the callback function from the jest unit test cases but had no luck. Do i need to change the html?
const MainPage = require('./mainpage';)
$('<div>').addClass('mainpage').appendTo('body');
var htmlcontent = `<div class="page">
<div class="shown">
<div class="bs">
<div class="collapse">
Test
</div>
</div>
</div>
<div class='module'>
<div class='header'></div>
<div class='content'></div>
</div>
</div>`;
$('.mainpage').html(htmlcontent);
describe("test cases mainpage", () => {
test("testing function call", () => {
return new window.Promise((done) => {
MainPage.page();
done();
})
});
});
page: pageFunction()
should bepage: pageFunction
. You're calling the function when you return the object, not whenMainPage.page()
is executed.$(selector).trigger("shown.bs.collapse")
whereselector
selects one of the elements that you're delegating to.