Skip to content

armgit / mimik

Public Imported from GitHub
0

🎬 A browser extension and desktop app that turns your clicks into step-by-step guides with annotated screenshots πŸ“–

fix(capture): shoot a checkbox after it flips, not before

davbioa5a16df96eea
diff --git a/src/core/capture/__tests__/click-intercept.test.ts b/src/core/capture/__tests__/click-intercept.test.ts
index 20b78a2..5be5e0e 100644
--- a/src/core/capture/__tests__/click-intercept.test.ts
+++ b/src/core/capture/__tests__/click-intercept.test.ts
@@ -56,8 +56,29 @@ describe('shouldInterceptClick', () => {
expect(shouldInterceptClick(el('<textarea></textarea>'), click())).toBe(false);
});
- it('still intercepts a checkbox, which is a click and not typing', () => {
- expect(shouldInterceptClick(el('<input type="checkbox">'), click())).toBe(true);
+ it('lets native toggles through, since holding one back shoots the state it is leaving', () => {
+ expect(shouldInterceptClick(el('<input type="checkbox">'), click())).toBe(false);
+ expect(shouldInterceptClick(el('<input type="radio">'), click())).toBe(false);
+ });
+
+ it('lets aria toggles through the same way', () => {
+ expect(shouldInterceptClick(el('<div role="checkbox" tabindex="0"></div>'), click())).toBe(false);
+ expect(shouldInterceptClick(el('<button role="switch"></button>'), click())).toBe(false);
+ expect(shouldInterceptClick(el('<div role="radio" tabindex="0"></div>'), click())).toBe(false);
+ });
+
+ it('still holds a checkbox inside a menu, which would close before the shot and take the box with it', () => {
+ const menu = el('<div role="menu"><div role="menuitem" tabindex="0"><input type="checkbox"></div></div>');
+ expect(shouldInterceptClick(menu.querySelector('input') as HTMLElement, click())).toBe(true);
+ });
+
+ it('lets a checkbox in a listbox through, since a multi-select stays open', () => {
+ const list = el('<div role="listbox"><div role="option"><input type="checkbox"></div></div>');
+ expect(shouldInterceptClick(list.querySelector('input') as HTMLElement, click())).toBe(false);
+ });
+
+ it('still holds a checkable menu item, whose menu closes on the click', () => {
+ expect(shouldInterceptClick(el('<div role="menuitemcheckbox" tabindex="0"></div>'), click())).toBe(true);
});
});
diff --git a/src/core/capture/dom/element-utils.ts b/src/core/capture/dom/element-utils.ts
index 2bb6934..b89ccba 100644
--- a/src/core/capture/dom/element-utils.ts
+++ b/src/core/capture/dom/element-utils.ts
@@ -1,5 +1,5 @@
export const FOCUSABLE_SELECTOR =
- 'a[href], button, input, select, textarea, [role="button"], [role="link"], [role="tab"], [role="menuitem"], [role="checkbox"], [role="radio"], [role="option"], [tabindex], [contenteditable="true"]';
+ 'a[href], button, input, select, textarea, [role="button"], [role="link"], [role="tab"], [role="menuitem"], [role="checkbox"], [role="radio"], [role="switch"], [role="option"], [tabindex], [contenteditable="true"]';
const MAX_ELEMENT_RATIO = 0.8;
@@ -45,6 +45,13 @@ export function isTextField(el: Element): boolean {
return el instanceof HTMLTextAreaElement || (el instanceof HTMLElement && el.isContentEditable);
}
+const TOGGLE_ROLES = new Set(['checkbox', 'radio', 'switch']);
+
+export function isToggle(el: Element): boolean {
+ if (el instanceof HTMLInputElement && (el.type === 'checkbox' || el.type === 'radio')) return true;
+ return TOGGLE_ROLES.has(el.getAttribute('role') ?? '');
+}
+
export function isNavigatingClick(el: HTMLElement): boolean {
const anchor = el.closest('a[href]');
if (!anchor) return false;
diff --git a/src/core/capture/events/__tests__/click-replay.test.ts b/src/core/capture/events/__tests__/click-replay.test.ts
index e5abded..5d184f6 100644
--- a/src/core/capture/events/__tests__/click-replay.test.ts
+++ b/src/core/capture/events/__tests__/click-replay.test.ts
@@ -157,3 +157,175 @@ describe('click interception', () => {
expect(seen).toBe(1);
});
});
+
+describe('toggles', () => {
+ function checkbox(checked = false): HTMLInputElement {
+ const box = place('input') as HTMLInputElement;
+ box.type = 'checkbox';
+ box.checked = checked;
+ return box;
+ }
+
+ function checkedAtCapture(box: HTMLInputElement): boolean[] {
+ const seen: boolean[] = [];
+ vi.mocked(sendMessage).mockImplementation((async (type: string) => {
+ if (type === 'captureStep') seen.push(box.checked);
+ return { stepId: 'step-1' };
+ }) as unknown as typeof sendMessage);
+ return seen;
+ }
+
+ it('screenshots a checkbox once it is checked, not in the state it is leaving', async () => {
+ const box = checkbox();
+ const seen = checkedAtCapture(box);
+
+ userClick(box);
+ await settle();
+
+ expect(seen).toEqual([true]);
+ expect(box.checked).toBe(true);
+ });
+
+ it('screenshots an unchecking click as unchecked', async () => {
+ const box = checkbox(true);
+ const seen = checkedAtCapture(box);
+
+ userClick(box);
+ await settle();
+
+ expect(seen).toEqual([false]);
+ expect(box.checked).toBe(false);
+ });
+
+ it('records a click on the label once, with the box it drives already flipped', async () => {
+ const label = place('label');
+ label.innerHTML = '<input type="checkbox"><span>Remember me</span>';
+ const box = label.querySelector('input') as HTMLInputElement;
+ const seen = checkedAtCapture(box);
+
+ userClick(label.querySelector('span') as HTMLElement);
+ await settle();
+
+ expect(seen).toEqual([true]);
+ expect(box.checked).toBe(true);
+ });
+
+ it('shoots a checkbox in a closing menu while the menu is still open', async () => {
+ const menu = place('div');
+ menu.setAttribute('role', 'menu');
+ menu.innerHTML = '<div role="menuitem" tabindex="0"><input type="checkbox"> Show hidden files</div>';
+ const item = menu.firstElementChild as HTMLElement;
+ item.addEventListener('click', () => {
+ menu.hidden = true;
+ });
+ const box = menu.querySelector('input') as HTMLInputElement;
+ const seen: Array<{ menuOpen: boolean }> = [];
+ vi.mocked(sendMessage).mockImplementation((async () => {
+ seen.push({ menuOpen: !menu.hidden });
+ return { stepId: 'step-1' };
+ }) as unknown as typeof sendMessage);
+
+ userClick(box);
+ await settle();
+
+ expect(seen).toEqual([{ menuOpen: true }]);
+ expect(box.checked).toBe(true);
+ expect(menu.hidden).toBe(true);
+ });
+
+ it('treats a click on a switch knob as a click on the switch', async () => {
+ const toggle = place('div');
+ toggle.setAttribute('role', 'switch');
+ toggle.setAttribute('aria-checked', 'false');
+ toggle.innerHTML = '<span></span>';
+ toggle.addEventListener('click', () => toggle.setAttribute('aria-checked', 'true'));
+ const seen: Array<string | null> = [];
+ vi.mocked(sendMessage).mockImplementation((async () => {
+ seen.push(toggle.getAttribute('aria-checked'));
+ return { stepId: 'step-1' };
+ }) as unknown as typeof sendMessage);
+
+ userClick(toggle.querySelector('span') as HTMLElement);
+ await settle();
+
+ expect(seen).toEqual(['true']);
+ });
+
+ it('lets a switch its own script flips through, so the shot shows it on', async () => {
+ const toggle = place('button');
+ toggle.setAttribute('role', 'switch');
+ toggle.setAttribute('aria-checked', 'false');
+ toggle.addEventListener('click', () => toggle.setAttribute('aria-checked', 'true'));
+ const seen: Array<string | null> = [];
+ vi.mocked(sendMessage).mockImplementation((async () => {
+ seen.push(toggle.getAttribute('aria-checked'));
+ return { stepId: 'step-1' };
+ }) as unknown as typeof sendMessage);
+
+ userClick(toggle);
+ await settle();
+
+ expect(seen).toEqual(['true']);
+ });
+});
+
+describe('inside a frame', () => {
+ let frameX = 981;
+ const parent = {
+ postMessage: (ask: { id: string }) => {
+ const x = frameX;
+ setTimeout(() =>
+ window.dispatchEvent(
+ new MessageEvent('message', {
+ data: { mimikFramePlacement: 'answer', id: ask.id, x, y: 527, scale: 1 },
+ source: parent as unknown as Window,
+ }),
+ ),
+ );
+ },
+ };
+
+ beforeEach(() => {
+ frameX = 981;
+ Object.defineProperty(window, 'parent', { configurable: true, value: parent });
+ });
+
+ afterEach(() => {
+ Object.defineProperty(window, 'parent', { configurable: true, value: window });
+ });
+
+ function sent(type: string) {
+ return vi.mocked(sendMessage).mock.calls.find((c) => c[0] === type)?.[1];
+ }
+
+ it('sends the rect and click point where the tab shows them, not where the frame does', async () => {
+ userClick(place('button'));
+ await settle();
+
+ expect(sent('captureStep')).toMatchObject({
+ elementMeta: { rect: { x: 985, y: 533, width: 120, height: 40 }, clickPoint: { x: 991, y: 547 } },
+ });
+ });
+
+ it('asks where the frame is when the shot is taken, since the page may have scrolled since the click', async () => {
+ userClick(place('button'));
+ frameX = 1000;
+ await settle();
+
+ expect(sent('captureStep')).toMatchObject({ elementMeta: { rect: { x: 1004 } } });
+ });
+
+ it('places both the first and the final typing screenshot in the tab', async () => {
+ vi.mocked(sendMessage).mockImplementation((async () => ({ stepId: 'step-1' })) as unknown as typeof sendMessage);
+ const field = place('input') as HTMLInputElement;
+ field.type = 'text';
+
+ userClick(field);
+ await settle();
+ await handle.stop();
+
+ const placed = { elementMeta: { rect: { x: 985, y: 533, width: 120, height: 40 } } };
+ expect(sent('captureStep')).toMatchObject(placed);
+ expect(sent('finalizeInputStep')).toMatchObject(placed);
+ });
+});
diff --git a/src/core/capture/events/click-intercept.ts b/src/core/capture/events/click-intercept.ts
index dd3c501..8ed1d2f 100644
--- a/src/core/capture/events/click-intercept.ts
+++ b/src/core/capture/events/click-intercept.ts
@@ -1,4 +1,6 @@
-import { isTextField } from '../dom/element-utils';
+import { isTextField, isToggle } from '../dom/element-utils';
+
+const MENU_SELECTOR = '[role="menu"], [role="menubar"]';
const replayed = new WeakSet<Event>();
@@ -10,6 +12,7 @@ export function shouldInterceptClick(target: HTMLElement, event: MouseEvent): bo
if (!event.isTrusted || event.shiftKey) return false;
if (target instanceof HTMLSelectElement || target instanceof HTMLOptionElement) return false;
if (target.isContentEditable) return false;
+ if (isToggle(target) && !target.closest(MENU_SELECTOR)) return false;
return !isTextField(target);
}