<commit_before>/* global it, describe, expect, spyOn */

import React from 'react';
import {shallow, mount, configure} from 'enzyme';
import  Adapter from 'enzyme-adapter-react-16';
import {createStore, combineReducers} from 'redux';

import MapReducer from '../../src/reducers/map';
import MapInfoReducer from '../../src/reducers/mapinfo';
import DrawingReducer from '../../src/reducers/drawing';
import ConnectedMap, {MapboxGL, getMapExtent} from '../../src/components/mapboxgl';
import SdkPopup from '../../src/components/map/popup';

configure({adapter: new Adapter()});

const createMapDrawMock = () => {
  return {
    changeMode: () => {},
    getAll: () => {},
  };
};
const createMapMock = () => {
  return {
    getSource: () => {
      return {
        setData: () => {
        }
      };
    },
    setStyle: () => {},
    setCenter: () => {},
    setBearing: () => {},
    setZoom: () => {},
    addControl: () => {},
    queryRenderedFeatures: () => {
      return [{
        layer: {
          id: 'foo',
        },
      }];
    }
  };
};

describe('MapboxGL component', () => {
  it('should render without throwing an error', () => {
    const wrapper = shallow(<MapboxGL />);
    expect(wrapper.find('.sdk-map').length).toBe(1);
  });

  it('should allow for custom className', () => {
    const wrapper = shallow(<MapboxGL className='foo' />);
    expect(wrapper.find('.foo').length).toBe(1);
  });

  it('should set layersVersion and sourcesVersion', () => {
    const sources = {};
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
    };

    const center = [0, 0];
    const zoom = 2;
    const apiKey = 'foo';
    const wrapper = mount(<MapboxGL
      mapbox={{accessToken: apiKey}}
      map={{center, zoom, sources, layers, metadata}}
    />);
    const map = wrapper.instance();
    expect(map.sourcesVersion).toBe(0);
    expect(map.layersVersion).toBe(0);
  });

  it('should return the correct draw mode', () => {
    const wrapper = shallow(<MapboxGL className='foo' />);
    const map = wrapper.instance();
    expect(map.getMode('Point')).toBe('draw_point');
    expect(map.getMode('LineString')).toBe('draw_line_string');
    expect(map.getMode('Polygon')).toBe('draw_polygon');
  });

  it('shouldComponentUpdate works as expected', () => {
    const sources = {
      geojson: {
        type: 'geojson',
        data: {
          features: []
        }
      },
    };
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
      'bnd:data-version:geojson': 0,
    };

    const center = [0, 0];
    const zoom = 2;
    const apiKey = 'foo';
    const wrapper = mount(<MapboxGL
      mapbox={{accessToken: apiKey}}
      map={{center, zoom, sources, layers, metadata}}
    />);
    const map = wrapper.instance();
    // mock up our GL map
    map.map = createMapMock();
    map.draw = createMapDrawMock();
    spyOn(map.map, 'setStyle');
    spyOn(map.map, 'setCenter');
    spyOn(map.map, 'setBearing');
    spyOn(map.map, 'setZoom');
    let newMetadata = Object.assign({}, metadata);
    newMetadata['bnd:source-version'] = 1;
    newMetadata['bnd:data-version:geojson'] = 1;
    let nextProps = {
      map: {
        center: [10, 10],
        bearing: 45,
        zoom: 3,
        sources: sources,
        metadata: newMetadata,
      },
      drawing: {
        interaction: 'Point',
        sourceName: 'geojson',
      },
    };
    const types = [];
    const on = (type) => {
      types.push(type);
    };
    map.map.on = on;
    const removeControl = () => {};
    map.map.removeControl = removeControl;
    spyOn(map.map, 'removeControl');
    map.shouldComponentUpdate(nextProps);
    expect(types).toEqual(['draw.create', 'draw.update']);
    expect(map.map.setStyle).toHaveBeenCalled();
    expect(map.map.setCenter).toHaveBeenCalled();
    expect(map.map.setBearing).toHaveBeenCalled();
    expect(map.map.setZoom).toHaveBeenCalled();
    expect(map.sourcesVersion).toBe(1);
    spyOn(map, 'updateInteraction').and.callThrough();
    nextProps.drawing = {
      interaction: 'Polygon',
      sourceName: 'geojson',
    };
    map.shouldComponentUpdate(nextProps);
    expect(map.updateInteraction).toHaveBeenCalled();
    delete nextProps.map.metadata;
    nextProps.map.sources = {};
    map.shouldComponentUpdate(nextProps);
    expect(map.sourcesVersion).not.toBeDefined();
    expect(map.layersVersion).not.toBeDefined();
  });

  it('onClick is triggered', () => {
    const sources = {
      geojson: {
        type: 'geojson',
      },
    };
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
      'bnd:data-version:geojson': 0,
    };
<commit_msg>fixed test<commit_after>/* global it, describe, expect, spyOn */

import React from 'react';
import {shallow, mount, configure} from 'enzyme';
import  Adapter from 'enzyme-adapter-react-16';
import {createStore, combineReducers} from 'redux';

import MapReducer from '../../src/reducers/map';
import MapInfoReducer from '../../src/reducers/mapinfo';
import DrawingReducer from '../../src/reducers/drawing';
import ConnectedMap, {MapboxGL, getMapExtent} from '../../src/components/mapboxgl';
import SdkPopup from '../../src/components/map/popup';

configure({adapter: new Adapter()});

const createMapDrawMock = () => {
  return {
    changeMode: () => {},
    getAll: () => {},
  };
};
const createMapMock = () => {
  return {
    getSource: () => {
      return {
        setData: () => {
        }
      };
    },
    setStyle: () => {},
    setCenter: () => {},
    setBearing: () => {},
    setZoom: () => {},
    addControl: () => {},
    queryRenderedFeatures: () => {
      return [{
        layer: {
          id: 'foo',
        },
      }];
    }
  };
};

describe('MapboxGL component', () => {
  it('should render without throwing an error', () => {
    const wrapper = shallow(<MapboxGL />);
    expect(wrapper.find('.sdk-map').length).toBe(1);
  });

  it('should allow for custom className', () => {
    const wrapper = shallow(<MapboxGL className='foo' />);
    expect(wrapper.find('.foo').length).toBe(1);
  });

  it('should set layersVersion and sourcesVersion', () => {
    const sources = {};
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
    };

    const center = [0, 0];
    const zoom = 2;
    const apiKey = 'foo';
    const wrapper = mount(<MapboxGL
      mapbox={{accessToken: apiKey}}
      map={{center, zoom, sources, layers, metadata}}
    />);
    const map = wrapper.instance();
    expect(map.sourcesVersion).toBe(0);
    expect(map.layersVersion).toBe(0);
  });

  it('should return the correct draw mode', () => {
    const wrapper = shallow(<MapboxGL className='foo' />);
    const map = wrapper.instance();
    expect(map.getMode('Point')).toBe('draw_point');
    expect(map.getMode('LineString')).toBe('draw_line_string');
    expect(map.getMode('Polygon')).toBe('draw_polygon');
  });

  it('shouldComponentUpdate works as expected', () => {
    const sources = {
      geojson: {
        type: 'geojson',
        data: {
          features: []
        }
      },
    };
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
      'bnd:data-version:geojson': 0,
    };

    const center = [0, 0];
    const zoom = 2;
    const apiKey = 'foo';
    const wrapper = mount(<MapboxGL
      mapbox={{accessToken: apiKey}}
      map={{center, zoom, sources, layers, metadata}}
    />);
    const map = wrapper.instance();
    // mock up our GL map
    map.map = createMapMock();
    map.draw = createMapDrawMock();
    spyOn(map.map, 'setStyle');
    spyOn(map.map, 'setCenter');
    spyOn(map.map, 'setBearing');
    spyOn(map.map, 'setZoom');
    let newMetadata = Object.assign({}, metadata);
    newMetadata['bnd:source-version'] = 1;
    newMetadata['bnd:data-version:geojson'] = 1;
    let nextProps = {
      map: {
        center: [10, 10],
        bearing: 45,
        zoom: 3,
        sources: sources,
        metadata: newMetadata,
      },
      drawing: {
        interaction: 'Point',
        sourceName: 'geojson',
      },
    };
    const types = [];
    const on = (type) => {
      types.push(type);
    };
    map.map.on = on;
    const removeControl = () => {};
    map.map.removeControl = removeControl;
    spyOn(map.map, 'removeControl');
    map.componentWillReceiveProps(nextProps);
    expect(types).toEqual(['draw.create', 'draw.update']);
    expect(map.map.setStyle).toHaveBeenCalled();
    expect(map.map.setCenter).toHaveBeenCalled();
    expect(map.map.setBearing).toHaveBeenCalled();
    expect(map.map.setZoom).toHaveBeenCalled();
    expect(map.sourcesVersion).toBe(1);
    spyOn(map, 'updateInteraction').and.callThrough();
    nextProps.drawing = {
      interaction: 'Polygon',
      sourceName: 'geojson',
    };
    map.componentWillReceiveProps(nextProps);
    expect(map.updateInteraction).toHaveBeenCalled();
    delete nextProps.map.metadata;
    nextProps.map.sources = {};
    map.componentWillReceiveProps(nextProps);
    expect(map.sourcesVersion).not.toBeDefined();
    expect(map.layersVersion).not.toBeDefined();
  });

  it('onClick is triggered', () => {
    const sources = {
      geojson: {
        type: 'geojson',
      },
    };
    const layers = [];
    const metadata = {
      'bnd:source-version': 0,
      'bnd:layer-version': 0,
      'bnd:data-version:geojson': 0,
    };

{'fraction_non_alphanumeric': 0.17731312075274439, 'fraction_numerical': 0.006272869837950863, 'mean_word_length': 2.778041074249605, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 55, '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': '5756225', 'n_tokens_mistral': 3078, 'n_tokens_neox': 3038, 'n_words': 585}