/* global describe, xdescribe, it, xit, expect, beforeEach, spyOn */

import { JSContext } from "poly-js-utils";
import testResponse from "../../app/fixtures/data.json";
import Models from "../../app/js/models/locator/base";

describe("Locator Model Tests: ", function() {
    beforeEach(function() {
        this.testContext = {
            base_link: "http://testlink/",
            orgId: 1,
            groupId: 38,
            USE_NEW_SEARCH: true,
            theme_data: {
                sort_mappings: {
                    'atoz': 'last_name|asc',
                    'ztoa': 'last_name|desc'
                }
            }
        };
        JSContext.loadData(this.testContext);
    });
    describe("Verify fixture data", function() {
        var agentSearch;
        beforeEach(function() {
            agentSearch = new Models.AgentSearch({mock: true});
            agentSearch.loadData();
        });
        it("should populate with fixture data on intial load", function() {
            expect(agentSearch.list.size()).toEqual(19);
        });
        // Pagination can't really be tested without mocking out a server/something to give an API response.
        xit("should correctly page back and forth", function() {
            agentSearch.pagination.nextPage();
            agentSearch.loadData();
            expect(agentSearch.list.at(0).get("profile").slug)
                .toEqual(testResponse.AgentSearchData.data[10].profile.slug);
            agentSearch.pagination.nextPage();
            agentSearch.loadData();
            expect(agentSearch.list.at(0).get("profile").slug)
                .toEqual(testResponse.AgentSearchData.data[20].profile.slug);
            agentSearch.pagination.nextPage();
            agentSearch.loadData();
            expect(agentSearch.list.at(0).get("profile").slug)
                .toEqual(testResponse.AgentSearchData.data[30].profile.slug);
            agentSearch.pagination.prevPage();
            agentSearch.pagination.prevPage();
            agentSearch.pagination.prevPage();
            agentSearch.loadData();
            expect(agentSearch.list.at(0).get("profile").slug)
                .toEqual(testResponse.AgentSearchData.data[0].profile.slug);
        });
        it("search location should exist in meta of response and be structured [number, number]", function() {
            // assumption: fixture data has initial value with a location as first element in array
            expect(agentSearch.get("meta").location.length).toEqual(2);
            expect(typeof agentSearch.get("meta").location[0]).toEqual("number");
            expect(typeof agentSearch.get("meta").location[1]).toEqual("number");
            expect(agentSearch.get("meta").location).toEqual([37.7648, -122.463]); // San Francisco
        });
        it("sites should have location data and should be structured [number, number]", function() {
            // assumption: fixture data has initial value with a location as first element in array
            expect(agentSearch.list.at(0).get("location").length).toEqual(2);
            expect(typeof agentSearch.list.at(0).get("location")[0]).toEqual("number");
            expect(typeof agentSearch.list.at(0).get("location")[1]).toEqual("number");
            expect(agentSearch.list.at(0).get("location")).toEqual(testResponse.AgentSearchData.data[0].location);
        });
    });
    describe("Filter model tests", function() {
        var filterModel;
        beforeEach(function() {
            filterModel = new Models.Filter();
        });
        it("should output tags as a comma separated string when serialized", function() {
            filterModel.set("tags", ["tag1", "tag2", "tag3"]);
            expect(filterModel.toApiJSON().tags).toEqual("tag1,tag2,tag3");
        });
        it("should handle an empty array of tags", function() {
            filterModel.set("tags", []);
            expect(filterModel.toApiJSON().tags).toEqual("");
        });
        it("should handle a single tag", function() {
            filterModel.set("tags", ["tag1"]);
            expect(filterModel.toApiJSON().tags).toEqual("tag1");
        });
        it("should output correct sort orders based - default relevancy", function() {
            filterModel.set("name", "switcheroo");
            expect(filterModel.toApiJSON().sortBy).toEqual("_score|desc,last_name|asc");
        });
        it("should output correct sort orders based - default location", function() {
            filterModel.set("location", "94306");
            filterModel.set("sortBy", "distance");
            expect(filterModel.toApiJSON().sortBy).toEqual("distance|asc,last_name|asc");
        });
        it("should output correct sort orders based - atoz", function() {
            filterModel.set("name", "switcheroo");
            filterModel.set("sortBy", "atoz");
            expect(filterModel.toApiJSON().sortBy).toEqual("last_name|asc,first_name|asc");
        });
        it("should output correct sort orders based - ztoa", function() {
            filterModel.set("name", "switcheroo");
            filterModel.set("sortBy", "ztoa");
            expect(filterModel.toApiJSON().sortBy).toEqual("last_name|desc,first_name|asc");
        });
        it("should output correct sort orders based - secondary sort", function() {
            this.testContext.theme_data.secondary_sort = [{"sort": "some_field|asc"}];
            JSContext.loadData(this.testContext);
            filterModel.set("name", "switcheroo");
            expect(filterModel.toApiJSON().sortBy).toEqual("_score|desc,some_field|asc");
        });
        it("should output correct sort orders based - secondary sort with atoz", function() {
            this.testContext.theme_data.secondary_sort = [{"sort": "some_field|asc"}];
            JSContext.loadData(this.testContext);
            filterModel.set("name", "switcheroo");
            filterModel.set("sortBy", "atoz");
            expect(filterModel.toApiJSON().sortBy).toEqual("last_name|asc,some_field|asc");
        });
        it("should output correct sort orders based - secondary sort with ztoa", function() {
            this.testContext.theme_data.secondary_sort = [{"sort": "some_field|asc"}];
            JSContext.loadData(this.testContext);
            filterModel.set("name", "switcheroo");
            filterModel.set("sortBy", "ztoa");
            expect(filterModel.toApiJSON().sortBy).toEqual("last_name|desc,some_field|asc");
        });

    });
});
