From afbe6e5b36de2587bb2c8443e596a1be5cdbb1c6 Mon Sep 17 00:00:00 2001 From: dhilt Date: Sat, 9 Jun 2018 03:32:47 +0300 Subject: [PATCH] tslint update tests --- tests/adapter.spec.ts | 15 +++++++------ tests/bug.spec.ts | 5 +++-- tests/common.spec.ts | 37 ++++++++++++++++---------------- tests/datasource-get.spec.ts | 3 ++- tests/eof.spec.ts | 33 ++++++++++++++-------------- tests/initial-load.spec.ts | 13 +++++------ tests/miscellaneous/debounce.ts | 8 +++---- tests/miscellaneous/misc.ts | 25 +++++++++++++-------- tests/scaffolding/datasources.ts | 22 +++++++++---------- tests/scaffolding/runner.ts | 16 +++++++++----- tests/scaffolding/templates.ts | 4 ++-- tests/scaffolding/testBed.ts | 2 +- tests/scroll-basic.spec.ts | 25 ++++++++++----------- tests/scroll-fast.spec.ts | 23 ++++++++++---------- 14 files changed, 126 insertions(+), 105 deletions(-) diff --git a/tests/adapter.spec.ts b/tests/adapter.spec.ts index 5cc9ceb3..4f43bb5b 100644 --- a/tests/adapter.spec.ts +++ b/tests/adapter.spec.ts @@ -1,4 +1,5 @@ -import { makeTest } from './scaffolding/runner'; +import { makeTest, TestBedConfig } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; const customDefault = { startIndex: null, scrollCount: 0, preLoad: false }; @@ -51,7 +52,7 @@ const interruptConfigList = configList.map((config, i) => ({ } })); -const checkExpectation = (config, misc) => { +const checkExpectation = (config: TestBedConfig, misc: Misc) => { const startIndex = config.custom.startIndex === null ? config.datasourceSettings.startIndex : config.custom.startIndex; const bufferSize = config.datasourceSettings.bufferSize; @@ -59,12 +60,12 @@ const checkExpectation = (config, misc) => { const nextIndex = firstIndex + bufferSize + 1; const firstItem = misc.scroller.buffer.getFirstVisibleItem(); - expect(firstItem.$index).toEqual(firstIndex); + expect(firstItem ? firstItem.$index : null).toEqual(firstIndex); expect(misc.getElementText(firstIndex)).toEqual(`${firstIndex} : item #${firstIndex}`); expect(misc.getElementText(nextIndex)).toEqual(`${nextIndex} : item #${nextIndex}`); }; -const doReload = (config, misc) => { +const doReload = (config: TestBedConfig, misc: Misc) => { if (config.custom.startIndex !== null) { misc.datasource.adapter.reload(config.custom.startIndex); } else { @@ -72,7 +73,7 @@ const doReload = (config, misc) => { } }; -const shouldReload = (config) => (misc) => (done) => { +const shouldReload = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { const startWFCount = config.custom.preLoad ? 0 : 1; spyOn(misc.workflow, 'finalize').and.callFake(() => { if (misc.workflow.cyclesDone < startWFCount + config.custom.scrollCount) { @@ -94,7 +95,7 @@ const shouldReload = (config) => (misc) => (done) => { } }; -const shouldReloadBeforeLoad = (config) => (misc) => (done) => { +const shouldReloadBeforeLoad = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { spyOn(misc.workflow, 'finalize').and.callFake(() => { expect(misc.scroller.cycleSubscriptions.length).toEqual(0); if (misc.workflow.cyclesDone === 1) { @@ -109,7 +110,7 @@ const shouldReloadBeforeLoad = (config) => (misc) => (done) => { }); }; -const shouldReloadInterruption = (config) => (misc) => (done) => { +const shouldReloadInterruption = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { spyOn(misc.workflow, 'finalize').and.callFake(() => { expect(misc.scroller.cycleSubscriptions.length).toEqual(0); if (misc.workflow.cyclesDone === 1) { diff --git a/tests/bug.spec.ts b/tests/bug.spec.ts index f6692f8d..4e77966f 100644 --- a/tests/bug.spec.ts +++ b/tests/bug.spec.ts @@ -1,6 +1,7 @@ import { makeTest } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; -const checkViewport = (misc, viewportHeight) => { +const checkViewport = (misc: Misc, viewportHeight: number) => { const position = misc.getScrollPosition(); const size = misc.getScrollableSize(); const bwdPadding = misc.padding.backward.getSize(); @@ -25,7 +26,7 @@ describe('Bug Spec', () => { makeTest({ title: 'should continue fetch', config, - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { const fwdCount = 4; let wfCount = null; let jump = false; diff --git a/tests/common.spec.ts b/tests/common.spec.ts index a0189966..266bf8f9 100644 --- a/tests/common.spec.ts +++ b/tests/common.spec.ts @@ -7,13 +7,13 @@ import { configureTestBed } from './scaffolding/testBed'; import { defaultDatasourceClass } from './scaffolding/datasources'; import { defaultTemplate } from './scaffolding/templates'; import { Misc } from './miscellaneous/misc'; -import { makeTest } from './scaffolding/runner'; +import { makeTest, TestBedConfig } from './scaffolding/runner'; describe('Common Spec', () => { - let misc: Misc; describe('Initialization', () => { + let misc: Misc; let reconfigure = true; beforeEach(async(() => { @@ -53,11 +53,11 @@ describe('Common Spec', () => { const _settings3 = { infinite: true }; const _settings4 = { startIndex: 99, bufferSize: 11, infinite: true }; - const checkSettings = (_settings) => (misc) => (done) => { + const checkSettings = (_settings: any) => (misc: Misc) => (done: Function) => { expect(misc.scroller.settings).toEqual(jasmine.any(Object)); const mergedSettings = { ...defaultSettings, ..._settings }; Object.keys(defaultSettings).forEach(key => { - expect(misc.scroller.settings[key]).toEqual(mergedSettings[key]); + expect((misc.scroller.settings)[key]).toEqual((mergedSettings)[key]); }); done(); }; @@ -95,7 +95,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { startIndex: false } }, title: 'should fallback startIndex to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.startIndex).toEqual(defaultSettings.startIndex); done(); } @@ -104,7 +104,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { bufferSize: { weird: true } } }, title: 'should fallback bufferSize to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.bufferSize).toEqual(defaultSettings.bufferSize); done(); } @@ -113,7 +113,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { bufferSize: 5.5 } }, title: 'should fallback bufferSize to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.bufferSize).toEqual(defaultSettings.bufferSize); done(); } @@ -122,7 +122,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { bufferSize: -1 } }, title: 'should fallback bufferSize to the minimum', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.bufferSize).toEqual(minSettings.bufferSize); done(); } @@ -131,7 +131,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { padding: 'something' } }, title: 'should fallback padding to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.padding).toEqual(defaultSettings.padding); done(); } @@ -140,7 +140,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { padding: -0.1 } }, title: 'should fallback padding to the minimum', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.padding).toEqual(minSettings.padding); done(); } @@ -149,7 +149,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { infinite: 'something' } }, title: 'should fallback infinite to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.infinite).toEqual(defaultSettings.infinite); done(); } @@ -158,7 +158,7 @@ describe('Common Spec', () => { makeTest({ config: { datasourceSettings: { horizontal: null } }, title: 'should fallback horizontal to the default', - it: (misc) => (done) => { + it: (misc: Misc) => (done: Function) => { expect(misc.scroller.settings.horizontal).toEqual(defaultSettings.horizontal); done(); } @@ -176,7 +176,7 @@ describe('Bad datasource', () => { toThrow: true }, title: 'should throw exception (datasource is not a constructor)', - it: (error) => (done) => { + it: (error: any) => (done: Function) => { expect(error).toBe('datasource is not a constructor'); done(); } @@ -194,7 +194,7 @@ describe('Bad datasource', () => { toThrow: true }, title: 'should throw exception (no get)', - it: (error) => (done) => { + it: (error: any) => (done: Function) => { expect(error).toBe('Datasource get method is not implemented'); done(); } @@ -214,7 +214,7 @@ describe('Bad datasource', () => { toThrow: true }, title: 'should throw exception (get is not a function)', - it: (error) => (done) => { + it: (error: any) => (done: Function) => { expect(error).toBe('Datasource get is not a function'); done(); } @@ -229,13 +229,14 @@ describe('Bad datasource', () => { this.settings = {}; } - get(offset) { - }; + get(offset: number) { + return ++offset; + } }, toThrow: true }, title: 'should throw exception (get has less than 2 arguments)', - it: (error) => (done) => { + it: (error: any) => (done: Function) => { expect(error).toBe('Datasource get method invalid signature'); done(); } diff --git a/tests/datasource-get.spec.ts b/tests/datasource-get.spec.ts index 4e608ca0..e4b54abb 100644 --- a/tests/datasource-get.spec.ts +++ b/tests/datasource-get.spec.ts @@ -1,8 +1,9 @@ import { makeTest } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; describe('Datasource Get', () => { - const shouldWork = (misc) => (done) => + const shouldWork = (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => { expect(misc.scroller.state.fetch.count).toBeGreaterThan(0); done(); diff --git a/tests/eof.spec.ts b/tests/eof.spec.ts index df029f98..5f35e67a 100644 --- a/tests/eof.spec.ts +++ b/tests/eof.spec.ts @@ -1,5 +1,6 @@ import { Direction } from '../src/component/interfaces'; import { makeTest } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; const min = 1, max = 100, scrollCount = 10; @@ -18,7 +19,7 @@ describe('EOF/BOF Spec', () => { } }; - const expectLimit = (misc, direction: Direction, noscroll = false) => { + const expectLimit = (misc: Misc, direction: Direction, noscroll = false) => { const _forward = direction === Direction.forward; const elements = misc.getElements(); expect(elements.length).toBeGreaterThan(config[_forward ? 'eof' : 'bof'].datasourceSettings.bufferSize); @@ -40,13 +41,13 @@ describe('EOF/BOF Spec', () => { const _eof = isEOF ? 'bof' : 'eof'; const direction = isEOF ? Direction.forward : Direction.backward; const directionOpposite = isEOF ? Direction.backward : Direction.forward; - const doScroll = (misc) => isEOF ? misc.scrollMin() : misc.scrollMax(); - const doScrollOpposite = (misc) => isEOF ? misc.scrollMax() : misc.scrollMin(); + const doScroll = (misc: Misc) => isEOF ? misc.scrollMin() : misc.scrollMax(); + const doScrollOpposite = (misc: Misc) => isEOF ? misc.scrollMax() : misc.scrollMin(); makeTest({ - config: config[eof], + config: (config)[eof], title: `should get ${eof} on init`, - it: (misc) => (done) => + it: (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => { expectLimit(misc, direction, true); done(); @@ -54,30 +55,30 @@ describe('EOF/BOF Spec', () => { }); makeTest({ - config: config[eof], + config: (config)[eof], title: `should reset ${eof} after scroll`, - it: (misc) => (done) => + it: (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => { + const buffer = (misc.scroller.buffer); if (misc.workflow.cyclesDone === 1) { - expect(misc.scroller.buffer[eof]).toEqual(true); + expect(buffer[eof]).toEqual(true); doScroll(misc); } else { - expect(misc.scroller.buffer[eof]).toEqual(false); - expect(misc.scroller.buffer[_eof]).toEqual(false); + expect(buffer[eof]).toEqual(false); + expect(buffer[_eof]).toEqual(false); done(); } }) }); makeTest({ - config: config[eof], + config: (config)[eof], title: `should stop when ${eof} is reached again`, - it: (misc) => (done) => + it: (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => { if (misc.workflow.cyclesDone === 1) { doScroll(misc); - } - else if (misc.workflow.cyclesDone === 2) { + } else if (misc.workflow.cyclesDone === 2) { doScrollOpposite(misc); } else { expectLimit(misc, direction); @@ -87,9 +88,9 @@ describe('EOF/BOF Spec', () => { }); makeTest({ - config: config[eof], + config: (config)[eof], title: `should reach ${_eof} after some scrolls`, - it: (misc) => (done) => + it: (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => { if (misc.workflow.cyclesDone < scrollCount) { doScroll(misc); diff --git a/tests/initial-load.spec.ts b/tests/initial-load.spec.ts index fbcfc52a..95c1d55b 100644 --- a/tests/initial-load.spec.ts +++ b/tests/initial-load.spec.ts @@ -1,5 +1,6 @@ import { Direction } from '../src/component/interfaces'; -import { makeTest } from './scaffolding/runner'; +import { makeTest, TestBedConfig } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; const configList = [{ datasourceSettings: { startIndex: 1, bufferSize: 1, padding: 2 }, @@ -47,7 +48,7 @@ const configListWithClip = [{ }]; const configListInfinite = configListWithClip - .filter((item, i) => [1, 3, 4, 5].includes(i)) + .filter((item, i) => [1, 3, 4, 5].indexOf(i) !== -1) .map(config => ({ ...config, datasourceSettings: { @@ -56,7 +57,7 @@ const configListInfinite = configListWithClip } })); -const _shouldNotClip = (settings, misc, done) => { +const _shouldNotClip = (settings: TestBedConfig, misc: Misc, done: Function) => { const startIndex = settings.datasourceSettings.startIndex; const bufferSize = settings.datasourceSettings.bufferSize; const padding = settings.datasourceSettings.padding; @@ -85,7 +86,7 @@ const _shouldNotClip = (settings, misc, done) => { done(); }; -const _shouldClip = (settings, misc, done) => { +const _shouldClip = (settings: TestBedConfig, misc: Misc, done: Function) => { const startIndex = settings.datasourceSettings.startIndex; const bufferSize = settings.datasourceSettings.bufferSize; const padding = settings.datasourceSettings.padding; @@ -122,12 +123,12 @@ const _shouldClip = (settings, misc, done) => { done(); }; -const shouldNotClip = (settings) => (misc) => (done) => +const shouldNotClip = (settings: TestBedConfig) => (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => _shouldNotClip(settings, misc, done) ); -const shouldClip = (settings) => (misc) => (done) => +const shouldClip = (settings: TestBedConfig) => (misc: Misc) => (done: Function) => spyOn(misc.workflow, 'finalize').and.callFake(() => _shouldClip(settings, misc, done) ); diff --git a/tests/miscellaneous/debounce.ts b/tests/miscellaneous/debounce.ts index cd2700cb..75ab0e38 100644 --- a/tests/miscellaneous/debounce.ts +++ b/tests/miscellaneous/debounce.ts @@ -1,17 +1,17 @@ -export function debounce(func, wait, immediate?) { - let timeout, context, args; +export function debounce(func: Function, wait: number, immediate?: boolean) { + let timeout: number, context: any, args: any; return function (this: any) { context = this; args = arguments; const later = function () { - timeout = null; + timeout = 0; if (!immediate) { func.apply(context, args); } }; const callNow = immediate && !timeout; clearTimeout(timeout); - timeout = setTimeout(later, wait); + timeout = setTimeout(later, wait); if (callNow) { func.apply(context, args); } diff --git a/tests/miscellaneous/misc.ts b/tests/miscellaneous/misc.ts index 7b1e8604..7b6d8f92 100644 --- a/tests/miscellaneous/misc.ts +++ b/tests/miscellaneous/misc.ts @@ -5,10 +5,11 @@ import { DebugElement } from '@angular/core'; import { TestComponentInterface } from '../scaffolding/testComponent'; import { TestBedConfig } from '../scaffolding/runner'; -import { Direction, Datasource } from '../../src/component/interfaces'; +import { Direction } from '../../src/component/interfaces'; import { UiScrollComponent } from '../../src/ui-scroll.component'; import { Scroller } from '../../src/component/scroller'; import { Workflow } from '../../src/component/workflow'; +import { Datasource } from '../../src/component/classes/datasource'; export class Padding { direction: Direction; @@ -24,7 +25,8 @@ export class Padding { } getSize(): number { - return parseInt(this.style[this.horizontal ? 'width' : 'height'], 10) || 0; + const size = this.style[this.horizontal ? 'width' : 'height'] || ''; + return parseInt(size, 10) || 0; } } @@ -38,7 +40,10 @@ export class Misc { uiScrollComponent: UiScrollComponent; workflow: Workflow; scroller: Scroller; - padding = {}; + padding: { + forward: Padding; + backward: Padding; + }; horizontal: boolean; window: boolean; @@ -49,16 +54,18 @@ export class Misc { constructor(fixture: ComponentFixture) { this.fixture = fixture; this.testComponent = fixture.componentInstance; - this.datasource = this.testComponent.datasource; + this.datasource = this.testComponent.datasource; this.uiScrollElement = this.fixture.debugElement.query(By.css('[ui-scroll]')); this.uiScrollComponent = this.uiScrollElement.componentInstance; - this.viewportElement = this.uiScrollElement.parent; + this.viewportElement = this.uiScrollElement.parent; this.workflow = this.uiScrollComponent.workflow; this.scroller = this.uiScrollComponent.workflow.scroller; this.horizontal = this.scroller.settings.horizontal; this.window = this.scroller.settings.windowViewport; - this.padding[Direction.forward] = new Padding(fixture, Direction.forward, this.horizontal); - this.padding[Direction.backward] = new Padding(fixture, Direction.backward, this.horizontal); + this.padding = { + forward: new Padding(fixture, Direction.forward, this.horizontal), + backward: new Padding(fixture, Direction.backward, this.horizontal) + }; } getViewportSize(settings: TestBedConfig): number { @@ -83,11 +90,11 @@ export class Misc { return element ? element.innerText.trim() : null; } - checkElementId(element, index: number): boolean { + checkElementId(element: HTMLElement, index: number): boolean { return element.getAttribute('data-sid') === `${index}`; } - getElementIndex(element): number { + getElementIndex(element: HTMLElement): number | null { const id = element.getAttribute('data-sid'); if (!id) { return null; diff --git a/tests/scaffolding/datasources.ts b/tests/scaffolding/datasources.ts index e957f0c6..1ae377c7 100644 --- a/tests/scaffolding/datasources.ts +++ b/tests/scaffolding/datasources.ts @@ -1,4 +1,4 @@ -import { Observable } from 'rxjs'; +import { Observable, Observer } from 'rxjs'; import { Datasource, Settings, DevSettings } from '../../src/component/interfaces'; export class DatasourceService implements Datasource { @@ -13,16 +13,16 @@ export const generateDatasourceClass = (_name: string, _settings?: Settings, _de devSettings: DevSettings; constructor() { - this.get = datasourceStore[_name].get.bind(this); - this.settings = datasourceStore[_name].settings || _settings || {}; - this.devSettings = datasourceStore[_name].devSettings || _devSettings || {}; + this.get = (datasourceStore)[_name].get.bind(this); + this.settings = (datasourceStore)[_name].settings || _settings || {}; + this.devSettings = (datasourceStore)[_name].devSettings || _devSettings || {}; } }; }; export const defaultDatasourceClass = generateDatasourceClass('initial'); -const datasourceGetInfinite = (index, count) => { +const datasourceGetInfinite = (index: number, count: number) => { const data = []; for (let i = index; i <= index + count - 1; i++) { data.push({ id: i, text: 'item #' + i }); @@ -30,13 +30,13 @@ const datasourceGetInfinite = (index, count) => { return data; }; -const datasourceGetLimited = (index, count, min, max) => { +const datasourceGetLimited = (index: number, count: number, min: number, max: number) => { const data = []; const start = Math.max(min, index); const end = Math.min(index + count - 1, max); if (start <= end) { for (let i = start; i <= end; i++) { - data.push({ id: i, text: "item #" + i }); + data.push({ id: i, text: 'item #' + i }); } } return data; @@ -60,13 +60,13 @@ const infiniteDatasourceGet = (type?: DatasourceType, delay?: number) => (index: number, count: number, success?: Function) => { switch (type) { case DatasourceType.Callback: - return delayedRun(() => success(datasourceGetInfinite(index, count)), delay); + return delayedRun(() => (success)(datasourceGetInfinite(index, count)), delay); case DatasourceType.Promise: return new Promise(resolve => delayedRun(() => resolve(datasourceGetInfinite(index, count)), delay) ); default: // DatasourceType.Observable - return Observable.create(observer => + return Observable.create((observer: Observer) => delayedRun(() => observer.next(datasourceGetInfinite(index, count)), delay) ); } @@ -76,13 +76,13 @@ const limitedDatasourceGet = (min: number, max: number, type?: DatasourceType, d (index: number, count: number, success?: Function) => { switch (type) { case DatasourceType.Callback: - return delayedRun(() => success(datasourceGetLimited(index, count, min, max)), delay); + return delayedRun(() => (success)(datasourceGetLimited(index, count, min, max)), delay); case DatasourceType.Promise: return new Promise(resolve => delayedRun(() => resolve(datasourceGetLimited(index, count, min, max)), delay) ); default: // DatasourceType.Observable - return Observable.create(observer => + return Observable.create((observer: Observer) => delayedRun(() => observer.next(datasourceGetLimited(index, count, min, max)), delay) ); } diff --git a/tests/scaffolding/runner.ts b/tests/scaffolding/runner.ts index 0464d37a..3b336742 100644 --- a/tests/scaffolding/runner.ts +++ b/tests/scaffolding/runner.ts @@ -1,10 +1,11 @@ +import { async } from '@angular/core/testing'; + import { Settings, DevSettings } from '../../src/component/interfaces'; import { Misc } from '../miscellaneous/misc'; import { configureTestBed } from './testBed'; import { generateTemplate, TemplateSettings } from './templates'; import { generateDatasourceClass } from './datasources'; -import { async } from '@angular/core/testing'; export interface TestBedConfig { datasourceClass?: any; @@ -19,7 +20,7 @@ export interface TestBedConfig { interface MakeTestConfig { title: string; - config?: TestBedConfig; + config: TestBedConfig; it?: any; async?: boolean; } @@ -66,7 +67,7 @@ export const makeTest = (data: MakeTestConfig) => { let _it, timeout = 2000; if (data.config) { let misc: Misc; - let error; + let error: any; beforeEach(() => { const datasourceClass = data.config.datasourceClass ? data.config.datasourceClass : @@ -80,10 +81,15 @@ export const makeTest = (data: MakeTestConfig) => { const fixture = configureTestBed(datasourceClass, templateData.template); misc = new Misc(fixture); } catch (_error) { - error = _error && _error.message; + error = _error; } }); - _it = (done) => data.it(data.config.toThrow ? error : misc)(done); + _it = (done: Function) => { + if (!data.config.toThrow && error) { + throw error; + } + return data.it(data.config.toThrow ? error.message : misc)(done); + }; timeout = data.config.timeout || timeout; } else { _it = data.it; diff --git a/tests/scaffolding/templates.ts b/tests/scaffolding/templates.ts index 24044e77..fdd922a4 100644 --- a/tests/scaffolding/templates.ts +++ b/tests/scaffolding/templates.ts @@ -1,9 +1,9 @@ export interface TemplateSettings { noViewportClass?: boolean; viewportHeight?: number; - viewportWidth?: number; + viewportWidth?: number | null; itemHeight?: number; - itemWidth?: number; + itemWidth?: number | null; horizontal?: boolean; } diff --git a/tests/scaffolding/testBed.ts b/tests/scaffolding/testBed.ts index 3561abac..89948bb7 100644 --- a/tests/scaffolding/testBed.ts +++ b/tests/scaffolding/testBed.ts @@ -6,7 +6,7 @@ import { UiScrollModule } from '../../src/ui-scroll.module'; import { TestComponent } from './testComponent'; import { DatasourceService } from './datasources'; -export const configureTestBed = (datasource, template) => { +export const configureTestBed = (datasource: any, template: string) => { return TestBed .configureTestingModule({ imports: [UiScrollModule], diff --git a/tests/scroll-basic.spec.ts b/tests/scroll-basic.spec.ts index 6cf83271..3470a8a3 100644 --- a/tests/scroll-basic.spec.ts +++ b/tests/scroll-basic.spec.ts @@ -1,5 +1,6 @@ import { Direction } from '../src/component/interfaces'; -import { makeTest } from './scaffolding/runner'; +import { makeTest, TestBedConfig } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; const singleForwardMaxScrollConfigList = [{ datasourceSettings: { startIndex: 100, bufferSize: 4, padding: 0.22 }, @@ -27,7 +28,7 @@ const singleForwardMaxScrollConfigList = [{ custom: { direction: Direction.forward, count: 1 } }]; -const treatIndex = (index) => index <= 3 ? index : (3 * 2 - index); +const treatIndex = (index: number) => index <= 3 ? index : (3 * 2 - index); const singleBackwardMaxScrollConfigList = singleForwardMaxScrollConfigList.map(config => ({ @@ -100,7 +101,7 @@ const massTwoDirectionalScrollsConfigList_bwd = timeout: 4000 })); -const doScrollMax = (config, misc) => { +const doScrollMax = (config: TestBedConfig, misc: Misc) => { if (config.custom.direction === Direction.forward) { misc.scrollMax(); } else { @@ -108,12 +109,12 @@ const doScrollMax = (config, misc) => { } }; -const invertDirection = (config) => { +const invertDirection = (config: TestBedConfig) => { const _forward = config.custom.direction === Direction.forward; config.custom.direction = _forward ? Direction.backward : Direction.forward; }; -const calculateIt = (config, misc) => { +const calculateIt = (config: TestBedConfig, misc: Misc) => { // settings const bufferSize = config.datasourceSettings.bufferSize; const padding = config.datasourceSettings.padding; @@ -126,7 +127,7 @@ const calculateIt = (config, misc) => { const forward = direction === Direction.forward; const elements = misc.getElements(); const _edgeElement = forward ? elements[elements.length - 1] : elements[0]; - const _edgeItemIndex = misc.getElementIndex(_edgeElement); + const _edgeItemIndex = misc.getElementIndex(_edgeElement) || 0; const _edgePosition = misc.padding[Direction.backward].getSize() + (forward ? elements.length * itemSize : 0); // future state calculations (direct) @@ -172,9 +173,9 @@ const calculateIt = (config, misc) => { }; }; -const shouldScroll = (config) => (misc) => (done) => { +const shouldScroll = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { const wfCount = config.custom.count + 1; - let result; + let result: any; spyOn(misc.workflow, 'finalize').and.callFake(() => { const cycles = misc.workflow.cyclesDone; @@ -196,10 +197,10 @@ const shouldScroll = (config) => (misc) => (done) => { const opposite = direction === Direction.forward ? Direction.backward : Direction.forward; const edgeItem = misc.scroller.buffer.getEdgeVisibleItem(direction); const oppositeEdgeItem = misc.scroller.buffer.getEdgeVisibleItem(opposite); - expect(misc.padding[direction].getSize()).toEqual(result.paddingSize); - expect(misc.padding[opposite].getSize()).toEqual(result.paddingSizeOpposite); - expect(edgeItem.$index).toEqual(result.edgeItemIndex); - expect(oppositeEdgeItem.$index).toEqual(result.edgeItemIndexOpposite); + expect((misc.padding)[direction].getSize()).toEqual(result.paddingSize); + expect((misc.padding)[opposite].getSize()).toEqual(result.paddingSizeOpposite); + expect(edgeItem ? edgeItem.$index : null).toEqual(result.edgeItemIndex); + expect(oppositeEdgeItem ? oppositeEdgeItem.$index : null).toEqual(result.edgeItemIndexOpposite); done(); } }); diff --git a/tests/scroll-fast.spec.ts b/tests/scroll-fast.spec.ts index a6e8129d..17e87fb2 100644 --- a/tests/scroll-fast.spec.ts +++ b/tests/scroll-fast.spec.ts @@ -1,5 +1,6 @@ import { debounce } from './miscellaneous/debounce'; -import { makeTest } from './scaffolding/runner'; +import { makeTest, TestBedConfig } from './scaffolding/runner'; +import { Misc } from './miscellaneous/misc'; describe('Fast Scroll Spec', () => { @@ -41,9 +42,9 @@ describe('Fast Scroll Spec', () => { } })); - const runFastScroll = (misc, customConfig) => { - misc.shared.fin = false; - const scr = (iteration) => new Promise(success => { + const runFastScroll = (misc: Misc, customConfig: any) => { + (misc.shared).fin = false; + const scr = (iteration: number) => new Promise(success => { setTimeout(() => { misc.scrollMax(); setTimeout(() => { @@ -58,10 +59,10 @@ describe('Fast Scroll Spec', () => { for (let i = 1; i <= customConfig.bounce; i++) { result = result.then(() => scr(i)); } - result.then(() => misc.shared.fin = true); + result.then(() => (misc.shared).fin = true); }; - const expectations = (config, misc, done) => { + const expectations = (config: TestBedConfig, misc: Misc, done: Function) => { const itemsCount = misc.scroller.buffer.size; const bufferHeight = itemsCount * misc.itemHeight; const _size = misc.padding.backward.getSize() + misc.padding.forward.getSize() + bufferHeight; @@ -82,8 +83,8 @@ describe('Fast Scroll Spec', () => { done(); }; - let expectationsTimer; - const preExpectations = (config, misc, done) => { + let expectationsTimer: number; + const preExpectations = (config: TestBedConfig, misc: Misc, done: Function) => { const position = misc.getScrollPosition(); const buffer = misc.scroller.buffer; const index = position === 0 ? 0 : buffer.size - 1; @@ -100,17 +101,17 @@ describe('Fast Scroll Spec', () => { if (!misc.scroller.state.pending && buffer.size && buffer.items[index] && buffer.items[index].element) { runExpectations(); } else { - expectationsTimer = setTimeout(() => preExpectations(config, misc, done), 25); + expectationsTimer = setTimeout(() => preExpectations(config, misc, done), 25); } }; - const checkFastScroll = (config) => (misc) => (done) => { + const checkFastScroll = (config: TestBedConfig) => (misc: Misc) => (done: Function) => { clearTimeout(expectationsTimer); const _done = debounce(() => preExpectations(config, misc, done), 25); spyOn(misc.workflow, 'finalize').and.callFake(() => { if (misc.workflow.cyclesDone === 1) { runFastScroll(misc, config.custom); - } else if (misc.shared.fin) { + } else if ((misc.shared).fin) { _done(); } });