From c262a80762dc4c4a9d01fc36897527988a9e1bad Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Fri, 1 Apr 2022 11:53:56 -0800 Subject: [PATCH] chore: extract image diff component (#13221) --- packages/html-reporter/src/imageDiffView.tsx | 65 +++++++++++++++++++ packages/html-reporter/src/testResultView.tsx | 46 +------------ 2 files changed, 66 insertions(+), 45 deletions(-) create mode 100644 packages/html-reporter/src/imageDiffView.tsx diff --git a/packages/html-reporter/src/imageDiffView.tsx b/packages/html-reporter/src/imageDiffView.tsx new file mode 100644 index 0000000000..6b4b786d7f --- /dev/null +++ b/packages/html-reporter/src/imageDiffView.tsx @@ -0,0 +1,65 @@ +/* + Copyright (c) Microsoft Corporation. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +import type { TestAttachment } from '@playwright-test/reporters/html'; +import * as React from 'react'; +import { AttachmentLink } from './links'; +import { TabbedPane } from './tabbedPane'; +import './testResultView.css'; + +export type ImageDiff = { + name: string, + left?: { attachment: TestAttachment, title: string }, + right?: { attachment: TestAttachment, title: string }, + diff?: { attachment: TestAttachment, title: string }, +}; + +export const ImageDiffView: React.FunctionComponent<{ + imageDiff: ImageDiff, +}> = ({ imageDiff: diff }) => { + // Pre-select a tab called "actual", if any. + const [selectedTab, setSelectedTab] = React.useState('left'); + const diffElement = React.useRef(null); + const setMinHeight = () => { + if (diffElement.current) + diffElement.current.style.minHeight = diffElement.current.offsetHeight + 'px'; + }; + const tabs = [ + { + id: 'left', + title: diff.left!.title, + render: () => + }, + { + id: 'right', + title: diff.right!.title, + render: () => + }, + ]; + if (diff.diff) { + tabs.push({ + id: 'diff', + title: diff.diff.title, + render: () => + }); + } + return
+ + + + {diff.diff && } +
; +}; diff --git a/packages/html-reporter/src/testResultView.tsx b/packages/html-reporter/src/testResultView.tsx index ba38b94d97..88fed67144 100644 --- a/packages/html-reporter/src/testResultView.tsx +++ b/packages/html-reporter/src/testResultView.tsx @@ -18,21 +18,14 @@ import type { TestAttachment, TestCase, TestResult, TestStep } from '@playwright import ansi2html from 'ansi-to-html'; import * as React from 'react'; import { TreeItem } from './treeItem'; -import { TabbedPane } from './tabbedPane'; import { msToString } from './uiUtils'; import { AutoChip } from './chip'; import { traceImage } from './images'; import { AttachmentLink } from './links'; import { statusIcon } from './statusIcon'; +import { ImageDiff, ImageDiffView } from './imageDiffView'; import './testResultView.css'; -type ImageDiff = { - name: string, - left?: { attachment: TestAttachment, title: string }, - right?: { attachment: TestAttachment, title: string }, - diff?: { attachment: TestAttachment, title: string }, -}; - function groupImageDiffs(screenshots: Set): ImageDiff[] { const snapshotNameToImageDiff = new Map(); for (const attachment of screenshots) { @@ -148,43 +141,6 @@ const StepTreeItem: React.FC<{ } : undefined} depth={depth}>; }; -const ImageDiffView: React.FunctionComponent<{ - imageDiff: ImageDiff, -}> = ({ imageDiff: diff }) => { - // Pre-select a tab called "actual", if any. - const [selectedTab, setSelectedTab] = React.useState('left'); - const diffElement = React.useRef(null); - const setMinHeight = () => { - if (diffElement.current) - diffElement.current.style.minHeight = diffElement.current.offsetHeight + 'px'; - }; - const tabs = [ - { - id: 'left', - title: diff.left!.title, - render: () => - }, - { - id: 'right', - title: diff.right!.title, - render: () => - }, - ]; - if (diff.diff) { - tabs.push({ - id: 'diff', - title: diff.diff.title, - render: () => - }); - } - return
- - - - {diff.diff && } -
; -}; - const ErrorMessage: React.FC<{ error: string; }> = ({ error }) => {