MOON
Server: Apache
System: Linux nserver.cafsindia.com 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: cafsindia (1002)
PHP: 8.2.30
Disabled: NONE
Upload Files
File: /home/cafsindia/.trash/dist.1/jquery-typeahead/test/integration/request-test.js
var expect = require('chai').expect,
    jQuery = $ = require("jquery"),
    Typeahead = require('../../src/jquery.typeahead')(jQuery, window);

describe('Typeahead request Tests', function () {
    'use strict';

    let myTypeahead,
        hasBeforeSend = false,
        hasComplete = false,
        hasDone = false,
        hasFail = false,
        hasThen = false,
        hasAlways = false;

    describe('$ajax.request as an Object', function () {

        before(function (done) {

            document.write('<input class="js-typeahead-request-object">');

            myTypeahead = $.typeahead({
                input: '.js-typeahead-request-object',
                minLength: 0,
                generateOnLoad: true,
                source: {
                    ajax: {
                        url: "http://www.gamer-hub.com/tag/list.json",
                        dataType: "jsonp",
                        path: "data",
                        beforeSend: function (jqXHR, options) {
                            hasBeforeSend = true;
                        },
                        complete: function () {
                            hasComplete = true;
                            setTimeout(function(){
                                done();
                            }, 250);
                        },
                        callback: {
                            done: function (data) {
                                hasDone = true;
                                return data;
                            },
                            fail: function () {
                                hasFail = true;
                            },
                            then: function () {
                                hasThen = true;
                            },
                            always: function () {
                                hasAlways = true;
                            }
                        }
                    }
                }
            });
        });

        it('should merge Typeahead $.ajax object', function () {
            expect(myTypeahead.source.group.length).to.be.above(100);
            expect(hasBeforeSend).to.be.true;
            expect(hasComplete).to.be.true;
            expect(hasDone).to.be.true;
            expect(hasFail).to.be.false;
            expect(hasThen).to.be.true;
            expect(hasAlways).to.be.true;
            expect(!!~myTypeahead.requests.group.request.beforeSend.toString().indexOf('scope.xhr[group] = jqXHR;')).to.be.true;
        });
    });

    describe('$ajax.request as an Array', function () {
        before(function (done) {

            hasBeforeSend = false;
            hasComplete = false;
            hasDone = false;
            hasFail = false;
            hasThen = false;
            hasAlways = false;

            document.write('<input class="js-typeahead-request-array">');

            myTypeahead = $.typeahead({
                input: '.js-typeahead-request-array',
                minLength: 0,
                generateOnLoad: true,
                source: {
                    ajax: [function (query) {
                        return {
                            url: `http://www.gamer-hub.com/tag/list.json?q=${query}`,
                            dataType: "jsonp",
                            beforeSend: function (jqXHR, options) {
                                hasBeforeSend = true;
                            },
                            complete: function () {
                                hasComplete = true;
                                setTimeout(function(){
                                    done();
                                }, 250);
                            },
                            callback: {
                                done: function (data) {
                                    hasDone = true;
                                    return data;
                                },
                                fail: function () {
                                    hasFail = true;
                                },
                                then: function () {
                                    hasThen = true;
                                },
                                always: function () {
                                    hasAlways = true;
                                }
                            }
                        }
                    }, "data"]
                }
            });
        });

        it('should merge Typeahead Array into $.ajax object', function () {
            myTypeahead.node.val('test');
            myTypeahead.node.trigger('input.typeahead');
            myTypeahead.node.trigger('generate.typeahead');

            expect(!!~myTypeahead.requests.group.request.url.indexOf('?q=test')).to.be.true;
            expect(myTypeahead.source.group.length).to.be.above(100);
            expect(hasBeforeSend).to.be.true;
            expect(hasComplete).to.be.true;
            expect(hasDone).to.be.true;
            expect(hasFail).to.be.false;
            expect(hasThen).to.be.true;
            expect(hasAlways).to.be.true;
            expect(!!~myTypeahead.requests.group.request.beforeSend.toString().indexOf('scope.xhr[group] = jqXHR;')).to.be.true;
        });
    });

});