Skip to content

Commit 08e38ae

Browse files
FRSgitclaude
andcommitted
fix: format files and add tests for pending-diff tasks to restore coverage
- Run prettier on commands.ts and support.ts (fix lint CI failure) - Add c8 ignore on browser-only template helpers in support.ts - Add unit tests for recordPendingDiffTask, getPendingDiffsTask, and clearPendingDiffsTask to bring function coverage back above 90% Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent d11e0bc commit 08e38ae

3 files changed

Lines changed: 74 additions & 25 deletions

File tree

packages/cypress-plugin-visual-regression-diff/src/commands.ts

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -254,30 +254,27 @@ Cypress.Commands.add(
254254
imgDiffBase64: res.imgDiffBase64 ?? '',
255255
message: res.message ?? '',
256256
};
257-
return (
258-
cy
259-
.task<number>(TASK.recordPendingDiff, record, { log: false })
260-
.then((count) => {
261-
/* c8 ignore start */
262-
if (top) {
263-
const badge = top.document.querySelector(
264-
`.${FAB_BADGE_CLASS}`,
257+
return cy
258+
.task<number>(TASK.recordPendingDiff, record, { log: false })
259+
.then((count) => {
260+
/* c8 ignore start */
261+
if (top) {
262+
const badge = top.document.querySelector(
263+
`.${FAB_BADGE_CLASS}`,
264+
);
265+
if (badge) {
266+
badge.textContent = String(count);
267+
badge.removeAttribute('hidden');
268+
badge.classList.add(`${FAB_BADGE_CLASS}--pulse`);
269+
setTimeout(
270+
() => badge.classList.remove(`${FAB_BADGE_CLASS}--pulse`),
271+
700,
265272
);
266-
if (badge) {
267-
badge.textContent = String(count);
268-
badge.removeAttribute('hidden');
269-
badge.classList.add(`${FAB_BADGE_CLASS}--pulse`);
270-
setTimeout(
271-
() =>
272-
badge.classList.remove(`${FAB_BADGE_CLASS}--pulse`),
273-
700,
274-
);
275-
}
276273
}
277-
/* c8 ignore stop */
278-
return matchImageReturn;
279-
}) as unknown as Cypress.MatchImageReturn
280-
);
274+
}
275+
/* c8 ignore stop */
276+
return matchImageReturn;
277+
}) as unknown as Cypress.MatchImageReturn;
281278
}
282279

283280
throw constructCypressError(log, new Error(res.message));

packages/cypress-plugin-visual-regression-diff/src/support.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import * as Base64 from '@frsource/base64';
22
import './commands';
3-
import { FAB_CLASS, FAB_BADGE_CLASS, LINK_PREFIX, OVERLAY_CLASS, TASK } from './constants';
3+
import {
4+
FAB_CLASS,
5+
FAB_BADGE_CLASS,
6+
LINK_PREFIX,
7+
OVERLAY_CLASS,
8+
TASK,
9+
} from './constants';
410
import type { PendingDiffRecord } from './types';
511

612
const CAROUSEL_CLASS = 'cp-visual-regression-diff-carousel';
@@ -78,11 +84,15 @@ export const generateOverlayTemplate = ({
7884
</div>
7985
</div>`;
8086

87+
/* c8 ignore start */
8188
const generateDiffPanelHTML = ({
8289
imgNewBase64,
8390
imgOldBase64,
8491
imgDiffBase64,
85-
}: Pick<PendingDiffRecord, 'imgNewBase64' | 'imgOldBase64' | 'imgDiffBase64'>) =>
92+
}: Pick<
93+
PendingDiffRecord,
94+
'imgNewBase64' | 'imgOldBase64' | 'imgDiffBase64'
95+
>) =>
8696
`<div style="display:flex;justify-content:space-evenly;align-items:flex-start;gap:15px;flex-wrap:wrap">
8797
<div
8898
style="position:relative;background:#fff;border:solid 15px #fff"
@@ -169,6 +179,7 @@ const generateCarouselTemplate = () =>
169179
<button data-type="next" style="background:transparent;border:1px solid #4a5568;color:#a0aec0;padding:8px 14px;border-radius:4px;cursor:pointer">Next →</button>
170180
</footer>
171181
</div>`;
182+
/* c8 ignore stop */
172183

173184
/* c8 ignore start */
174185
function showFABSuccess() {
@@ -200,7 +211,9 @@ function openCarousel(diffs: PendingDiffRecord[]) {
200211
carouselEl
201212
.find('[data-carousel-counter]')
202213
.text(`Image ${index + 1} of ${diffs.length}`);
203-
carouselEl.find('[data-carousel-title]').text(`${diff.title} – screenshot diff`);
214+
carouselEl
215+
.find('[data-carousel-title]')
216+
.text(`${diff.title} – screenshot diff`);
204217
(carouselEl.find('[data-type="prev"]')[0] as HTMLButtonElement).disabled =
205218
index === 0;
206219
(carouselEl.find('[data-type="next"]')[0] as HTMLButtonElement).disabled =

packages/cypress-plugin-visual-regression-diff/src/task.hook.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ import {
99
getScreenshotPathInfoTask,
1010
CompareImagesCfg,
1111
cleanupImagesTask,
12+
recordPendingDiffTask,
13+
getPendingDiffsTask,
14+
clearPendingDiffsTask,
1215
} from './task.hook';
1316
import { generateScreenshotPath } from './screenshotPath.utils';
1417
import { IMAGE_SNAPSHOT_PREFIX } from './constants';
@@ -383,3 +386,39 @@ describe('doesFileExistsTask', () => {
383386
).toBe(true);
384387
});
385388
});
389+
390+
describe('pendingDiffs tasks', () => {
391+
const record = {
392+
title: 'test title',
393+
imgPath: 'some/path.png',
394+
imgOldPath: 'some/old.png',
395+
imgNewBase64: 'newbase64',
396+
imgOldBase64: 'oldbase64',
397+
imgDiffBase64: 'diffbase64',
398+
message: 'images differ',
399+
};
400+
401+
beforeEach(() => {
402+
clearPendingDiffsTask();
403+
});
404+
405+
it('recordPendingDiffTask adds record and returns count', () => {
406+
expect(recordPendingDiffTask(record)).toBe(1);
407+
expect(recordPendingDiffTask(record)).toBe(2);
408+
});
409+
410+
it('getPendingDiffsTask returns all recorded diffs', () => {
411+
recordPendingDiffTask(record);
412+
recordPendingDiffTask({ ...record, title: 'other' });
413+
const diffs = getPendingDiffsTask();
414+
expect(diffs).toHaveLength(2);
415+
expect(diffs[0]).toEqual(record);
416+
expect(diffs[1].title).toBe('other');
417+
});
418+
419+
it('clearPendingDiffsTask empties the list and returns null', () => {
420+
recordPendingDiffTask(record);
421+
expect(clearPendingDiffsTask()).toBeNull();
422+
expect(getPendingDiffsTask()).toHaveLength(0);
423+
});
424+
});

0 commit comments

Comments
 (0)