0

我正在使用 QUnit 并有一个用于 JQuery UI Widget 的测试脚本:

define(

    ['jquery',
    'knockout',
    '../Widget/SearchPod/searchPod',
    'jqueryui',
    'jquery.ui.widget',
    '../Widget/SearchPod/clr.searchPod'],

function ($, ko, searchPod) {

    var checkSearchBy = function () {
            test('check if select has Search By text', function () {
            var expected = "Search By";
            alert(searchPod.employees);//.checkSearchByWidget());
            deepEqual(searchPod.employees, expected, "We expect drop down text to
            display 'Search By' by default");

            return  1;
        });
    };

    return {
        checkSearchBy: checkSearchBy
    };
  }
);

出于某种原因,每当我运行测试脚本时,都会发生错误,提示上面的参数 searchpod 未定义或为空。searchpod的代码如下:

 require(['jquery',
    'knockout',
    'jqueryui',
    'jquery.ui.widget',
    'domReady!',
    'Widget/SearchPod/clr.searchPod',
    'Widget/Listbox/clr.combobox'],

function ($, ko) {
    $(document).ready(function () {

        $("#search-pod").searchPod({
            ready: function () {              
                var minimumLength = 2;
                $("#search-message").hide();
                //start                    

                //end
                var employees = [*some data*]
                var leaves_filed = [*some data*]
                var claims_filed = = [*some data*]

                function sortData(prop, asc) {
                    data = data.sort(function (a, b) {
                        if (asc) return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
                        else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
                    });
                }

                //This is to display search messages depends on selected search type and 
                function validateCriteria(selectedType) {

                    var searchMessage = 'No values match search criteria';

                    if (selectedType == 'ssn') {

                        if ($("#searchBox").val().length > minimumLength) {

                            searchMessage = 'Must enter all 9 digits of SSN';

                        }

                    }



                    $("#search-message").text(searchMessage);

                }



                // Search pod items

                var selectedType = 'lastname';

                var data = employees;

                // Apply the combobox widget

                $("#searchBy").combobox({

                    ready: function () {



                    },

                    select: function () {

                        selectedType = $("option:selected", this).val();



                        switch (selectedType) {

                            case 'lastname':

                                return;

                                if (employees.length == 1) {

                                    $("#loadingImage").css("display", "");

                                    $("#searchBox").css("display", "none");

                                    $.ajax({

                                        url: "api/Dashboard/GetEmployeeListReport",

                                        type: "GET",

                                        processData: false,

                                        success: function (result) {

                                            employees.length = 0;

                                            employees = result.dataSet.dataTable.row;

                                            $("#loadingImage").css("display", "none");

                                            $("#searchBox").css({ display: '' });

                                            data = employees;

                                        }

                                    });

                                }

                                else { data = employees; }

                                minimumLength = 1;

                                break;

                            case 'ssn':

                                minimumLength = 10;

                                break;

                            case 'eeid':

                                if (employees.length == 1) {

                                    $("#loadingImage").css("display", "");

                                    $("#searchBox").css("display", "none");

                                    $.ajax({

                                        url: "api/Dashboard/GetEmployeeListReport",

                                        type: "GET",

                                        processData: false,

                                        success: function (result) {

                                            employees.length = 0;

                                            employees = result.dataSet.dataTable.row;

                                            $("#loadingImage").css("display", "none");

                                            $("#searchBox").css({ display: '' });

                                            data = employees;

                                            minimumLength = employees[5].eeid.length;

                                        }

                                    });

                                }

                                else { data = employees; minimumLength = 
                                employees[5].eeid.length; }

                                break;

                            case 'leave_number':

                                if (leaves_filed.length == 1) {

                                    $("#loadingImage").css("display", "");

                                    $("#searchBox").css("display", "none");

                                    $.ajax({

                                        url: "api/Dashboard/GetLeavesList",

                                        type: "GET",

                                        processData: false,

                                        success: function (result) {

                                            leaves_filed.length = 0;

                                            leaves_filed = result.dataSet.dataTable.row;

                                            $("#loadingImage").css("display", "none");

                                            $("#searchBox").css({ display: '' });

                                            data = leaves_filed;

                                            minimumLength = leaves_filed[0].leaveNumber.length;

                                        }

                                    });

                                }

                                else { data = leaves_filed; minimumLength = 
                                leaves_filed[0].leaveNumber.length; }



                                break;

                            case 'claim_number':

                                if (claims_filed.length == 1) {

                                    $("#loadingImage").css("display", "");

                                    $("#searchBox").css("display", "none");

                                    $.ajax({

                                        url: "api/Dashboard/GetClaimsList",

                                        type: "GET",

                                        processData: false,

                                        success: function (result) {

                                            claims_filed.length = 0;

                                            claims_filed = result.dataSet.dataTable.row;

                                            $("#loadingImage").css("display", "none");

                                            $("#searchBox").css({ display: '' });

                                            data = claims_filed;

                                            minimumLength = claims_filed[10].claimNumber.length;

                                        }

                                    });

                                }

                                else { data = claims_filed; minimumLength = 
                                claims_filed[10].claimNumber.length; }

                                break;

                        }

                        sortData(selectedType, true);

                        $('#searchBox').val('');

                    }

                });



                sortData(selectedType, true);



                //This will hide search-message when backpress is pressed.

                $('html').keyup(function (e) {

                    if (e.keyCode == 8) {

                        if ($("#searchBox").val().length < minimumLength) {

                            if ($("#search-message").show()) $("#search-message").hide();

                        }

                    }

                    return;

                });



                //Code for Making SSN AutoComplete

                $('#searchBox').keyup(function () {

                    if (selectedType == "ssn" && $('#searchBox').val().length == 10) {

                        var rptParam = "?ssn=" + $('#searchBox').val();

                        var ssnData = [{ "ssn": "", "lastname": "" }];

                        $.ajax({

                            url: "api/Dashboard/GetSsnList" + rptParam,

                            type: "GET",

                            processData: false,

                            success: function (result) {

                                ssnData = result.dataSet.dataTable.row;

                                var ssnArray = [];

                                ssnArray.push(ssnData);

                                if (ssnArray.length > 0) {

                                    $("#searchBox").autocomplete({

                                        minLength: 10,

                                        source: function (request, response) {

                                            var searchField;

                                            var filteredArray = $.map(ssnArray, function (item) {

                                                if (item.ssn != null) {

                                                    searchField = item.ssn;

                                                    if (searchField.toLowerCase().indexOf
                                                    (request.term) == 0 || searchField.indexOf
                                                    (request.term) == 0) {

                                                        return item;

                                                    }

                                                } else { return null; }

                                            });

                                            response(filteredArray);

                                        },

                                        focus: function (event, ui) {

                                            var focusValue;

                                            focusValue = ui.item.ssn;

                                            $("#searchBox").val(focusValue);

                                            return false;

                                        },

                                        select: function (event, ui) {



                                        }

                                    }).data("ui-autocomplete")._renderItem = 
                                    function (ul,item){

                                        return $("<li>")

                                        .append(displayFormat)

                                        .appendTo(ul);

                                    };

                                }

                                $('#searchBox').autocomplete("search");

                            }

                        });

                    }

                    return;

                });



                $("#searchBox").focus(function () {

                    if (selectedType == 'ssn') {

                        $("#searchBox").autocomplete();

                        $("#searchBox").autocomplete("destroy");

                        return;

                    }

                    var searchField;

                    $("#searchBox").autocomplete({

                        minLength: minimumLength,

                        source: function (request, response) {

                            var filteredArray = $.map(data, function (item) {

                                if (selectedType === 'lastname') {

                                    searchField = item.lastname;

                                }

                                if (selectedType === 'ssn') {

                                    return false;

                                }

                                if (selectedType === 'eeid') {

                                    searchField = item.eeid;

                                }

                                if (selectedType === 'leave_number') {

                                    searchField = item.leaveNumber;

                                }

                                if (selectedType === 'claim_number') {

                                    searchField = item.claimNumber;

                                }

                                if (searchField.toLowerCase().indexOf(request.term) == 0 ||

                                    searchField.indexOf(request.term) == 0) {



                                    if (selectedType === 'ssn' && request.term.length < 4) {

                                        return null;

                                    }



                                    return item;

                                }

                                else {

                                    return null;

                                }

                            });



                            if (!filteredArray.length) {

                                $("#search-message").show();

                                validateCriteria(selectedType);

                            }

                            else {

                                $("#search-message").hide();

                            }

                            response(filteredArray);

                        },

                        focus: function (event, ui) {

                            var focusValue;

                            if (selectedType === 'lastname') {

                                focusValue = ui.item.lastname;

                            }

                            if (selectedType === 'eeid') {

                                focusValue = ui.item.eeid;

                            }

                            if (selectedType === 'leave_number') {

                                focusValue = ui.item.leaveNumber;

                            }

                            if (selectedType === 'claim_number') {

                                focusValue = ui.item.claimNumber;

                            }



                            $("#searchBox").val(focusValue);

                            return false;

                        },

                        create: function (event, ui) {

                            $(this).autocomplete("widget").addClass("search-results-list");

                        },

                        open: function (event, ui) {

                            $(".search-results-list li.ui-menu-item").addClass("search-results-item");

                        },

                        select: function (event, ui) {

                            //return false; // Cancel the select event

                            var focusValue;

                            if (selectedType === 'lastname') {

                                focusValue = ui.item.lastname;

                      // TODO: call a function here that will send the name of the report to show

                                $('#report-popup-dialog-overlay').show();

                                $('#report-popup-dialog').show();

                            }



                            if (selectedType === 'eeid') {

                                focusValue = ui.item.eeid;

                            }

                            if (selectedType === 'leave_number') {

                                focusValue = ui.item.leaveNumber;

                                $('#dynamictext').text('Leave Report for ' + focusValue);

                                $('#dynamicHeader').text('Leave Report');

                            }



                            if (selectedType === 'claim_number') {

                                focusValue = ui.item.claimNumber;

                                $('#dynamicHeader').text('Claim Report for ' + focusValue);

                                $('#dynamictext').html("Loading...");

                                //Need to make Ajax Call to get the report.

                                var parameter = "?claimNumber=" + focusValue;

                                $.ajax({

                                    url: "api/ViewReport/GetClaimReport" + parameter,

                                    type: "POST",

                                    processData: false,

                                    success: function (result) {

                                        $('#dynamictext').html("");

                                        $('#dynamictext').html(result);

                                    }

                                });

                            }



                            return false;

                        }

                    })

                    .data("ui-autocomplete")._renderItem = function (ul, item) {



                        var displayFormat = "";


                        return $("<li>")
                        .append(displayFormat)
                        .appendTo(ul);
                    };
                });
            }
        });
    });

    //Test helper functions
    //2. checkSearchBy

    function checkSearchBy() {
        alert($('#searchBy').text());
        return $('#searchBy').text();
    }

    return {
        checkSearchByWidget: function () {
            return checkSearchBy();
        }
    };
});

我已经在这里待了两天,但似乎无法让上面的测试脚本看到 searchpod

4

0 回答 0