<commit_before>import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import Spy = jasmine.Spy;
import { Observable } from 'rxjs/Observable';

import { BulkUploadComponent } from './bulk-upload.component';
import { Person } from '@lib/models';
import { PersonService } from '../person.service';
import { SharedModule } from '../shared/shared.module';

describe('BulkUploadComponent', () => {
  let component: BulkUploadComponent;
  let fixture: ComponentFixture<BulkUploadComponent>;
  let personServiceSpy: PersonService;

  beforeEach(async(() => {
    personServiceSpy = jasmine.createSpyObj('PersonService', ['addPerson']);
    (personServiceSpy.addPerson as Spy).and.returnValue(Observable.empty());

    TestBed.configureTestingModule({
      declarations: [BulkUploadComponent],
      imports: [SharedModule],
      providers: [{ provide: PersonService, useValue: personServiceSpy }],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(BulkUploadComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should invoke the upload method when the submit button is clicked', () => {
    spyOn(component, 'upload');
    component.bulkDataForm.setValue({ data: 'Charlie,123,C' });
    fixture.detectChanges();

    fixture.nativeElement.querySelector('button.is-success').click();

    expect(component.upload).toHaveBeenCalled();
  });

  describe('upload method', () => {
    it('should call the service with bulk upload data if confirmed', () => {
      spyOn(window, 'confirm').and.returnValue(true);
      component.bulkDataForm.setValue({ data: 'Charlie,123,C' });

      component.upload();

      expect(window.confirm).toHaveBeenCalled();
      expect(personServiceSpy.addPerson).toHaveBeenCalledWith(new Person('Charlie', 123, 'C'));
    });

    it('should not call the service with bulk upload data if not confirmed', () => {
      spyOn(window, 'confirm').and.returnValue(false);
      component.bulkDataForm.setValue({ data: 'Charlie,123,C' });

      component.upload();

      expect(window.confirm).toHaveBeenCalled();
      expect(personServiceSpy.addPerson).not.toHaveBeenCalled();
    });

    it('should not allow an empty form to be submitted', () => {
      spyOn(window, 'confirm').and.returnValue(false);
      component.bulkDataForm.setValue({ data: '' });

      component.upload();

      expect(window.confirm).not.toHaveBeenCalled();
      expect(fixture.nativeElement.querySelector('.is-success:disabled'))
          .not.toBeNull('no disabled success buttons found');
    });
  });
});
<commit_msg>Refactor bulk upload tests<commit_after>import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';

import Spy = jasmine.Spy;
import { Observable } from 'rxjs/Observable';

import { BulkUploadComponent } from './bulk-upload.component';
import { Person } from '@lib/models';
import { PersonService } from '../person.service';

describe('BulkUploadComponent', () => {
  let component: BulkUploadComponent;
  let fixture: ComponentFixture<BulkUploadComponent>;
  let personServiceSpy: PersonService;

  beforeEach(async(() => {
    personServiceSpy = jasmine.createSpyObj('PersonService', ['addPerson']);
    (personServiceSpy.addPerson as Spy).and.returnValue(Observable.empty());

    TestBed.configureTestingModule({
      declarations: [BulkUploadComponent],
      imports: [ReactiveFormsModule],
      providers: [{ provide: PersonService, useValue: personServiceSpy }],
    }).compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(BulkUploadComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  describe('data upload', () => {
    it('should call the service with bulk upload data if confirmed', () => {
      spyOn(window, 'confirm').and.returnValue(true);
      setFormData('Charlie,123,C');

      clickUploadButton();

      expect(window.confirm).toHaveBeenCalled();
      expect(personServiceSpy.addPerson).toHaveBeenCalledWith(new Person('Charlie', 123, 'C'));
    });

    it('should not call the service with bulk upload data if not confirmed', () => {
      spyOn(window, 'confirm').and.returnValue(false);
      setFormData('Charlie,123,C');

      clickUploadButton();

      expect(window.confirm).toHaveBeenCalled();
      expect(personServiceSpy.addPerson).not.toHaveBeenCalled();
    });

    it('should not allow an empty form to be submitted', () => {
      spyOn(window, 'confirm').and.returnValue(false);
      setFormData('');

      clickUploadButton();

      expect(window.confirm).not.toHaveBeenCalled();
      expect(fixture.nativeElement.querySelector('.is-success:disabled'))
          .not.toBeNull('no disabled success buttons found');
    });
  });

  function setFormData (data: string) {
    const textarea: HTMLTextAreaElement = fixture.nativeElement.querySelector('textarea');
    textarea.value = data;
    textarea.dispatchEvent(new Event('input'));
    return fixture.detectChanges();
  }

  function clickUploadButton() {
    return fixture.nativeElement.querySelector('button.is-success').click();
  }
});

{'fraction_non_alphanumeric': 0.14676616915422885, 'fraction_numerical': 0.004018369690011481, 'mean_word_length': 4.306598984771574, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 20, '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': '3827126', 'n_tokens_mistral': 1502, 'n_tokens_neox': 1463, 'n_words': 289}