簡體   English   中英

Angular中jQuery數據表的回調方法單元測試6

[英]Unit test of jQuery datatable's callback method in Angular 6

無論如何使用業力/茉莉花測試 Angular 6 中 jQuery 數據表的回調方法?

我想測試以下方法的頁腳回調,但我想不出任何方法。 我是單元測試的新手,在 web 上找不到任何有用的東西。

 initializeBugIndexPageSetting() {
    let self = this;
    $('example').DataTable({
        'footerCallback': function (row, data, start, end, display) {
            const api = this.api();
            // Remove the formatting to get integer data for summation
            const intVal = function (i) {
                if (i === 'N/A') {
                    return 0;
                }
                return i * 1;
            };
            // Total over this page
            const pageInProgrssToResolved = api
                .column(14, { page: 'current' })
                .data()
                .reduce(function (a, b) {
                    return intVal(a) + intVal(b);
                }, 0);
            const pageCount = api
                .column(5, { page: 'current' })
                .data().count();
           // Update footer           
            $(api.column(14).footer()).html((pageInProgrssToResolved / pageCount).toFixed(2));
        }
    });
}

一點幫助和指導。

我建議將回調的內容放在一個單獨的方法中,因為這個新的 function 非常容易進行單元測試。

And as a second test I would suggest to test the initializeBugIndexPageSetting function by adding a jasmine spy on the new function that is then called in the footerCallback and you can just check if it has been called (.toHaveBeenCalledTimes(1) on the spy after calling初始化BugIndexPageSetting 函數)。

您可以在這里找到有價值的教程: https://scriptverse.academy/tutorials/jasmine-createspy-createspyobj.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM