Skip to content

Commit

Permalink
chore(misc): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescoBorzi committed Jun 16, 2020
1 parent 158119b commit 76a6a3b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 37 deletions.
@@ -1,7 +1,6 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { of } from 'rxjs';
import Spy = jasmine.Spy;

import { ReferenceLootTemplateComponent } from './reference-loot-template.component';
import { ReferenceLootTemplateModule } from './reference-loot-template.module';
Expand All @@ -13,13 +12,6 @@ import { ReferenceLootHandlerService } from './reference-loot-handler.service';
class ReferenceLootTemplatePage extends MultiRowEditorPageObject<ReferenceLootTemplateComponent> {}

describe('ReferenceLootTemplate integration tests', () => {
let component: ReferenceLootTemplateComponent;
let fixture: ComponentFixture<ReferenceLootTemplateComponent>;
let queryService: MysqlQueryService;
let querySpy: Spy;
let handlerService: ReferenceLootHandlerService;
let page: ReferenceLootTemplatePage;

const id = 1234;

const originalRow0 = new ReferenceLootTemplate();
Expand All @@ -44,29 +36,30 @@ describe('ReferenceLootTemplate integration tests', () => {
}));

function setup(creatingNew: boolean) {
handlerService = TestBed.inject(ReferenceLootHandlerService);
const handlerService = TestBed.inject(ReferenceLootHandlerService);
handlerService['_selected'] = `${id}`;
handlerService.isNew = creatingNew;

queryService = TestBed.inject(MysqlQueryService);
querySpy = spyOn(queryService, 'query').and.returnValue(of());
const queryService = TestBed.inject(MysqlQueryService);
const querySpy = spyOn(queryService, 'query').and.returnValue(of());
spyOn(queryService, 'queryValue').and.returnValue(of());

spyOn(queryService, 'selectAll').and.returnValue(of(
creatingNew ? [] : [originalRow0, originalRow1, originalRow2]
));

fixture = TestBed.createComponent(ReferenceLootTemplateComponent);
component = fixture.componentInstance;
page = new ReferenceLootTemplatePage(fixture);
const fixture = TestBed.createComponent(ReferenceLootTemplateComponent);
const page = new ReferenceLootTemplatePage(fixture);
fixture.autoDetectChanges(true);
fixture.detectChanges();

return { page, querySpy, handlerService };
}

describe('Creating new', () => {
beforeEach(() => setup(true));

it('should correctly initialise', () => {
const { page } = setup(true);
page.expectDiffQueryToBeEmpty();
page.expectFullQueryToBeEmpty();
expect(page.formError.hidden).toBe(true);
Expand All @@ -85,6 +78,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('should correctly update the unsaved status', () => {
const { page, handlerService } = setup(true);
expect(handlerService.isUnsaved).toBe(false);
page.addNewRow();
expect(handlerService.isUnsaved).toBe(true);
Expand All @@ -93,6 +87,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('adding new rows and executing the query should correctly work', () => {
const { page, querySpy } = setup(true);
const expectedQuery = 'DELETE FROM `reference_loot_template` WHERE (`Entry` = 1234) AND (`Item` IN (0, 1, 2));\n' +
'INSERT INTO `reference_loot_template` (`Entry`, `Item`, `Reference`, `Chance`, `QuestRequired`, `LootMode`, `GroupId`, ' +
'`MinCount`, `MaxCount`, `Comment`) VALUES\n' +
Expand All @@ -115,6 +110,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('adding a row and changing its values should correctly update the queries', () => {
const { page } = setup(true);
page.addNewRow();
page.expectDiffQueryToContain(
'DELETE FROM `reference_loot_template` WHERE (`Entry` = 1234) AND (`Item` IN (0));\n' +
Expand Down Expand Up @@ -174,9 +170,9 @@ describe('ReferenceLootTemplate integration tests', () => {
});

describe('Editing existing', () => {
beforeEach(() => setup(false));

it('should correctly initialise', () => {
const { page } = setup(false);
expect(page.formError.hidden).toBe(true);
page.expectDiffQueryToBeShown();
page.expectDiffQueryToBeEmpty();
Expand All @@ -191,6 +187,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('deleting rows should correctly work', () => {
const { page } = setup(false);
page.deleteRow(1);
expect(page.getEditorTableRowsCount()).toBe(2);
page.expectDiffQueryToContain(
Expand Down Expand Up @@ -225,6 +222,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('editing existing rows should correctly work', () => {
const { page } = setup(false);
page.clickRowOfDatatable(1);
page.setInputValueById('LootMode', 1);

Expand All @@ -248,6 +246,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('combining add, edit and delete should correctly work', () => {
const { page } = setup(false);
page.addNewRow();
expect(page.getEditorTableRowsCount()).toBe(4);

Expand Down Expand Up @@ -276,6 +275,7 @@ describe('ReferenceLootTemplate integration tests', () => {
});

it('using the same row id for multiple rows should correctly show an error', () => {
const { page } = setup(false);
page.clickRowOfDatatable(2);
page.setInputValueById('Item', 0);

Expand Down
@@ -1,8 +1,7 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { async, TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
import { of } from 'rxjs';
import Spy = jasmine.Spy;

import { MysqlQueryService } from '@keira-shared/services/mysql-query.service';
import { SelectReferenceLootComponent } from './select-reference-loot.component';
Expand All @@ -17,13 +16,6 @@ class SelectReferenceLootComponentPage extends SelectPageObject<SelectReferenceL
}

describe('SelectReferenceLoot integration tests', () => {
let component: SelectReferenceLootComponent;
let fixture: ComponentFixture<SelectReferenceLootComponent>;
let selectService: SelectReferenceLootService;
let page: SelectReferenceLootComponentPage;
let queryService: MysqlQueryService;
let querySpy: Spy;
let navigateSpy: Spy;

const value = 1200;

Expand All @@ -40,23 +32,27 @@ describe('SelectReferenceLoot integration tests', () => {
.compileComponents();
}));

beforeEach(() => {
navigateSpy = spyOn(TestBed.inject(Router), 'navigate');
queryService = TestBed.inject(MysqlQueryService);
querySpy = spyOn(queryService, 'query').and.returnValue(of(
[{ max: 1 }]
function setup() {
const navigateSpy = spyOn(TestBed.inject(Router), 'navigate');
const queryService = TestBed.inject(MysqlQueryService);
const querySpy = spyOn(queryService, 'query').and.returnValue(of(
[{max: 1}]
));

selectService = TestBed.inject(SelectReferenceLootService);
const selectService = TestBed.inject(SelectReferenceLootService);

fixture = TestBed.createComponent(SelectReferenceLootComponent);
page = new SelectReferenceLootComponentPage(fixture);
component = fixture.componentInstance;
const fixture = TestBed.createComponent(SelectReferenceLootComponent);
const page = new SelectReferenceLootComponentPage(fixture);
const component = fixture.componentInstance;
fixture.autoDetectChanges(true);
fixture.detectChanges();
});

return { component, fixture, selectService, page, queryService, querySpy, navigateSpy };
}

it('should correctly initialise', async () => {
const { fixture, page, querySpy, component } = setup();

await fixture.whenStable();
expect(page.createInput.value).toEqual(`${component.customStartingId}`);
page.expectNewEntityFree();
Expand All @@ -69,6 +65,8 @@ describe('SelectReferenceLoot integration tests', () => {
});

it('should correctly behave when inserting and selecting free entry', async () => {
const { fixture, page, querySpy, navigateSpy } = setup();

await fixture.whenStable();
querySpy.calls.reset();
querySpy.and.returnValue(of([]));
Expand All @@ -90,9 +88,11 @@ describe('SelectReferenceLoot integration tests', () => {
});

it('should correctly behave when inserting an existing entity', async () => {
const { fixture, page, querySpy } = setup();

await fixture.whenStable();
querySpy.calls.reset();
querySpy.and.returnValue(of(['mock value']));
querySpy.and.returnValue(of([{}]));

page.setInputValue(page.createInput, value);

Expand All @@ -111,6 +111,8 @@ describe('SelectReferenceLoot integration tests', () => {
},
]) {
it(`searching an existing entity should correctly work [${id}]`, () => {
const { page, querySpy } = setup();

querySpy.calls.reset();
if (entry) {
page.setInputValue(page.searchIdInput, entry);
Expand All @@ -127,6 +129,8 @@ describe('SelectReferenceLoot integration tests', () => {
}

it('searching and selecting an existing entity from the datatable should correctly work', () => {
const { navigateSpy, page, querySpy } = setup();

const results: Partial<ReferenceLootTemplate>[] = [
{ Entry: 1 },
{ Entry: 2 },
Expand Down

0 comments on commit 76a6a3b

Please sign in to comment.