{"size": 3239, "ext": "js", "max_stars_repo_path": "src/common/layers/Layer.test.js", "max_stars_repo_name": "geops/mobility-toolbox-js", "max_stars_repo_stars_event_min_datetime": "2020-06-30T07:22:13.000Z", "max_stars_repo_stars_event_max_datetime": "2021-03-26T07:37:52.000Z", "max_issues_repo_path": "src/common/layers/Layer.test.js", "max_issues_repo_name": "geops/mobility-toolbox-js", "max_issues_repo_issues_event_min_datetime": "2020-06-16T07:12:09.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-29T07:33:00.000Z", "max_forks_repo_path": "src/common/layers/Layer.test.js", "max_forks_repo_name": "geops/mobility-toolbox-js", "max_forks_repo_forks_event_min_datetime": "2022-01-12T18:13:51.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-12T18:13:51.000Z"}
{"max_stars_count": 4, "max_issues_count": 148, "max_forks_count": 1, "avg_line_length": 26.7685950413, "max_line_length": 72, "alphanum_fraction": 0.5866008027}
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import Layer from './Layer';

const olLayer = new VectorLayer({ source: new VectorSource() });

describe('Layer', () => {
  test('should initialize.', () => {
    const layer = new Layer({ name: 'Layer', olLayer });
    expect(layer).toBeInstanceOf(Layer);
  });

  test('should be visible by default.', () => {
    const layer = new Layer({ name: 'Layer', olLayer });
    expect(layer.visible).toBe(true);
  });

  test('should be invisible if defined.', () => {
    const layer = new Layer({ name: 'Layer', visible: false, olLayer });
    expect(layer.visible).toBe(false);
  });

  test('should be invisible if set.', () => {
    const layer = new Layer({ name: 'Layer', olLayer });
    layer.setVisible(false);
    expect(layer.visible).toBe(false);
  });

  test('should visibility stay unchanged', () => {
    const layer = new Layer({ name: 'Layer', visible: false, olLayer });
    layer.setVisible(false);
    expect(layer.visible).toBe(false);
  });

  test('should return its name.', () => {
    const layer = new Layer({ name: 'Layer', visible: false, olLayer });
    expect(layer.name).toEqual('Layer');
  });

  test('should called terminate on initialization.', () => {
    const layer = new Layer({ name: 'Layer', olLayer });
    const spy = jest.spyOn(layer, 'terminate');
    layer.init();
    expect(spy).toHaveBeenCalledTimes(1);
  });

  test('should properties correctly set and get.', () => {
    const layer = new Layer({
      name: 'Layer',
      olLayer,
      properties: {
        abc: 'foo',
      },
    });
    expect(layer).toBeInstanceOf(Layer);
    expect(layer.get('abc')).toEqual('foo');

    layer.set('abc', 'bar');
    expect(layer.get('abc')).toEqual('bar');
  });

  test('should set children', () => {
    const layer = new Layer({
      name: 'foo',
      children: [
        new Layer({
          name: 'bar',
        }),
        new Layer({
          name: 'foobar',
          visible: false,
        }),
      ],
    });
    expect(layer.getVisibleChildren().length).toBe(1);
    expect(layer.hasVisibleChildren()).toBe(true);
    expect(layer.hasChildren(false)).toBe(true);

    layer.addChild(
      new Layer({
        name: 'bla',
      }),
    );

    expect(layer.getVisibleChildren().length).toBe(2);

    layer.removeChild('bla');

    expect(layer.getVisibleChildren().length).toBe(1);
  });

  test('should onClick throw error.', () => {
    const layer = new Layer({ name: 'Layer', olLayer });
    expect(() => {
      layer.onClick('not of type function');
    }).toThrow(Error);
  });

  test('should initialize copyrights property.', () => {
    const layer = new Layer({
      name: 'Layer',
      olLayer,
      copyrights: ['&copy: copyright', 'another copyright'],
    });

    expect(layer.copyrights[0]).toEqual('&copy: copyright');
  });

  test('should set and get copyright property.', () => {
    const layer = new Layer({
      name: 'Layer',
      olLayer,
    });
    expect(layer).toBeInstanceOf(Layer);
    expect(layer.copyright).toEqual(undefined);

    layer.copyright = '&copy; OSM Contributors';
    expect(layer.copyright).toEqual('&copy; OSM Contributors');
  });
});
