Como cobrir a linha com testes

问题描述 投票:0回答:0

我有这个代码:

updateAlarmWithTag(alarm: AlarmData) {
this.geometricLayers.markers.forEach((marker) => {
  if (marker.tag === alarm.tagInfo.tag && marker.layer) {
    if (alarm.alarm) {
      const date = new Date(alarm.ts);
      const format = 'dd/MM/yyyy HH:mm:ss';
      const locale = $localize.locale != null ? $localize.locale : 'en-US';

      const content = this.mapDrawService.changePopUpContent(
        parseInt(marker.id),
        'Alarmed - ' + formatDate(date, format, locale) + '<br>' + alarm.alarm.description,
      );
      if (content) {
        marker.layer.getPopup()?.setContent(content);
      }
      marker.layer.getElement()?.classList.add('alarm-blink');
    } else {
      const content = this.mapDrawService.changePopUpContent(parseInt(marker.id), 'Normalized');
      if (content) {
        marker.layer.getPopup()?.setContent(content);
      }
      marker.layer.getElement()?.classList.remove('alarm-blink');
    }
  }
});

我有这个测试:

describe('updateAlarmWithTag', () => {
it('should call classList.add when alarm is true', () => {
  const tagInfo: TagInfo = {
    device: '1234568',
    tag: '1_AI_0001',
    type: 'testtype',
  };
  const range: Range = {
    max: 10,
    min: 5,
  };
  const alarm: Alarm = {
    description: 'Chuva Forte',
    range: range,
  };
  const alarmData: AlarmData = {
    alarm: alarm,
    tagInfo: tagInfo,
    value: 7.12398343,
    ts: new Date(),
  };
  const marker = {
    tag: '1_AI_0001',
    layer: {
      getElement: () => ({ classList: { add: jasmine.createSpy() } }),
      getPopup: () => ({ getPopup: { setContent: jasmine.createSpy() } }),
    } as unknown as L.Marker,
  } as unknown as MarkerLayerViewInfo;

  component.geometricLayers.markers = [marker];
  component.updateAlarmWithTag(alarmData);
  expect(component.geometricLayers.markers[0].layer?.getElement()?.classList.add).toHaveBeenCalledWith;
  expect(component.geometricLayers.markers[0].layer?.getPopup()?.setContent).toHaveBeenCalled;
});

it('should call classList.remove when alarm is false', () => {
  const tagInfo: TagInfo = {
    device: '1234568',
    tag: '1_AI_0001',
    type: 'testtype',
  };
  const alarmData: AlarmData = {
    tagInfo: tagInfo,
    value: 7.12398343,
    ts: new Date(),
  };
  const marker = {
    tag: '1_AI_0001',
    layer: {
      getElement: () => ({ classList: { remove: jasmine.createSpy() } }),
      getPopup: () => ({ getPopup: () => ({ setContent: jasmine.createSpy() }) }),
    } as unknown as L.Marker,
  } as unknown as MarkerLayerViewInfo;

  component.geometricLayers.markers = [marker];
  component.updateAlarmWithTag(alarmData);
  expect(component.geometricLayers.markers[0].layer?.getElement()?.classList.remove).toHaveBeenCalledWith;
  expect(component.geometricLayers.markers[0].layer?.getPopup()?.setContent).toHaveBeenCalled;
});
it('should not update marker if layer element is null', () => {
  const tagInfo: TagInfo = {
    device: '1234568',
    tag: '1_AI_0001',
    type: 'testtype',
  };
  const alarm: Alarm = {
    description: 'Chuva Forte',
    range: { min: 0, max: 100 },
  };
  const alarmData: AlarmData = {
    alarm: alarm,
    tagInfo: tagInfo,
    value: 50,
    ts: new Date(),
  };
  const marker = {
    tags: { lat: '1_AI_0001', lng: '1_AI_0001' },
    latLng: [{ lat: 19, lng: 44 }],
    layer: {
      getElement: () => null,
      getPopup: () => ({ getPopup: () => ({ setContent: jasmine.createSpy() }) }),
    } as unknown as L.Marker,
  } as unknown as MarkerLayerViewInfo;
  component.geometricLayers.markers = [marker];
  component.updateAlarmWithTag(alarmData);
  expect(component.geometricLayers.markers[0].layer?.getElement()?.classList.add).not.toHaveBeenCalled;
  expect(component.geometricLayers.markers[0].layer?.getElement()?.classList.remove).not.toHaveBeenCalled;
  expect(component.geometricLayers.markers[0].layer?.getPopup()?.setContent).toHaveBeenCalled;
});

如何在我的 updateAlarmWithTag 函数中实现包含 marker.layer.getPopup()?.setContent(content) 的行的测试覆盖?我目前的测试没有涵盖这些行,我正在寻找解决这一覆盖差距的方法。解决这个问题的最佳方法是什么?

angular typescript unit-testing jasmine
© www.soinside.com 2019 - 2024. All rights reserved.