Skip to content

Commit

Permalink
test: more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi committed Jul 9, 2019
1 parent e303655 commit a15c787
Showing 1 changed file with 90 additions and 0 deletions.
@@ -0,0 +1,90 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { anything, instance, when } from 'ts-mockito';
import { of, throwError } from 'rxjs';
import Spy = jasmine.Spy;

import { CommonTestModule } from '../../../../test-utils/common-test.module';
import { MysqlService } from '../../../../services/mysql.service';
import { MockedMysqlService } from '../../../../test-utils/mocks';
import { CommonEditorTestModule } from '../../../../test-utils/common-editor-test-module';
import { LootTemplateComponent } from './loot-template.component';
import { CreatureLootTemplate } from '../../../../types/creature-loot-template.type';
import { CreatureLootTemplateComponent } from '../../creature/creature-loot-template/creature-loot-template.component';
import { CreatureLootTemplateService } from '../../../../services/editors/creature/creature-loot-template.service';
import { LootEditorService } from '../../../../services/editors/loot-editor.service';

describe('LootTemplateComponent', () => {
let component: LootTemplateComponent<CreatureLootTemplate>;
let fixture: ComponentFixture<CreatureLootTemplateComponent>;
let editorService: LootEditorService<CreatureLootTemplate>;
let getLootIdSpy: Spy;
let reloadSpy: Spy;

const lootId = 1230;

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [
CreatureLootTemplateComponent,
],
imports: [
CommonTestModule,
CommonEditorTestModule,
],
providers: [
{ provide : MysqlService, useValue: instance(MockedMysqlService) },
]
})
.compileComponents();
}));

beforeEach(() => {
when(MockedMysqlService.query(anything(), anything())).thenReturn(of());
editorService = TestBed.get(CreatureLootTemplateService);
reloadSpy = spyOn(editorService, 'reload');
getLootIdSpy = spyOn(editorService, 'getLootId');
getLootIdSpy.and.returnValue(of({ results: [ { lootId } ]}));

fixture = TestBed.createComponent(CreatureLootTemplateComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});

it('should correctly initialise', () => {
expect(getLootIdSpy).toHaveBeenCalledTimes(1);
expect(reloadSpy).toHaveBeenCalledTimes(1);
});

it('it should not reload if the lootId is 0', () => {
getLootIdSpy.calls.reset();
reloadSpy.calls.reset();
getLootIdSpy.and.returnValue(of({ results: [ { lootId: 0 } ]}));

component.ngOnInit();

expect(getLootIdSpy).toHaveBeenCalledTimes(1);
expect(reloadSpy).toHaveBeenCalledTimes(0);
});

it('it should not reload if the same entity has already been loaded', () => {
getLootIdSpy.calls.reset();
reloadSpy.calls.reset();
editorService['_loadedEntityId'] = lootId;

component.ngOnInit();

expect(getLootIdSpy).toHaveBeenCalledTimes(1);
expect(reloadSpy).toHaveBeenCalledTimes(0);
});

it('should properly handle error', () => {
const errorSpy = spyOn(console, 'error');
const error = 'some error';
getLootIdSpy.and.returnValue(throwError(error));

component.ngOnInit();

expect(errorSpy).toHaveBeenCalledTimes(1);
expect(errorSpy).toHaveBeenCalledWith(error);
});
});

0 comments on commit a15c787

Please sign in to comment.