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

fix: yarn create command in PackageManagerTabs #1358

Merged
merged 2 commits into from
Aug 28, 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
11 changes: 11 additions & 0 deletions e2e/fixtures/package-manager-tabs/doc/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PackageManagerTabs } from '@theme';

# Hello World

## PackageManagerTabs A

<PackageManagerTabs command="create rspress@latest" />

## PackageManagerTabs B

<PackageManagerTabs command="install rspress -D" />
16 changes: 16 additions & 0 deletions e2e/fixtures/package-manager-tabs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@rspress-fixture/rspress-package-manager-tabs",
"version": "1.0.0",
"private": true,
"scripts": {
"dev": "rspress dev",
"build": "rspress build",
"preview": "rspress preview"
},
"dependencies": {
"rspress": "workspace:*"
},
"devDependencies": {
"@types/node": "^18.11.17"
}
}
6 changes: 6 additions & 0 deletions e2e/fixtures/package-manager-tabs/rspress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as path from 'node:path';
import { defineConfig } from 'rspress/config';

export default defineConfig({
root: path.join(__dirname, 'doc'),
});
92 changes: 92 additions & 0 deletions e2e/tests/package-manager-tabs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect, test } from '@playwright/test';
import path from 'node:path';
import { getPort, killProcess, runDevCommand } from '../utils/runCommands';

const fixtureDir = path.resolve(__dirname, '../fixtures');

test.describe('tabs-component test', async () => {
let appPort;
let app;

test.beforeAll(async () => {
const appDir = path.join(fixtureDir, 'package-manager-tabs');
appPort = await getPort();
app = await runDevCommand(appDir, appPort);
});

test.afterAll(async () => {
if (app) {
await killProcess(app);
}
});

test('Index page', async ({ page }) => {
await page.goto(`http://localhost:${appPort}`);

const tabs = await page.$$('[class^="tab_"] > div > span');
const tabsText = await Promise.all(
tabs.map(element => element.textContent()),
);

expect(tabsText).toEqual([
'npm',
'yarn',
'pnpm',
'bun',
'npm',
'yarn',
'pnpm',
'bun',
]);

const clickTabs = await page.$$('[class^="tab_"]');

await clickTabs[0].click();
const npmSpanElements = await page.$$('code > span > span');
const npmCode = await Promise.all(
npmSpanElements.map(element => element.textContent()),
);
expect(npmCode).toEqual([
'npm create rspress@latest',
'npm install rspress ',
'-',
'D',
]);

await clickTabs[1].click();
const yarnSpanElements = await page.$$('code > span > span');
const yarnCode = await Promise.all(
yarnSpanElements.map(element => element.textContent()),
);
expect(yarnCode).toEqual([
'yarn create rspress',
'yarn add rspress ',
'-',
'D',
]);

await clickTabs[2].click();
const pnpmSpanElements = await page.$$('code > span > span');
const pnpmCode = await Promise.all(
pnpmSpanElements.map(element => element.textContent()),
);
expect(pnpmCode).toEqual([
'pnpm create rspress@latest',
'pnpm install rspress ',
'-',
'D',
]);

await clickTabs[3].click();
const bunSpanElements = await page.$$('code > span > span');
const bunCode = await Promise.all(
bunSpanElements.map(element => element.textContent()),
);
expect(bunCode).toEqual([
'bun create rspress@latest',
'bun add rspress ',
'-',
'D',
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ export interface PackageManagerTabProps {
}

function normalizeCommand(command: string): string {
// If command is yarn create foo@latest, remove `@latest`
if (command.startsWith('yarn create')) {
return command.replace(/(yarn create [^\s]+)@latest/, '$1');
Timeless0911 marked this conversation as resolved.
Show resolved Hide resolved
}

if (!command?.includes('install')) {
return command;
}

// If command include `install` and package name, replace `install` with `add`
const pureCommand = command
.split(' ')
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.