refactor: attachment categories in own function

This commit is contained in:
Simon Knott 2024-10-10 11:10:51 +02:00
parent 77f6dbfec6
commit 468c1215cd
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC

View file

@ -61,6 +61,18 @@ function groupImageDiffs(screenshots: Set<TestAttachment>): ImageDiff[] {
return [...snapshotNameToImageDiff.values()]; return [...snapshotNameToImageDiff.values()];
} }
function getAttachmentCategory(attachment: TestAttachment) {
if (attachment.contentType.startsWith('image/'))
return 'screenshot';
if (attachment.name === 'video')
return 'video';
if (attachment.name === 'trace')
return 'trace';
if (attachment.contentType.startsWith('text/html'))
return 'html';
return 'other';
}
export const TestResultView: React.FC<{ export const TestResultView: React.FC<{
test: TestCase, test: TestCase,
result: TestResult, result: TestResult,
@ -69,12 +81,11 @@ export const TestResultView: React.FC<{
const { screenshots, videos, traces, otherAttachments, diffs, htmls } = React.useMemo(() => { const { screenshots, videos, traces, otherAttachments, diffs, htmls } = React.useMemo(() => {
const attachments = result?.attachments || []; const attachments = result?.attachments || [];
const screenshots = new Set(attachments.filter(a => a.contentType.startsWith('image/'))); const screenshots = new Set(attachments.filter(a => getAttachmentCategory(a) === 'screenshot'));
const videos = attachments.filter(a => a.name === 'video'); const videos = attachments.filter(a => getAttachmentCategory(a) === 'video');
const traces = attachments.filter(a => a.name === 'trace'); const traces = attachments.filter(a => getAttachmentCategory(a) === 'trace');
const htmls = attachments.filter(a => a.contentType.startsWith('text/html')); const htmls = attachments.filter(a => getAttachmentCategory(a) === 'html');
const otherAttachments = new Set<TestAttachment>(attachments); const otherAttachments = attachments.filter(a => getAttachmentCategory(a) === 'other');
[...screenshots, ...videos, ...traces, ...htmls].forEach(a => otherAttachments.delete(a));
const diffs = groupImageDiffs(screenshots); const diffs = groupImageDiffs(screenshots);
return { screenshots: [...screenshots], videos, traces, otherAttachments, diffs, htmls }; return { screenshots: [...screenshots], videos, traces, otherAttachments, diffs, htmls };
}, [result]); }, [result]);
@ -136,7 +147,7 @@ export const TestResultView: React.FC<{
</div>)} </div>)}
</AutoChip>} </AutoChip>}
{!!(otherAttachments.size + htmls.length) && <AutoChip header='Attachments'> {!!(otherAttachments.length + htmls.length) && <AutoChip header='Attachments'>
{[...htmls].map((a, i) => ( {[...htmls].map((a, i) => (
<AttachmentLink key={`html-link-${i}`} attachment={a} openInNewTab />) <AttachmentLink key={`html-link-${i}`} attachment={a} openInNewTab />)
)} )}