"use strict";

var _google2address = require("./google2address");
/* eslint camelcase: off*/

describe('google 2 address', function () {
  var deepClone = function deepClone(obj) {
    return JSON.parse(JSON.stringify(obj));
  };
  var aComponent = function aComponent(long_name, short_name) {
    for (var _len = arguments.length, types = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
      types[_key - 2] = arguments[_key];
    }
    return {
      long_name: long_name,
      short_name: short_name,
      types: types
    };
  };
  var aGeometry = function aGeometry(lng, lat) {
    return {
      location: {
        lat: function lat() {
          return lng;
        },
        lng: function lng() {
          return lat;
        }
      }
    };
  };
  var aGoogleResponse = function aGoogleResponse(_ref) {
    var components = _ref.components,
      formatted = _ref.formatted,
      geometry = _ref.geometry,
      types = _ref.types,
      adr_address = _ref.adr_address;
    return {
      geometry: geometry || aGeometry(1, 2),
      address_components: components || [],
      formatted_address: formatted || '',
      types: types || [],
      adr_address: adr_address
    };
  };
  it('should set state according to administrative_area_level_1', function () {
    var someState = 'some-state';
    var component = aComponent(null, someState, 'administrative_area_level_1');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).state).toEqual(someState);
  });
  it('should set the subpremise', function () {
    var aptNumber = '16';
    var component = aComponent(aptNumber, null, 'subpremise');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).subpremise).toEqual(aptNumber);
  });
  describe('city', function () {
    var someLocality = 'some-locality';
    var someSublocality = 'some-sublocality';
    var somePostalTown = 'some-postal_town';
    var localityComponent = aComponent(someLocality, null, 'locality');
    var subLocalityComponent = aComponent(someSublocality, null, 'sublocality');
    var postalTownComponent = aComponent(somePostalTown, null, 'postal_town');
    it('should be according to locality, ignore sublocality and postal_town', function () {
      var components = [localityComponent, subLocalityComponent, postalTownComponent];
      expect((0, _google2address.google2address)(aGoogleResponse({
        components: components
      })).city).toEqual(someLocality);
    });
    it('should be according to sublocality if locality is missing, ignore postal_town', function () {
      var components = [subLocalityComponent, postalTownComponent];
      expect((0, _google2address.google2address)(aGoogleResponse({
        components: components
      })).city).toEqual(someSublocality);
    });
    it('should be according to postal_town if locality and sublocality are missing', function () {
      expect((0, _google2address.google2address)(aGoogleResponse({
        components: [postalTownComponent]
      })).city).toEqual(somePostalTown);
    });
  });
  it('should set street according to route', function () {
    var someStreet = 'some-street';
    var component = aComponent(someStreet, null, 'route');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).street).toEqual(someStreet);
  });
  it('should set country according to country - long name', function () {
    var someCountry = 'some-country';
    var component = aComponent(someCountry, null, 'country');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).country).toEqual(someCountry);
  });
  it('should set country code according to country - short name', function () {
    var someCountryCode = 'some-country-code';
    var component = aComponent(null, someCountryCode, 'country');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).countryCode).toEqual(someCountryCode);
  });
  it('should set postal code according to postal_code', function () {
    var somePostalCode = 'some-postal-code';
    var component = aComponent(somePostalCode, null, 'postal_code');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).postalCode).toEqual(somePostalCode);
  });
  it('should set street number according to street_number', function () {
    var someStreetNumber = 'some-street-number';
    var component = aComponent(someStreetNumber, null, 'street_number');
    expect((0, _google2address.google2address)(aGoogleResponse({
      components: [component]
    })).number).toEqual(someStreetNumber);
  });
  it('should set formatted according to formatted_address', function () {
    var someFormattedAddress = 'some-formatted-address';
    expect((0, _google2address.google2address)(aGoogleResponse({
      formatted: someFormattedAddress
    })).formatted).toEqual(someFormattedAddress);
  });
  it('should set latLng according to geometry when they are functions', function () {
    var someGeometry = aGeometry(100, 22);
    expect((0, _google2address.google2address)(aGoogleResponse({
      geometry: someGeometry
    })).latLng).toEqual({
      lng: 22,
      lat: 100
    });
  });
  it('should be able to accept lagLng as numbers', function () {
    var geometry = {
      location: {
        lat: 777,
        lng: 666
      }
    };
    expect((0, _google2address.google2address)(aGoogleResponse({
      geometry: geometry
    })).latLng).toEqual({
      lng: 666,
      lat: 777
    });
  });
  it('should set approximate if street_address is not in types', function () {
    expect((0, _google2address.google2address)(aGoogleResponse({
      types: ['street_address']
    })).approximate).toBe(false);
    expect((0, _google2address.google2address)(aGoogleResponse({
      types: ['anything else']
    })).approximate).toBe(true);
  });
  it('should set approximate if premise is not in types', function () {
    expect((0, _google2address.google2address)(aGoogleResponse({
      types: ['premise']
    })).approximate).toBe(false);
    expect((0, _google2address.google2address)(aGoogleResponse({
      types: ['anything else']
    })).approximate).toBe(true);
  });
  it('should omit any undefined field', function () {
    expect((0, _google2address.google2address)(aGoogleResponse({}))).toEqual({
      approximate: true,
      latLng: {
        lat: 1,
        lng: 2
      },
      formatted: ''
    });
  });
  it('should extract the formatted street address if available', function () {
    var address = '<span class="street-address">Ha-Namal St 40</span>, <span class="locality">Tel Aviv</span>, <span class="country-name">Israel</span>';
    expect((0, _google2address.google2address)(aGoogleResponse({
      adr_address: address
    })).formattedStreetAddress).toEqual('Ha-Namal St 40');
  });
  it('should complete street address with user input (5-202) in case google api doesnt return it', function () {
    var resultFromGoogle = {
      address_components: [{
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    var userInput = '5-202 Chemin';
    var actual = (0, _google2address.trySetStreetNumberIfNotReceived)(resultFromGoogle, userInput);
    var expected = {
      address_components: [{
        long_name: '5-202',
        short_name: '5-202',
        types: ['street_number']
      }, {
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    expect(actual).toEqual(expected);
  });
  it('should complete street address with user input (5/202) in case google api doesnt return it', function () {
    var resultFromGoogle = {
      address_components: [{
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    var userInput = '5/202 Chemin';
    var actual = (0, _google2address.trySetStreetNumberIfNotReceived)(resultFromGoogle, userInput);
    var expected = {
      address_components: [{
        long_name: '5/202',
        short_name: '5/202',
        types: ['street_number']
      }, {
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    expect(actual).toEqual(expected);
  });
  it('should complete street address with user input (5 202) in case google api doesnt return it', function () {
    var resultFromGoogle = {
      address_components: [{
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    var userInput = '5 202 Chemin';
    var actual = (0, _google2address.trySetStreetNumberIfNotReceived)(resultFromGoogle, userInput);
    var expected = {
      address_components: [{
        long_name: '5 202',
        short_name: '5 202',
        types: ['street_number']
      }, {
        long_name: 'Chemin West Hill',
        short_name: 'Chemin West Hill',
        types: ['route']
      }]
    };
    expect(actual).toEqual(expected);
  });
  it('should not complete street address in case it has been returned from google api', function () {
    var given = {
      address_components: [{
        long_name: '5-202',
        short_name: '5-202',
        types: ['street_number']
      }]
    };
    var expected = deepClone(given);
    expect((0, _google2address.trySetStreetNumberIfNotReceived)(given, '77777 Chemin')).toEqual(expected);
  });
});