<commit_before>/*global describe, it, expect, geo, xit, jasmine*/

describe('widget api', function () {
  'use strict';

  var map, width = 800, height = 800;

  map = geo.map({
    node: '#map',
    center: {
      x: -98.0,
      y: 39.5
    },
    zoom: 1
  });

  map.createLayer('osm');
  map.resize(0, 0, width, height);
  map.createLayer('ui');
  map.draw();

  var uiLayer = map.createLayer('ui');
  var widgets = [];

  describe('a widget stuck to albany', function () {
    widgets.push(uiLayer.createWidget('dom', {
      sticky: true,
      positionType: 'gcs',
      position: {
        x: -73.7572,
        y: 42.6525
      }
    }));

    var widget = widgets[0];

    it('should be the only child of the ui layer', function () {
      expect($(uiLayer.canvas()).children().length).toBe(1);
      expect($(uiLayer.canvas()).children()[0].tagName).toBe('DIV');
    });

    it('should have the UI layer as it\'s parent', function () {
      expect(widget.parent).toEqual(jasmine.any(geo.gui.uiLayer));
    });

    it('should be out of the viewport if we pan to moscow', function () {
      map.transition({
        center: {x: 37.6167, y: 55.7500}
      });

      expect(widget.isInViewport()).toBe(false);
    });

    it('should be back in the viewport if we pan to albany', function () {
      map.transition({
        center: {x: 37.6167, y: 55.7500}
      });

      expect(widget.isInViewport()).toBe(true);
    });
  });

  describe('a sticky widget on the top left', function () {
    widgets.push(uiLayer.createWidget('dom', {
      sticky: true
    }));

    var widget = widgets[1];

    it('should always be in the viewport', function () {
      expect($(widget.canvas()).position()).toBe({top: 0, left: 0});
      expect(widget.isInViewport()).toBe(true);

      map.transition({
        center: {x: 37.6167, y: 55.7500}
      });

      expect(widget.isInViewport()).toBe(true);
      expect($(widget.canvas()).position()).toBe({top: 0, left: 0});
    });
  });

  describe('removing the widgets', function () {
    widgets.map(function (widget) {
      widget._exit();
    });

    it('should leave the ui layer empty', function () {
      expect($(uiLayer.canvas()).children().length).toBe(0);
    });
  });


  describe('an svg widget within a dom widget', function () {
    var domWidget = uiLayer.createWidget('dom');
    var svgWidget = uiLayer.createWidget('svg', {
      parent: domWidget
    });

    it('should be properly structured', function () {
      expect($(uiLayer.canvas()).children().length).toBe(1);
      expect($(svgWidget.canvas()).parent()[0]).toBe($(domWidget.canvas())[0]);
    });

    it('should be removed when its parent is', function () {
      domWidget._exit();
      expect($(uiLayer.canvas()).children().length).toBe(0);
    });
  });
});<commit_msg>Clean and fix widget API tests to pass<commit_after>/*global describe, it, expect, geo, jasmine*/

describe('widget api', function () {
  'use strict';

  var map, width = 800, height = 800;

  map = geo.map({
    node: '#map',
    center: {
      x: -98.0,
      y: 39.5
    },
    zoom: 1
  });

  map.createLayer('osm');
  map.resize(0, 0, width, height);
  map.createLayer('ui');
  map.draw();

  var uiLayer = map.createLayer('ui');
  window.geoWidgets = [];

  it('a widget should have the UI layer as its parent', function (done) {
    var widget = uiLayer.createWidget('dom');

    // @todo this is kind of ugly
    setTimeout(function () {
      done();
    }, 1000);

    expect(widget.parent()).toEqual(jasmine.any(geo.gui.uiLayer));
  });

  it('a widget stuck to albany shouldn\'t be in the viewport ' +
     'if we pan to moscow', function (done) {
       var widget = uiLayer.createWidget('dom', {
         sticky: true,
         positionType: 'gcs',
         position: {
           x: -73.7572,
           y: 42.6525
         }
       });

       map.transition({
         center: {x: 37.6167, y: 55.7500}
       });

       map.geoOff(geo.event.transitionend)
         .geoOn(geo.event.transitionend, function () {
           expect(widget.isInViewport()).toBe(false);
           done();
         });
     });

  it('a widget stuck to albany should be in the viewport if albany is', function (done) {
    var widget = uiLayer.createWidget('dom', {
      sticky: true,
      positionType: 'gcs',
      position: {
        x: -73.7572,
        y: 42.6525
      }
    });

    map.transition({
      center: {x: -73.7572, y: 42.6525}
    });

    map.geoOff(geo.event.transitionend)
      .geoOn(geo.event.transitionend, function () {
        expect(widget.isInViewport()).toBe(true);
        done();
      });
  });

  it('a widget stuck to the top left should always be in the viewport', function (done) {
    var widget = uiLayer.createWidget('dom', {
      sticky: true
    });

    expect($(widget.canvas()).position()).toEqual({top: 0, left: 0});

    map.transition({
      center: {x: 37.6167, y: 55.7500}
    });

    map.geoOff(geo.event.transitionend)
      .geoOn(geo.event.transitionend, function () {
        expect(widget.isInViewport()).toBe(true);
        expect($(widget.canvas()).position()).toEqual({top: 0, left: 0});
        done();
      });
  });

  it('nested widgets should be properly structured', function () {
    var domWidget = uiLayer.createWidget('dom');
    var svgWidget = uiLayer.createWidget('svg', {
      parent: domWidget
    });
    var widgetCount = $(uiLayer.canvas()).children().length;

    expect($(svgWidget.canvas()).parent()[0]).toBe($(domWidget.canvas())[0]);

    // Only top level widgets are children of the UI Layer
    // So removing the domWidget will also remove the svgWidget, but only
    // reduce the number of widgets the UILayer has as children by 1
    domWidget._exit();
    expect($(uiLayer.canvas()).children().length).toBe(widgetCount - 1);
  });
});
{'fraction_non_alphanumeric': 0.1740766073871409, 'fraction_numerical': 0.02787277701778386, 'mean_word_length': 2.61495673671199, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5891605', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1837, 'n_words': 448}