diff --git a/src/app/features/other-loots/reference-loot/reference-loot-template.integration.spec.ts b/src/app/features/other-loots/reference-loot/reference-loot-template.integration.spec.ts index 61f34d5e72..0fe9c64e75 100644 --- a/src/app/features/other-loots/reference-loot/reference-loot-template.integration.spec.ts +++ b/src/app/features/other-loots/reference-loot/reference-loot-template.integration.spec.ts @@ -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'; @@ -13,13 +12,6 @@ import { ReferenceLootHandlerService } from './reference-loot-handler.service'; class ReferenceLootTemplatePage extends MultiRowEditorPageObject {} describe('ReferenceLootTemplate integration tests', () => { - let component: ReferenceLootTemplateComponent; - let fixture: ComponentFixture; - let queryService: MysqlQueryService; - let querySpy: Spy; - let handlerService: ReferenceLootHandlerService; - let page: ReferenceLootTemplatePage; - const id = 1234; const originalRow0 = new ReferenceLootTemplate(); @@ -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); @@ -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); @@ -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' + @@ -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' + @@ -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(); @@ -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( @@ -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); @@ -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); @@ -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); diff --git a/src/app/features/other-loots/reference-loot/select-reference-loot.integration.spec.ts b/src/app/features/other-loots/reference-loot/select-reference-loot.integration.spec.ts index e325198959..83b9eb95ff 100644 --- a/src/app/features/other-loots/reference-loot/select-reference-loot.integration.spec.ts +++ b/src/app/features/other-loots/reference-loot/select-reference-loot.integration.spec.ts @@ -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'; @@ -17,13 +16,6 @@ class SelectReferenceLootComponentPage extends SelectPageObject { - let component: SelectReferenceLootComponent; - let fixture: ComponentFixture; - let selectService: SelectReferenceLootService; - let page: SelectReferenceLootComponentPage; - let queryService: MysqlQueryService; - let querySpy: Spy; - let navigateSpy: Spy; const value = 1200; @@ -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(); @@ -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([])); @@ -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); @@ -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); @@ -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[] = [ { Entry: 1 }, { Entry: 2 },