Skip to content

Commit

Permalink
[Angular] Improve coverage on MetricsComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
qmonmert committed Oct 1, 2023
1 parent 1c53df4 commit 3089407
Showing 1 changed file with 108 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@
See the License for the specific language governing permissions and
limitations under the License.
-%>
import { ChangeDetectorRef } from '@angular/core';
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { of } from 'rxjs';

import MetricsComponent from './metrics.component';
import { MetricsService } from './metrics.service';
import { Metrics } from './metrics.model';
import { Metrics, Thread, ThreadDump } from './metrics.model';

describe('MetricsComponent', () => {
let comp: MetricsComponent;
let fixture: ComponentFixture<MetricsComponent>;
let service: MetricsService;
let changeDetector: ChangeDetectorRef;

beforeEach(
waitForAsync(() => {
Expand All @@ -43,18 +45,122 @@ describe('MetricsComponent', () => {
fixture = TestBed.createComponent(MetricsComponent);
comp = fixture.componentInstance;
service = TestBed.inject(MetricsService);
changeDetector = fixture.debugElement.injector.get(ChangeDetectorRef);
});

describe('refresh', () => {
it('should call refresh on init', () => {
// GIVEN
jest.spyOn(service, 'getMetrics').mockReturnValue(of({} as Metrics));
const metrics = {
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
collectionTime: 0,
},
'PS MarkSweep': {
collectionCount: 0,
collectionTime: 0,
},
},
} as unknown as Metrics;
const threadDump = { threads: [ { threadName: 'thread 1' } as Thread] } as ThreadDump;

jest.spyOn(service, 'getMetrics').mockReturnValue(of(metrics));
jest.spyOn(service, 'threadDump').mockReturnValue(of(threadDump));
jest.spyOn(changeDetector.constructor.prototype, 'markForCheck');

// WHEN
comp.ngOnInit();

// THEN
expect(service.getMetrics).toHaveBeenCalled();
expect(comp.metrics).toEqual(metrics);
expect(comp.threads).toEqual(threadDump.threads);
expect(comp.updatingMetrics).toBeFalsy();
expect(changeDetector.constructor.prototype.markForCheck).toHaveBeenCalled();
});
});

describe('metricsKeyExists', () => {
it('should check that metrics key exists', () => {
// GIVEN
comp.metrics = {
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
collectionTime: 0,
},
'PS MarkSweep': {
collectionCount: 0,
collectionTime: 0,
},
},
} as unknown as Metrics;

// WHEN
const garbageCollectorKeyExists = comp.metricsKeyExists('garbageCollector');

// THEN
expect(garbageCollectorKeyExists).toBeTruthy();
});

it('should check that metrics key does not exist', () => {
// GIVEN
comp.metrics = {
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
collectionTime: 0,
},
'PS MarkSweep': {
collectionCount: 0,
collectionTime: 0,
},
},
} as unknown as Metrics;

// WHEN
const databasesCollectorKeyExists = comp.metricsKeyExists('databases');

// THEN
expect(databasesCollectorKeyExists).toBeFalsy();
});
});

describe('metricsKeyExistsAndObjectNotEmpty', () => {
it('should check that metrics key exists and is not empty', () => {
// GIVEN
comp.metrics = {
garbageCollector: {
'PS Scavenge': {
collectionCount: 0,
collectionTime: 0,
},
'PS MarkSweep': {
collectionCount: 0,
collectionTime: 0,
},
},
} as unknown as Metrics;

// WHEN
const garbageCollectorKeyExistsAndNotEmpty = comp.metricsKeyExistsAndObjectNotEmpty('garbageCollector');

// THEN
expect(garbageCollectorKeyExistsAndNotEmpty).toBeTruthy();
});

it('should check that metrics key is empty', () => {
// GIVEN
comp.metrics = {
garbageCollector: {},
} as Metrics;

// WHEN
const garbageCollectorKeyEmpty = comp.metricsKeyExistsAndObjectNotEmpty('garbageCollector');

// THEN
expect(garbageCollectorKeyEmpty).toBeFalsy();
});
});
});

0 comments on commit 3089407

Please sign in to comment.