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

import { By } from '@angular/platform-browser';
import {
  Component,
  DebugElement,
  Input
} from '@angular/core';

import { createMock } from '../../../../testing/mock';

import { Observable } from 'rxjs';

import { CollapseModule } from 'ngx-bootstrap/collapse';

import { DeploymentsComponent } from './deployments.component';
import { DeploymentsService } from './services/deployments.service';
import { CpuStat } from './models/cpu-stat';
import { Environment } from './models/environment';
import { MemoryStat } from './models/memory-stat';
import { Stat } from './models/stat';

import { Spaces } from 'ngx-fabric8-wit';

@Component({
  selector: 'deployments-resource-usage',
  template: ''
})

class FakeDeploymentsResourceUsageComponent {
  @Input() environments: Observable<Environment[]>;
}

@Component({
  selector: 'deployments-apps',
  template: ''
})
class FakeDeploymentAppsComponent {
  @Input() spaceId: Observable<string>;
  @Input() environments: Observable<Environment[]>;
  @Input() applications: Observable<string[]>;
}

describe('DeploymentsComponent', () => {

  let component: DeploymentsComponent;
  let fixture: ComponentFixture<DeploymentsComponent>;
  let mockSvc: jasmine.SpyObj<DeploymentsService>;
  let spaces: Spaces;
  let mockApplications = Observable.of(['foo-app', 'bar-app']);
  let mockEnvironments = Observable.of([
    { environmentId: 'a1', name: 'stage' },
    { environmentId: 'b2', name: 'prod' }
  ]);

  beforeEach(() => {
    mockSvc = createMock(DeploymentsService);
    mockSvc.getApplications.and.returnValue(mockApplications);
    mockSvc.getEnvironments.and.returnValue(mockEnvironments);

    spaces = {
      current: Observable.of({ id: 'fake-spaceId' })
    } as Spaces;

    TestBed.configureTestingModule({
      imports: [ CollapseModule.forRoot() ],
      declarations: [
        DeploymentsComponent,
        FakeDeploymentAppsComponent,
        FakeDeploymentsResourceUsageComponent
      ],
      providers: [
        { provide: DeploymentsService, useValue: mockSvc },
        { provide: Spaces, useValue: spaces }
      ]
    });

    fixture = TestBed.createComponent(DeploymentsComponent);
    component = fixture.componentInstance;

    fixture.detectChanges();
  });

  it('should set service result to applications property', (done: DoneFn) => {
    expect(mockSvc.getApplications).toHaveBeenCalledWith('fake-spaceId');
    component.applications.subscribe(applications => {
      expect(applications).toEqual(['foo-app', 'bar-app']);
      done();
    });
  });

  it('should set service result to environments property', (done: DoneFn) => {
    expect(mockSvc.getEnvironments).toHaveBeenCalledWith('fake-spaceId');
    component.environments.subscribe(environments => {
      expect(environments).toEqual([
        { environmentId: 'a1', name: 'stage' },
        { environmentId: 'b2', name: 'prod' }
      ]);
      done();
    });
  });

  it('should pass values to children resource usage components', () => {
    let resourceUsageComponents = fixture.debugElement.queryAll(By.directive(FakeDeploymentsResourceUsageComponent));
    expect(resourceUsageComponents.length).toEqual(1);
    let resourceUsageComponent = resourceUsageComponents[0].componentInstance;
    expect(resourceUsageComponent.environments).toEqual(mockEnvironments);

    let appsComponents = fixture.debugElement.queryAll(By.directive(FakeDeploymentAppsComponent));
    expect(appsComponents.length).toEqual(1);
    let appsComponent = appsComponents[0].componentInstance;
    expect(appsComponent.environments).toEqual(mockEnvironments);
    expect(appsComponent.applications).toEqual(mockApplications);
  });

});<commit_msg>test(deployments): use test-context for DeploymentsComponent spec<commit_after>import { ComponentFixture } from '@angular/core/testing';

