Skip to content

Commit 63472ee

Browse files
authored
feat(html-report): Keep query when clicking project filter and right click (#37916)
1 parent 9aa5539 commit 63472ee

File tree

6 files changed

+16
-17
lines changed

6 files changed

+16
-17
lines changed

packages/html-reporter/src/filter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,8 @@ function cacheSearchValues(test: TestCaseSummary & { [searchValuesSymbol]?: Sear
213213
// Extract quoted groups of search params, or tokens separated by whitespace
214214
const SEARCH_PARAM_GROUP_REGEX = /("[^"]*"|"[^"]*$|\S+)/g;
215215

216-
export function filterWithQuery(existingQuery: string, token: string, append: boolean): string {
216+
export function filterWithQuery(searchParams: URLSearchParams, token: string, append: boolean): string {
217+
const existingQuery = searchParams.get('q') ?? '';
217218
const tokens = [...existingQuery.matchAll(SEARCH_PARAM_GROUP_REGEX)].map(m => {
218219
const rawValue = m[0];
219220
return rawValue.startsWith('"') && rawValue.endsWith('"') && rawValue.length > 1 ? rawValue.slice(1, rawValue.length - 1) : rawValue;

packages/html-reporter/src/headerView.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,10 @@ const NavLink: React.FC<{
102102
count: number,
103103
}> = ({ token, count }) => {
104104
const searchParams = React.useContext(SearchParamsContext);
105-
const q = searchParams.get('q')?.toString() || '';
106105
const queryToken = `s:${token}`;
107106

108-
const clickUrl = filterWithQuery(q, queryToken, false);
109-
const ctrlClickUrl = filterWithQuery(q, queryToken, true);
107+
const clickUrl = filterWithQuery(searchParams, queryToken, false);
108+
const ctrlClickUrl = filterWithQuery(searchParams, queryToken, true);
110109

111110
const label = token.charAt(0).toUpperCase() + token.slice(1);
112111

packages/html-reporter/src/labels.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ const LabelsClickView: React.FC<{
6060

6161
const onClickHandle = React.useCallback((e: React.MouseEvent, label: string) => {
6262
e.preventDefault();
63-
const q = searchParams.get('q')?.toString() || '';
64-
navigate(filterWithQuery(q, label, e.metaKey || e.ctrlKey));
63+
navigate(filterWithQuery(searchParams, label, e.metaKey || e.ctrlKey));
6564
}, [searchParams]);
6665

6766
return <>

packages/html-reporter/src/links.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { clsx, useFlash } from '@web/uiUtils';
2424
import { trace } from './icons';
2525
import { Expandable } from './expandable';
2626
import { Label } from './labels';
27+
import { filterWithQuery } from './filter';
2728

2829
export function navigate(href: string | URL) {
2930
window.history.pushState({}, '', href);
@@ -61,10 +62,9 @@ export const LinkBadge: React.FunctionComponent<LinkProps & { dim?: boolean }> =
6162
export const ProjectLink: React.FunctionComponent<{
6263
projectNames: string[],
6364
projectName: string,
64-
}> = ({ projectNames, projectName }) => {
65-
const encoded = encodeURIComponent(projectName);
66-
const value = projectName === encoded ? projectName : `"${encoded.replace(/%22/g, '%5C%22')}"`;
67-
return <Link href={`#?q=p:${value}`}>
65+
}> = ({ projectNames, projectName }) => {
66+
const searchParams = React.useContext(SearchParamsContext);
67+
return <Link click={filterWithQuery(searchParams, `p:${projectName}`, false)} ctrlClick={filterWithQuery(searchParams, `p:${projectName}`, true)}>
6868
<Label label={projectName} colorIndex={projectNames.indexOf(projectName) % 6} />
6969
</Link>;
7070
};

packages/html-reporter/src/reportView.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ export const ReportView: React.FC<{
9090
break;
9191
case 'p':
9292
event.preventDefault();
93-
navigate(filterWithQuery(q, 's:passed', false));
93+
navigate(filterWithQuery(searchParams, 's:passed', false));
9494
break;
9595
case 'f':
9696
event.preventDefault();
97-
navigate(filterWithQuery(q, 's:failed', false));
97+
navigate(filterWithQuery(searchParams, 's:failed', false));
9898
break;
9999
case 'ArrowLeft':
100100
if (prev) {
@@ -113,7 +113,7 @@ export const ReportView: React.FC<{
113113

114114
document.addEventListener('keydown', handleKeyDown);
115115
return () => document.removeEventListener('keydown', handleKeyDown);
116-
}, [prev, next, filterParam, q]);
116+
}, [prev, next, filterParam, q, searchParams]);
117117

118118
React.useEffect(() => {
119119
if (reportTitle)

packages/html-reporter/src/testCaseView.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ test('should correctly render prev and next', async ({ mount }) => {
216216
- text: group
217217
- link "« previous"
218218
- link "next »"
219-
- text: "My test test.spec.ts:42 10ms"
219+
- text: "My test test.spec.ts:42 10ms chromium"
220220
`);
221221
});
222222

@@ -241,17 +241,17 @@ const testCaseWithTwoAttempts: TestCase = {
241241
test('total duration is selected run duration', async ({ mount, page }) => {
242242
const component = await mount(<TestCaseView projectNames={['chromium', 'webkit']} testRunMetadata={{}} test={testCaseWithTwoAttempts} prev={undefined} next={undefined} run={0}></TestCaseView>);
243243
await expect(component).toMatchAriaSnapshot(`
244-
- text: "My test test.spec.ts:42 200ms"
244+
- text: "My test test.spec.ts:42 200ms chromium"
245245
- tablist:
246246
- tab "Run 50ms"
247247
- 'tab "Retry #1 150ms"'
248248
`);
249249
await page.getByRole('tab', { name: 'Run' }).click();
250250
await expect(component).toMatchAriaSnapshot(`
251-
- text: "My test test.spec.ts:42 200ms"
251+
- text: "My test test.spec.ts:42 200ms chromium"
252252
`);
253253
await page.getByRole('tab', { name: 'Retry' }).click();
254254
await expect(component).toMatchAriaSnapshot(`
255-
- text: "My test test.spec.ts:42 200ms"
255+
- text: "My test test.spec.ts:42 200ms chromium"
256256
`);
257257
});

0 commit comments

Comments
 (0)