Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add playwright test #17

Merged
merged 6 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests
on:
push:
branches: [ main, master ]
pull_request:
branches: [ main, master ]
jobs:
test:
timeout-minutes: 60
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '16.x'
- name: Install dependencies
run: npm i
- name: Install Playwright Browsers
run: npx playwright install --with-deps
- name: Run unit tests
run: npm test
- name: Run Playwright tests
run: npx playwright test
- uses: actions/upload-artifact@v4
if: always()
with:
name: playwright-report
path: playwright-report/
retention-days: 30
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,7 @@ TSWLatexianTemp*
# expex forward references with \gathertags

# End of https://www.gitignore.io/api/latex,tex
/test-results/
/playwright-report/
/blob-report/
/playwright/.cache/
41,123 changes: 23,108 additions & 18,015 deletions package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react": "16.12.0",
"react-color": "2.18.0",
"react-dom": "16.12.0",
"react-scripts": "3.0.1"
"react-scripts": "5.0.1"
},
"scripts": {
"start": "react-scripts start",
Expand All @@ -23,6 +23,12 @@
"format": "npx @biomejs/biome format --write ./src",
"lint": "npx @biomejs/biome lint --apply ./src"
},
"jest": {
"testPathIgnorePatterns": [
"/node_modules/",
"tests/e2e/"
]
},
"browserslist": [
">0.2%",
"not dead",
Expand All @@ -31,7 +37,9 @@
],
"devDependencies": {
"@biomejs/biome": "1.7.3",
"jest": "^29.7.0",
"@playwright/test": "^1.44.0",
"@types/node": "^20.12.12",
"jest": "28.1.2",
"lefthook": "^1.6.11"
}
}
79 changes: 79 additions & 0 deletions playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @ts-check
const { defineConfig, devices } = require('@playwright/test');

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* @see https://playwright.dev/docs/test-configuration
*/
module.exports = defineConfig({
testDir: './tests/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://127.0.0.1:3000',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
webServer: {
command: 'npm start',
url: 'http://127.0.0.1:3000',
reuseExistingServer: !process.env.CI,
},
});

13 changes: 9 additions & 4 deletions src/components/DiagramEditor/utils/getStyleByKey.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
export default function getStyleByKey(style, key) {
const obj = {};
const stylesArr = style.split(";");
const stylesArr = style.split(";").filter(Boolean); // Filter out empty strings

// Using for...of loop instead of forEach
for (const v of stylesArr) {
const [keyPart, value] = v.split("=");
obj[keyPart.trim()] = value.trim();
const keyValueArray = v.split("=");
if (keyValueArray.length === 2) {
const keyPart = keyValueArray[0].trim();
const value = keyValueArray[1]?.trim() || ""; // Safely trim and provide a fallback value
obj[keyPart] = value;
} else {
console.warn(`Unexpected format in style string "${v}"`);
}
}

return obj[key];
Expand Down
17 changes: 17 additions & 0 deletions tests/e2e/entity.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { test, expect } from '@playwright/test';

test('add entities to the canvas', async ({ page }) => {
await page.goto('/');
// TODO: Improve this using better ids for the elements
// This is the canvas
const canvas = page.locator("svg")
await page.getByRole('img').first().dragTo(canvas);

await canvas.click();

await page.locator('rect').first().dblclick();
await page.keyboard.type('Hello');

await canvas.click();

});
File renamed without changes.
File renamed without changes.
Loading