1

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();
        })
    });
});
3
  • page: pageFunction() should be page: pageFunction. You're calling the function when you return the object, not when MainPage.page() is executed.
    – Barmar
    Commented Jul 4 at 19:25
  • @Barmar i need help triggering the callback function $(".page").on("shown.bs.collapse, hidden.bs.collapse", '.module.content', function (e) { return null }); is there any way i can trigger this callback with this event "shown.bs.collapse, hidden.bs.collapse"
    – keerthi
    Commented Jul 5 at 6:35
  • 1
    You trigger the event with $(selector).trigger("shown.bs.collapse") where selector selects one of the elements that you're delegating to.
    – Barmar
    Commented Jul 5 at 14:31

0

Browse other questions tagged or ask your own question.