import { By } from '@angular/platform-browser';
import {
  Component,
  Input
} from '@angular/core';

import { createMock } from '../../../../testing/mock';

import { Observable } from 'rxjs';

import {
  initContext,
  TestContext
} from '../../../../testing/test-context';

import { CollapseModule } from 'ngx-bootstrap/collapse';

import { DeploymentsComponent } from './deployments.component';
import { DeploymentsService } from './services/deployments.service';
import { Environment } from './models/environment';

import { Spaces } from 'ngx-fabric8-wit';

@Component({
  selector: 'deployments-resource-usage',
  template: ''
})

class FakeDeploymentsResourceUsageComponent {
  @Input() environments: Observable<Environment[]>;
}

@Component({
  selector: 'deployments-apps',
  template: ''
})
class FakeDeploymentAppsComponent {
  @Input() spaceId: Observable<string>;
  @Input() environments: Observable<Environment[]>;
  @Input() applications: Observable<string[]>;
}

@Component({
  template: '<alm-apps></alm-apps>'
})
class HostComponent { }

describe('DeploymentsComponent', () => {
  type Context = TestContext<DeploymentsComponent, HostComponent>;

  let component: DeploymentsComponent;
  let fixture: ComponentFixture<DeploymentsComponent>;

  let mockSvc: jasmine.SpyObj<DeploymentsService>;
  let spaces = { current: Observable.of({ id: 'fake-spaceId' }) };
  let mockApplications = Observable.of(['foo-app', 'bar-app']);
  let mockEnvironments = Observable.of([
    { environmentId: 'a1', name: 'stage' },
    { environmentId: 'b2', name: 'prod' }
  ]);

  beforeAll(() => {
    mockSvc = createMock(DeploymentsService);
    mockSvc.getApplications.and.returnValue(mockApplications);
    mockSvc.getEnvironments.and.returnValue(mockEnvironments);
  });

  initContext(DeploymentsComponent, HostComponent, {
    imports: [CollapseModule.forRoot()],
    declarations: [
      FakeDeploymentAppsComponent,
      FakeDeploymentsResourceUsageComponent
    ],
    providers: [
      { provide: DeploymentsService, useFactory: () => mockSvc },
      { provide: Spaces, useValue: spaces }
    ]
  });

  it('should set service result to applications property', function (this: Context, done: DoneFn) {
    expect(mockSvc.getApplications).toHaveBeenCalledWith('fake-spaceId');
    this.testedDirective.applications.subscribe(applications => {
      expect(applications).toEqual(['foo-app', 'bar-app']);
      done();
    });
  });

  it('should set service result to environments property', function (this: Context, done: DoneFn) {
    expect(mockSvc.getEnvironments).toHaveBeenCalledWith('fake-spaceId');
    this.testedDirective.environments.subscribe(environments => {
      expect(environments).toEqual([
        { environmentId: 'a1', name: 'stage' },
        { environmentId: 'b2', name: 'prod' }
      ]);
      done();
    });
  });

  it('should pass values to children resource usage components', function (this: Context) {
    let resourceUsageComponents = this.tested.queryAll(By.directive(FakeDeploymentsResourceUsageComponent));
    expect(resourceUsageComponents.length).toEqual(1);
    let resourceUsageComponent = resourceUsageComponents[0].componentInstance;
    expect(resourceUsageComponent.environments).toBe(mockEnvironments);

    let appsComponents = this.tested.queryAll(By.directive(FakeDeploymentAppsComponent));
    expect(appsComponents.length).toEqual(1);
    let appsComponent = appsComponents[0].componentInstance;
    expect(appsComponent.environments).toBe(mockEnvironments);
    expect(appsComponent.applications).toBe(mockApplications);
  });

});
{'fraction_non_alphanumeric': 0.13931475803226237, 'fraction_numerical': 0.0023996800426609784, 'mean_word_length': 4.606875934230194, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 30, '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': '6959841', 'n_tokens_mistral': 2172, 'n_tokens_neox': 2125, 'n_words': 405}