artifact-summary.component.spec.ts 3.05 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ArtifactSummaryComponent } from "./artifact-summary.component";
import { of } from "rxjs";
import { ClarityModule } from "@clr/angular";
import { NO_ERRORS_SCHEMA } from "@angular/core";
import { Artifact } from "../../../../../ng-swagger-gen/models/artifact";
import { ProjectService } from "../../../../lib/services";
import { ArtifactService } from "../../../../../ng-swagger-gen/services/artifact.service";
import { ErrorHandler } from "../../../../lib/utils/error-handler";
import { TranslateFakeLoader, TranslateLoader, TranslateModule } from "@ngx-translate/core";
import { ActivatedRoute, Router } from "@angular/router";
import { AppConfigService } from "../../../app-config.service";

describe('ArtifactSummaryComponent', () => {

  const mockedArtifact: Artifact = {
    id: 123,
    type: 'IMAGE'
  };

  const fakedProjectService = {
    getProject() {
      return of({name: 'test'});
    }
  };

  const fakedArtifactService = {
    getArtifact() {
       return of(mockedArtifact);
    }
  };
  let component: ArtifactSummaryComponent;
  let fixture: ComponentFixture<ArtifactSummaryComponent>;
  const mockActivatedRoute = {
    RouterparamMap: of({ get: (key) => 'value' }),
    snapshot: {
      params: {
        id: 1,
        repo: "test",
        digest: "ABC",
        subscribe: () => {
          return of(null);
        }
      },
      data: {
        projectResolver: {
          has_project_admin_role: true,
          current_user_role_id: 3,
          name: "demo"
        }
      }
    },
    data: of({
      projectResolver: {
        ismember: true,
        role_name: 'master',
      }
    })
  };
  const fakedAppConfigService = {
    getConfig: () => {
      return {with_admiral: false};
    }
  };
  const mockRouter = {
    navigate: () => { }
  };
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [
        ClarityModule,
        TranslateModule.forRoot({
          loader: {
            provide: TranslateLoader,
            useClass: TranslateFakeLoader,
          }
        })
      ],
      declarations: [
        ArtifactSummaryComponent
      ],
      schemas: [
        NO_ERRORS_SCHEMA
      ],
      providers: [
        { provide: AppConfigService, useValue: fakedAppConfigService },
        { provide: Router, useValue: mockRouter },
        { provide: ActivatedRoute, useValue: mockActivatedRoute },
        { provide: ProjectService, useValue: fakedProjectService },
        { provide: ArtifactService, useValue: fakedArtifactService },
        ErrorHandler
      ]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(ArtifactSummaryComponent);
    component = fixture.componentInstance;
    component.repositoryName = 'demo';
    component.artifactDigest = 'sha: acf4234f';
    fixture.detectChanges();
  });

  it('should create and get artifactDetails', async () => {
    expect(component).toBeTruthy();
    await fixture.whenStable();
    expect(component.artifact.type).toEqual('IMAGE');
  });
});