<commit_before>define([
    "expect",
    "jquery",
    "sinon",
    "pat-registry",
    "mockup-patterns-livesearch",
], function (expect, $, sinon, registry, Livesearch) {
    "use strict";

    window.mocha.setup("bdd");
    $.fx.off = true;

    /* ==========================
   TEST: Livesearch 
  ========================== */

    describe("Livesearch", function () {
        beforeEach(function () {
            this.server = sinon.fakeServer.create();
            this.server.autoRespond = true;

            this.server.respondWith("GET", /livesearch\.json/, function (xhr, id) {
                var items = [];
                for (var i = 0; i < 7; i = i + 1) {
                    items.push({
                        url: "http://localhost:8081/news/aggregator",
                        description: "Site News",
                        title: "News",
                        state: "published",
                    });
                }
                xhr.respond(
                    200,
                    { "Content-Type": "application/json" },
                    JSON.stringify({
                        total: items.length * 2,
                        items: items,
                    })
                );
            });
            this.clock = sinon.useFakeTimers();

            this.$el = $(
                "" +
                    '<form class="pat-livesearch">' +
                    ' <input type="text" value="foobar" />' +
                    "</form>"
            ).appendTo($("body"));

            this.ls = new Livesearch(this.$el, {
                ajaxUrl: "livesearch.json",
            });
        });

        afterEach(function () {
            $("body").empty();
            this.server.restore();
            this.clock.restore();
            this.$el.remove();
        });

        it("results on focus", function () {
            this.ls.$input.trigger("focusin");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(9);
        });

        it("responds to keyup", function () {
            this.ls.$input.trigger("keyup");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(9);
        });

        it("does not show with minimum length not met", function () {
            this.ls.$input.attr("value", "fo");
            this.ls.$input.trigger("keyup");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(1);
        });

        it("focus out hides", function () {
            this.ls.$input.trigger("keyup");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(9);

            this.ls.$input.trigger("focusout");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results").is(":visible")).to.equal(false);
        });

        it("focus back in shows already searched", function () {
            this.ls.$input.trigger("keyup");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(9);

            this.ls.$input.trigger("focusout");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results").is(":visible")).to.equal(false);

            this.ls.$input.trigger("focusin");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results").is(":visible")).to.equal(true);
        });

        it("should show next and prev", function () {
            this.ls.$input.trigger("keyup");
            this.clock.tick(1000);
            expect(this.$el.find(".livesearch-results li").length).to.equal(9);
            expect(this.$el.find(".livesearch-results li a.next").length).to.equal(1);

            this.ls.doSearch(2);
            this.clock.tick(1000);

            expect(this.$el.find(".livesearch-results li").length).to.equal(9);
            expect(this.$el.find(".livesearch-results li a.prev").length).to.equal(1);
        });
    });
});<commit_msg>Ported livesearch test. 2 failures remaining, related to focus.<commit_after>import "./livesearch";
import Livesearch from "./livesearch";
import $ from "jquery";
import registry from "@patternslib/patternslib/src/core/registry";
import sinon from "sinon";

describe("Livesearch", function () {
    beforeEach(function () {
        this.server = sinon.fakeServer.create();
        this.server.autoRespond = true;

        this.server.respondWith("GET", /livesearch\.json/, function (xhr, id) {
            var items = [];
            for (var i = 0; i < 7; i = i + 1) {
                items.push({
                    url: "http://localhost:8081/news/aggregator",
                    description: "Site News",
                    title: "News",
                    state: "published",
                });
            }
            xhr.respond(
                200,
                { "Content-Type": "application/json" },
                JSON.stringify({
                    total: items.length * 2,
                    items: items,
                })
            );
        });
        this.clock = sinon.useFakeTimers();

        this.$el = $(
            "" +
                '<form class="pat-livesearch">' +
                ' <input type="text" value="foobar" />' +
                "</form>"
        ).appendTo($("body"));

        this.ls = new Livesearch(this.$el, {
            ajaxUrl: "livesearch.json",
        });
    });

    afterEach(function () {
        $("body").empty();
        this.server.restore();
        this.clock.restore();
        this.$el.remove();
    });

    it("results on focus", function () {
        this.ls.$input.trigger("focusin");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(9);
    });

    it("responds to keyup", function () {
        this.ls.$input.trigger("keyup");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(9);
    });

    it("does not show with minimum length not met", function () {
        this.ls.$input.attr("value", "fo");
        this.ls.$input.trigger("keyup");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(1);
    });

    it("focus out hides", function () {
        this.ls.$input.trigger("keyup");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(9);

        this.ls.$input.trigger("focusout");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results").is(":visible")).toEqual(false);
    });

    it("focus back in shows already searched", function () {
        this.ls.$input.trigger("keyup");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(9);

        this.ls.$input.trigger("focusout");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results").is(":visible")).toEqual(false);

        this.ls.$input.trigger("focusin");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results").is(":visible")).toEqual(true);
    });

    it("should show next and prev", function () {
        this.ls.$input.trigger("keyup");
        this.clock.tick(1000);
        expect(this.$el.find(".livesearch-results li").length).toEqual(9);
        expect(this.$el.find(".livesearch-results li a.next").length).toEqual(1);

        this.ls.doSearch(2);
        this.clock.tick(1000);

        expect(this.$el.find(".livesearch-results li").length).toEqual(9);
        expect(this.$el.find(".livesearch-results li a.prev").length).toEqual(1);
    });
});
{'fraction_non_alphanumeric': 0.1815483870967742, 'fraction_numerical': 0.015870967741935485, 'mean_word_length': 1.7791323054858372, 'pattern_counts': {'":': 8, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '234111', 'n_tokens_mistral': 2336, 'n_tokens_neox': 2178, 'n_words': 327}