Skip to content

Commit

Permalink
Tighten up unit tests
Browse files Browse the repository at this point in the history
These all caused failures in the Angular 8 environment.

Signed-off-by: michael sorens <msorens@chef.io>
  • Loading branch information
msorens committed Aug 11, 2019
1 parent 72d5da9 commit 7bcd135
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { async, TestBed } from '@angular/core/testing';
import { ServerOrgFilterSidebarComponent } from './server-org-filter-sidebar.component';
import { async, TestBed, ComponentFixture } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { CommonModule } from '@angular/common';
import { StoreModule, Store } from '@ngrx/store';
import { NgrxStateAtom } from 'app/ngrx.reducers';
import * as sidebar from '../../services/sidebar/sidebar.reducer';
import * as sidebarActions from '../../services/sidebar/sidebar.actions';
import * as sidebarSelectors from '../../services/sidebar/sidebar.selectors';
import { ServerOrgFilterSidebarComponent } from './server-org-filter-sidebar.component';

describe('ServerOrgFilterSidebarComponent', () => {
let fixture, component;
let fixture: ComponentFixture<ServerOrgFilterSidebarComponent>;
let component: ServerOrgFilterSidebarComponent;
let ngrxStore: Store<NgrxStateAtom>;
let selectedChefServers = [];

beforeEach(async(() => {
TestBed.configureTestingModule({
Expand All @@ -30,12 +34,12 @@ describe('ServerOrgFilterSidebarComponent', () => {
beforeEach(() => {
fixture = TestBed.createComponent(ServerOrgFilterSidebarComponent);
component = fixture.componentInstance;
this.ngrxStore = TestBed.get(Store);
ngrxStore = TestBed.get(Store);
component.ngOnInit();
this.ngrxStore.dispatch(
ngrxStore.dispatch(
sidebarActions.getChefServersSuccess(['source1', 'source2', 'localhost']));

this.ngrxStore.dispatch(sidebarActions.getOrgsSuccess(['org1', 'org2']));
ngrxStore.dispatch(sidebarActions.getOrgsSuccess(['org1', 'org2']));
});

it('converts incoming localhost chef server to Local Mode', () => {
Expand All @@ -48,26 +52,26 @@ describe('ServerOrgFilterSidebarComponent', () => {
});

it('When filter is set with Local Mode, localhost is set instead', () => {
this.selectedChefServers = [];
this.ngrxStore.select(sidebarSelectors.selectedChefServers)
selectedChefServers = [];
ngrxStore.select(sidebarSelectors.selectedChefServers)
.subscribe(updatedSelectedChefServers =>
this.selectedChefServers = updatedSelectedChefServers
selectedChefServers = updatedSelectedChefServers
);

component.filter('servers', [component.localModeTag]);

expect(this.selectedChefServers).toEqual([component.localModeSource]);
expect(selectedChefServers).toEqual([component.localModeSource]);
});

it('When filter is set with Local Mode and other server name, ' +
'localhost is only swapped for Local Mode', () => {
this.selectedChefServers = [];
this.ngrxStore.select(sidebarSelectors.selectedChefServers)
selectedChefServers = [];
ngrxStore.select(sidebarSelectors.selectedChefServers)
.subscribe(updateSelectedChefServers =>
this.selectedChefServers = updateSelectedChefServers);
selectedChefServers = updateSelectedChefServers);

component.filter('servers', [component.localModeTag, 'www.chef.io']);

expect(this.selectedChefServers).toEqual([component.localModeSource, 'www.chef.io']);
expect(selectedChefServers).toEqual([component.localModeSource, 'www.chef.io']);
});
});
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { StoreModule, Store } from '@ngrx/store';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { RouterTestingModule } from '@angular/router/testing';
import { TestBed, fakeAsync, tick, ComponentFixture } from '@angular/core/testing';
import { Router } from '@angular/router';
import { StoreModule, Store } from '@ngrx/store';
import { ServiceGroupsComponent } from './service-groups.component';
import { ServiceStatusIconPipe } from '../../pipes/service-status-icon.pipe';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
import { NgrxStateAtom } from 'app/ngrx.reducers';
import { serviceGroupEntityReducer } from 'app/entities/service-groups/service-groups.reducer';
import {
UpdateServiceGroupFilters,
Expand All @@ -19,8 +20,10 @@ class MockTelemetryService {
}

describe('ServiceGroupsComponent', () => {
let fixture, component;
let fixture: ComponentFixture<ServiceGroupsComponent>;
let component: ServiceGroupsComponent;
let router: Router;
let ngrxStore: Store<NgrxStateAtom>;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -41,7 +44,7 @@ describe('ServiceGroupsComponent', () => {
fixture = TestBed.createComponent(ServiceGroupsComponent);
component = fixture.componentInstance;
router = TestBed.get(Router);
this.ngrxStore = TestBed.get(Store);
ngrxStore = TestBed.get(Store);
component.ngOnInit();
});

Expand All @@ -54,7 +57,7 @@ describe('ServiceGroupsComponent', () => {

describe('with ServiceGroupsCounts', () => {
beforeEach(() => {
this.ngrxStore.dispatch(new GetServiceGroupsCountsSuccess({
ngrxStore.dispatch(new GetServiceGroupsCountsSuccess({
total: 21,
ok: 10,
warning: 5,
Expand All @@ -69,7 +72,7 @@ describe('ServiceGroupsComponent', () => {

describe('and OK status filter update', () => {
beforeEach(() => {
this.ngrxStore.dispatch(new UpdateServiceGroupFilters({filters: {status: 'ok'}}));
ngrxStore.dispatch(new UpdateServiceGroupFilters({filters: {status: 'ok'}}));
});

it('should update the total number of service groups and selected status', fakeAsync(() => {
Expand Down Expand Up @@ -155,7 +158,13 @@ describe('ServiceGroupsComponent', () => {

describe('onPageChange', () => {
it('when the first page is selected remove page from URL', fakeAsync(() => {
component.sgHealthSummary = { total: 30, ok: 30 };
component.sgHealthSummary = {
total: 30,
ok: 30,
warning: 0,
critical: 0,
unknown: 0
};
router.navigate([''], {queryParams: { }});

tick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export class ServiceGroupsComponent implements OnInit, OnDestroy {
private selectedSortField$: Observable<string>;
private healthSummary$: Observable<HealthSummary>;
private currentPage$: Observable<number>;
private currentFieldDirection: SortDirection;
private currentSortField: string;
public currentFieldDirection: SortDirection;
public currentSortField: string;
private defaultFieldDirection: FieldDirection = {
name: 'ASC',
percent_ok: 'ASC',
Expand All @@ -78,8 +78,8 @@ export class ServiceGroupsComponent implements OnInit, OnDestroy {

constructor(
private route: ActivatedRoute,
private router: Router,
private store: Store<NgrxStateAtom>,
public router: Router,
public store: Store<NgrxStateAtom>,
private telemetryService: TelemetryService
) { }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,29 @@ class MockChefSessionService {
}

describe('UploadService', () => {
let service: UploadService;
let mockChefSessionService;

beforeEach(() => {
this.mockChefSessionService = new MockChefSessionService();
this.service = new UploadService(this.mockChefSessionService);
mockChefSessionService = new MockChefSessionService();
service = new UploadService(mockChefSessionService);
});
describe('getProgress', () => {
it('returns the correct object', () => {
expect(this.service.getProgress('file', 'percent', 'status', 'response'))
expect(service.getProgress('file', 25, 1, 'response'))
.toEqual({
'name': 'file',
'percent': 'percent',
'status': 'status',
'percent': 25,
'status': 1,
'response': 'response'
});
});
});

describe('estimateContentType', () => {
it('returns the expected type', () => {
expect(this.service.estimateContentType(null, 'file.tgz')).toEqual('application/gzip');
expect(this.service.estimateContentType(null, 'file.zip')).toEqual('application/zip');
expect(service.estimateContentType(null, 'file.tgz')).toEqual('application/gzip');
expect(service.estimateContentType(null, 'file.zip')).toEqual('application/zip');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class UploadService {
});
}

private getProgress(file: string, percent: number, status?: number, response?: string): any {
getProgress(file: string, percent: number, status?: number, response?: string): any {
return {
'name': file,
'percent': percent,
Expand All @@ -73,7 +73,7 @@ export class UploadService {
};
}

private estimateContentType(mime, name): string {
estimateContentType(mime, name): string {
switch (mime) {
case 'application/gzip':
case 'application/x-gzip':
Expand Down

0 comments on commit 7bcd135

Please sign in to comment.