Skip to content

Commit

Permalink
e2e: runner
Browse files Browse the repository at this point in the history
  • Loading branch information
dhilt committed May 24, 2024
1 parent b8497f4 commit 2ebdda3
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 58 deletions.
18 changes: 5 additions & 13 deletions tests/e2e/initialization.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { test, expect, Page } from '@playwright/test';
import { test, expect } from '@playwright/test';
import { runScroller } from './misc/runner';

const URL = '127.0.0.1:3000';

Expand Down Expand Up @@ -30,18 +31,9 @@ test('DOM + Workflow', async ({ page }) => {
expect(name).toBe('vscroll');
});

const runScroller = async (page: Page, { settings = {}, devSettings = {} } = {}) =>
await page.evaluate(({ settings, devSettings }) => {
const { Scroller, datasource } = window['__vscroll__'];
datasource.settings = { ...datasource.settings, ...settings };
datasource.devSettings = { ...datasource.devSettings, ...devSettings };
const { workflow } = new Scroller(datasource);
window['__vscroll__'].workflow = workflow;
}, { settings, devSettings });

test('Workflow & Adapter. Delayed initialization', async ({ page }) => {
await page.goto(URL + '/need-run');
await runScroller(page, { devSettings: { initDelay: 100 } });
await runScroller(page, { datasourceDevSettings: { initDelay: 100 } });

await page.waitForFunction(() => {
const { workflow, datasource } = window['__vscroll__'];
Expand All @@ -58,7 +50,7 @@ test('Workflow & Adapter. Delayed initialization', async ({ page }) => {

test('Workflow & Adapter. Disposing after delayed initialization', async ({ page }) => {
await page.goto(URL + '/need-run');
await runScroller(page, { devSettings: { initDelay: 100 } });
await runScroller(page, { datasourceDevSettings: { initDelay: 100 } });

await page.waitForTimeout(100);
await page.evaluate(() => window['__vscroll__'].workflow.dispose());
Expand All @@ -71,7 +63,7 @@ test('Workflow & Adapter. Disposing after delayed initialization', async ({ page

test('Workflow & Adapter. Disposing before delayed initialization', async ({ page }) => {
await page.goto(URL + '/need-run');
await runScroller(page, { devSettings: { initDelay: 100 } });
await runScroller(page, { datasourceDevSettings: { initDelay: 100 } });

await page.evaluate(() => window['__vscroll__'].workflow.dispose());
await page.waitForTimeout(100);
Expand Down
1 change: 0 additions & 1 deletion tests/e2e/misc/itemsCounter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { Page } from '@playwright/test';
import { Direction } from '../../../src/inputs/common';
import { TESTS } from './types';
Expand Down
42 changes: 42 additions & 0 deletions tests/e2e/misc/runner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Page } from '@playwright/test';
import { Config } from './types';

export const runScroller = async (page: Page, config: Config = {}) =>
await page.evaluate(config => {
const { datasourceSettings, datasourceDevSettings, templateSettings } = config as Config;
const { Scroller, datasource } = window['__vscroll__'];
datasource.settings = { ...datasource.settings, ...datasourceSettings };
datasource.devSettings = { ...datasource.devSettings, ...datasourceDevSettings };

const viewport = window.document.querySelector('.viewport') as HTMLElement;
if (templateSettings?.viewportWidth) {
viewport.style.width = templateSettings.viewportWidth + 'px';
}
if (templateSettings?.viewportHeight) {
viewport.style.height = templateSettings.viewportHeight + 'px';
}
if (templateSettings?.horizontal) {
viewport.className += ' horizontal';
}
let styles = '';
if (templateSettings?.itemWidth) {
styles += `
.viewport .item {
width: ${templateSettings.itemWidth}px;
}`;
}
if (templateSettings?.itemHeight) {
styles += `
.viewport .item {
height: ${templateSettings.itemHeight}px;
}`;
}
if (styles) {
const styleSheet = document.createElement('style');
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
}

const { workflow } = new Scroller(datasource);
window['__vscroll__'].workflow = workflow;
}, config as unknown);
4 changes: 2 additions & 2 deletions tests/e2e/misc/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ type TemplateSettings = {
export type Config<Custom = void> = {
datasourceClass?: { new(): unknown };
datasourceName?: string;
datasourceSettings: Settings;
datasourceDevSettings: DevSettings;
datasourceSettings?: Settings;
datasourceDevSettings?: DevSettings;
templateSettings?: TemplateSettings;
toThrow?: boolean;
custom?: Custom;
Expand Down
44 changes: 2 additions & 42 deletions tests/e2e/scroll-basic.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { test, expect, Page } from '@playwright/test';
import { ScrollResult, initializeItemsCounter } from './misc/itemsCounter';
import { Config, It, MakeTest } from './misc/types';
import { runScroller } from './misc/runner';
import { Direction } from '../../src/inputs/common';

// test.use({ headless: false });
Expand Down Expand Up @@ -166,53 +167,12 @@ const shouldScroll = async (config: Config<ICustom>, page: Page) => {
.toHaveText(`${oppositeItemIndex[0]}) item #${oppositeItemIndex[0]}`);
};

const runScroller = async (page: Page, config: Config<ICustom>) =>
await page.evaluate(config => {
const { datasourceSettings, datasourceDevSettings, templateSettings } = config as Config<ICustom>;
const { Scroller, datasource } = window['__vscroll__'];
datasource.settings = { ...datasource.settings, ...datasourceSettings };
datasource.devSettings = { ...datasource.devSettings, ...datasourceDevSettings };

const viewport = window.document.querySelector('.viewport') as HTMLElement;
if (templateSettings?.viewportWidth) {
viewport.style.width = templateSettings.viewportWidth + 'px';
}
if (templateSettings?.viewportHeight) {
viewport.style.height = templateSettings.viewportHeight + 'px';
}
if (templateSettings?.horizontal) {
viewport.className += ' horizontal';
}
let styles = '';
if (templateSettings?.itemWidth) {
styles += `
.viewport .item {
width: ${templateSettings.itemWidth}px;
}`;
}
if (templateSettings?.itemHeight) {
styles += `
.viewport .item {
height: ${templateSettings.itemHeight}px;
}`;
}
if (styles) {
const styleSheet = document.createElement('style');
styleSheet.innerText = styles;
document.head.appendChild(styleSheet);
}

const { workflow } = new Scroller(datasource);
window['__vscroll__'].workflow = workflow;
}, config as unknown);


const makeTest: MakeTest<ICustom> = ({ title, config, it }) =>
test(title, ({ page }) => it({ config, page }));

const shouldWork: It<ICustom> = async ({ config, page }) => {
await page.goto(URL + '/need-run');
await runScroller(page, config);
await runScroller(page, config as Config);
await shouldScroll(config, page);
// await new Promise(r => setTimeout(r, 2000));
};
Expand Down

0 comments on commit 2ebdda3

Please sign in to comment.