From a622ab6c5771b5daf43a46994d0f57dee24f0c4f Mon Sep 17 00:00:00 2001 From: Matthias Date: Mon, 8 Apr 2024 20:53:15 +0200 Subject: [PATCH] Refactor mock system for playwright --- e2e/helpers.ts | 51 ++++++++++++++++++++++++++++++++++++++--------- e2e/trade.spec.ts | 26 +----------------------- 2 files changed, 43 insertions(+), 34 deletions(-) diff --git a/e2e/helpers.ts b/e2e/helpers.ts index 797be20a..3be26114 100644 --- a/e2e/helpers.ts +++ b/e2e/helpers.ts @@ -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) { page.route('**/api/v1/**', (route) => { route.fulfill({ @@ -27,15 +42,33 @@ export async function defaultMocks(page: Page) { }); }); - await page.route('**/api/v1/ping', (route) => { - return route.fulfill({ path: './cypress/fixtures/ping.json' }); - }); - await page.route('**/api/v1/show_config', (route) => { - return route.fulfill({ path: './cypress/fixtures/show_config.json' }); - }); - await page.route('**/api/v1/pair_candles?*', (route) => { - return route.fulfill({ path: './cypress/fixtures/pair_candles_btc_1m.json' }); - }); + const mapping: mockArray[] = [ + { name: '@Ping', url: '**/api/v1/ping', fixture: 'ping.json' }, + { name: '@Ping', url: '**/api/v1/show_config', fixture: 'show_config.json' }, + { name: '@Ping', url: '**/api/v1/pair_candles?*', fixture: 'pair_candles_btc_1m.json' }, + ]; + + mockRequests(page, mapping); +} + +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) { diff --git a/e2e/trade.spec.ts b/e2e/trade.spec.ts index 5bb5230a..911da944 100644 --- a/e2e/trade.spec.ts +++ b/e2e/trade.spec.ts @@ -1,30 +1,6 @@ import { test, expect } from '@playwright/test'; -import { setLoginInfo, defaultMocks } 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}` }); - }); - }); -} +import { setLoginInfo, defaultMocks, tradeMocks } from './helpers'; test.describe('Trade', () => { test.beforeEach(async ({ page }) => {