Refactor mock system for playwright

This commit is contained in:
Matthias 2024-04-08 20:53:15 +02:00
parent a2fe720d31
commit a622ab6c57
2 changed files with 43 additions and 34 deletions

View File

@ -19,6 +19,21 @@ export async function setLoginInfo(page) {
}); });
} }
interface mockArray {
name: string;
url: string;
fixture: string;
method?: string;
}
function mockRequests(page, mocks: mockArray[]) {
mocks.forEach((item) => {
page.route(item.url, (route) => {
return route.fulfill({ path: `./cypress/fixtures/${item.fixture}` });
});
});
}
export async function defaultMocks(page: Page) { export async function defaultMocks(page: Page) {
page.route('**/api/v1/**', (route) => { page.route('**/api/v1/**', (route) => {
route.fulfill({ route.fulfill({
@ -27,15 +42,33 @@ export async function defaultMocks(page: Page) {
}); });
}); });
await page.route('**/api/v1/ping', (route) => { const mapping: mockArray[] = [
return route.fulfill({ path: './cypress/fixtures/ping.json' }); { name: '@Ping', url: '**/api/v1/ping', fixture: 'ping.json' },
}); { name: '@Ping', url: '**/api/v1/show_config', fixture: 'show_config.json' },
await page.route('**/api/v1/show_config', (route) => { { name: '@Ping', url: '**/api/v1/pair_candles?*', fixture: 'pair_candles_btc_1m.json' },
return route.fulfill({ path: './cypress/fixtures/show_config.json' }); ];
});
await page.route('**/api/v1/pair_candles?*', (route) => { mockRequests(page, mapping);
return route.fulfill({ path: './cypress/fixtures/pair_candles_btc_1m.json' }); }
});
export function tradeMocks(page) {
const mapping: mockArray[] = [
{ name: '@Status', url: '**/api/v1/status', fixture: 'status_empty.json' },
{ name: '@Profit', url: '**/api/v1/profit', fixture: 'profit.json' },
{ name: '@Trades', url: '**/api/v1/trades*', fixture: 'trades.json' },
{ name: '@Balance', url: '**/api/v1/balance', fixture: 'balance.json' },
{ name: '@Whitelist', url: '**/api/v1/whitelist', fixture: 'whitelist.json' },
{ name: '@Blacklist', url: '**/api/v1/blacklist', fixture: 'blacklist.json' },
{ name: '@Locks', url: '**/api/v1/locks', fixture: 'locks_empty.json' },
{ name: '@Performance', url: '**/api/v1/performance', fixture: 'performance.json' },
{
name: '@ReloadConfig',
method: 'POST',
url: '**/api/v1/reload_config',
fixture: 'reload_config.json',
},
];
mockRequests(page, mapping);
} }
export function getWaitForResponse(page: Page, url: string) { export function getWaitForResponse(page: Page, url: string) {

View File

@ -1,30 +1,6 @@
import { test, expect } from '@playwright/test'; import { test, expect } from '@playwright/test';
import { setLoginInfo, defaultMocks } from './helpers'; import { setLoginInfo, defaultMocks, tradeMocks } from './helpers';
function tradeMocks(page) {
const mapping = [
{ name: '@Status', url: '**/api/v1/status', fixture: 'status_empty.json' },
{ name: '@Profit', url: '**/api/v1/profit', fixture: 'profit.json' },
{ name: '@Trades', url: '**/api/v1/trades*', fixture: 'trades.json' },
{ name: '@Balance', url: '**/api/v1/balance', fixture: 'balance.json' },
{ name: '@Whitelist', url: '**/api/v1/whitelist', fixture: 'whitelist.json' },
{ name: '@Blacklist', url: '**/api/v1/blacklist', fixture: 'blacklist.json' },
{ name: '@Locks', url: '**/api/v1/locks', fixture: 'locks_empty.json' },
{ name: '@Performance', url: '**/api/v1/performance', fixture: 'performance.json' },
{
name: '@ReloadConfig',
method: 'POST',
url: '**/api/v1/reload_config',
fixture: 'reload_config.json',
},
];
mapping.forEach((item) => {
page.route(item.url, (route) => {
return route.fulfill({ path: `./cypress/fixtures/${item.fixture}` });
});
});
}
test.describe('Trade', () => { test.describe('Trade', () => {
test.beforeEach(async ({ page }) => { test.beforeEach(async ({ page }) => {