<commit_before>import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChartSummarizationComponent } from './chart-summarization.component';
import { LineChartComponent } from '../line-chart/line-chart.component';
import { SimpleChange } from '@angular/core';
import { ScreenReaderModule } from '../screen-reader/screen-reader.module';
import { MatCardModule } from '@angular/material/card';
import { createLineChartMeta } from '../../datasets/metas/line-chart.meta';
import { mockData } from '../../utils/mocks.spec';

describe('ChartSummarizationComponent', () => {
  let fixture: ComponentFixture<ChartSummarizationComponent>;
  let component: ChartSummarizationComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        MatCardModule,
      ],
      declarations: [
        LineChartComponent,
      ],
    });
    const hostFixture = TestBed.createComponent(LineChartComponent);
    const host = hostFixture.componentInstance;
    host.meta = createLineChartMeta(
      'Title',
      () => mockData,
    );
    TestBed.resetTestingModule();

    TestBed.configureTestingModule({
      imports: [
        ScreenReaderModule,
      ],
      providers: [
        { provide: 'host', useValue: host },
      ],
      declarations: [
        ChartSummarizationComponent,
      ],
    });
    fixture = TestBed.createComponent(ChartSummarizationComponent);

    // init component inputs
    component = fixture.componentInstance;
    component.enabled = true;
    component.validityThreshold = 0.5;

    host.ngOnInit();
    host.ngOnChanges({
      meta: new SimpleChange(null, host.meta, true),
    });
    component.ngOnInit();
  });

  it('should instantiate.', () => {
    expect(component).toBeInstanceOf(ChartSummarizationComponent);
  });

  it('should have summaries.', () => {
    expect(component.hasSummaries).toBeTrue();
  });

  it('should have summaries with validity greater than or equal to threshold.', () => {
    for (const summary of component.summaries) {
      expect(summary.validity).toBeGreaterThanOrEqual(component.validityThreshold);
    }
  });

  it('should sort summaries by validity in descending order.', () => {
    const summaries = component.summaries;
    for (let i = 1; i < summaries.length; i++) {
      const currentValidity = summaries[i].validity;
      const previousValidity = summaries[i - 1].validity;
      expect(currentValidity).toBeLessThanOrEqual(previousValidity);
    }
  });
});<commit_msg>Add DOM tests for ChartSummarizationComponent<commit_after>import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ChartSummarizationComponent } from './chart-summarization.component';
import { LineChartComponent } from '../line-chart/line-chart.component';
import { SimpleChange } from '@angular/core';
import { ScreenReaderModule } from '../screen-reader/screen-reader.module';
import { MatCardModule } from '@angular/material/card';
import { createLineChartMeta } from '../../datasets/metas/line-chart.meta';

describe('ChartSummarizationComponent', () => {
  const nMockSummaries = 10;
  const mockSummaries = [...Array(nMockSummaries).keys()].map(i => ({
    text: `This is summary ${i}.`,
    validity: i / nMockSummaries,
  }));
  const mockData = [{
    label: `MockDatum`,
    points: [],
    querySummaries: () => mockSummaries,
  }];
  const validityThreshold = 0.5;
  let fixture: ComponentFixture<ChartSummarizationComponent>;
  let component: ChartSummarizationComponent;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [
        MatCardModule,
      ],
      declarations: [
        LineChartComponent,
      ],
    });
    const hostFixture = TestBed.createComponent(LineChartComponent);
    const host = hostFixture.componentInstance;
    host.meta = createLineChartMeta(
      'Title',
      () => mockData,
    );
    TestBed.resetTestingModule();

    TestBed.configureTestingModule({
      imports: [
        ScreenReaderModule,
      ],
      providers: [
        { provide: 'host', useValue: host },
      ],
      declarations: [
        ChartSummarizationComponent,
      ],
    });
    fixture = TestBed.createComponent(ChartSummarizationComponent);

    // init component inputs
    component = fixture.componentInstance;
    component.enabled = true;
    component.validityThreshold = validityThreshold;

    host.ngOnInit();
    host.ngOnChanges({
      meta: new SimpleChange(null, host.meta, true),
    });
    component.ngOnInit();
    fixture.detectChanges();
  });

  it('should instantiate.', () => {
    expect(component).toBeInstanceOf(ChartSummarizationComponent);
  });

  it('should have summaries.', () => {
    expect(component.hasSummaries).toBeTrue();
  });

  it('should have summaries with validity greater than or equal to threshold.', () => {
    for (const summary of component.summaries) {
      expect(summary.validity).toBeGreaterThanOrEqual(component.validityThreshold);
    }
  });

  it('should sort summaries by validity in descending order.', () => {
    const summaries = component.summaries;
    for (let i = 1; i < summaries.length; i++) {
      const currentValidity = summaries[i].validity;
      const previousValidity = summaries[i - 1].validity;
      expect(currentValidity).toBeLessThanOrEqual(previousValidity);
    }
  });

  it('should render summaries with validity greater than or equal to threshold.', () => {
    const summarizationElement: HTMLElement = fixture.nativeElement;
    const psummaries = Array.from(summarizationElement.querySelectorAll('p.summary'));
    for (const summary of mockSummaries){
      if (summary.validity >= validityThreshold) {
        expect(psummaries.some(psummary => psummary.textContent?.includes(summary.text))).toBeTrue();
      }
    }
  });

  it('should not render summaries with validity less than threshold.', () => {
    const summarizationElement: HTMLElement = fixture.nativeElement;
    const psummaries = Array.from(summarizationElement.querySelectorAll('p.summary'));
    for (const summary of mockSummaries){
      if (summary.validity < validityThreshold) {
        expect(psummaries.every(psummary => !psummary.textContent?.includes(summary.text))).toBeTrue();
      }
    }
  });
});
{'fraction_non_alphanumeric': 0.13125499600319745, 'fraction_numerical': 0.0015987210231814548, 'mean_word_length': 3.5564457392571014, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 26, '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': '1090577', 'n_tokens_mistral': 1787, 'n_tokens_neox': 1750, 'n_words': 351}