Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

test(image-map): drive whole-value strength edits without the page keyboard

Lincoln Steinb4597610834f
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
index 8676c738fa..fffcb111f0 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
+++ b/invokeai/frontend/webv2/src/workbench/widgets/image-map/ClusterStrengthField.browser.test.tsx
@@ -62,13 +62,42 @@ let root: Root | null = null;
(globalThis as typeof globalThis & { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
/** Longer than the field's own debounce, so a commit has certainly fired. */
+const SETTLE_MS = 900;
+
+/**
+ * Real-time settle, for the tests that type through the real keyboard. Everything else fakes the clock (see
+ * `setValue`): this file runs beside other browser test files that share the page's focus, and seconds of real
+ * typing and waiting here were enough to knock their popovers and menus closed on CI.
+ */
const settle = () =>
act(async () => {
await new Promise<void>((resolve) => {
- globalThis.setTimeout(resolve, 900);
+ globalThis.setTimeout(resolve, SETTLE_MS);
});
});
+const settleFake = () =>
+ act(() => {
+ vi.advanceTimersByTime(SETTLE_MS);
+ });
+
+const setNativeValue = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value')!.set!;
+
+/**
+ * A whole-value edit (paste, fill, or clear) without driving the page's keyboard: the native setter plus an `input`
+ * event is what React's onChange observes. Keystroke-level behaviour — a `type="number"` box's `badInput`, partial
+ * numbers, typing after a refill — still goes through `userEvent`, since only real keys produce it.
+ */
+const setValue = async (value: string) => {
+ const input = spinner();
+
+ await act(() => {
+ input.focus();
+ setNativeValue.call(input, value);
+ input.dispatchEvent(new Event('input', { bubbles: true }));
+ });
+};
+
const field = imageMapSettingsContribution.fields.find((entry) => entry.id === 'clusterEps')!;
/** `resolved` is what the server says it clustered with. */
@@ -129,6 +158,7 @@ beforeEach(() => {
});
afterEach(async () => {
+ vi.useRealTimers();
await act(() => root?.unmount());
host?.remove();
host = null;
@@ -145,12 +175,13 @@ describe('ClusterStrengthField', () => {
});
it('commits a typed strength once, after the edit settles', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
+ await setValue('0.25');
expect(settings.patch).not.toHaveBeenCalled();
- await settle();
+ await settleFake();
expect(settings.patch).toHaveBeenCalledTimes(1);
expect(settings.patch).toHaveBeenCalledWith({ clusterEps: 0.25 });
@@ -158,13 +189,14 @@ describe('ClusterStrengthField', () => {
});
it('hands the choice back to the heuristic when cleared', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
- await settle();
+ await setValue('0.25');
+ await settleFake();
settings.patch.mockClear();
- await userEvent.clear(spinner());
- await settle();
+ await setValue('');
+ await settleFake();
expect(settings.patch).toHaveBeenCalledWith({ clusterEps: null });
// And the box refills with the derived value rather than staying empty.
@@ -176,10 +208,11 @@ describe('ClusterStrengthField', () => {
// `type="number"` reports an empty value for "0." too. Reading that as
// "use the heuristic" would throw the user's setting away mid-keystroke.
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
+ await setValue('0.25');
await settle();
settings.patch.mockClear();
+ // Cleared by key, not by setValue: the empty box and the "." are one keystroke sequence here.
await userEvent.clear(spinner());
await userEvent.type(spinner(), '.');
// The box reads as empty here, which is the whole trap.
@@ -195,7 +228,7 @@ describe('ClusterStrengthField', () => {
// "0.15" passes through "0", which is below the minimum. Validating per
// keystroke would flash a range error at a user typing a fine value.
await render(0.0945);
- await userEvent.clear(spinner());
+ await setValue('');
await userEvent.type(spinner(), '0.15');
expect(host?.textContent).not.toContain('clusterStrengthRange');
@@ -206,13 +239,14 @@ describe('ClusterStrengthField', () => {
});
it('refills the box when cleared from the heuristic it was already on', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
// Nothing was chosen, so committing null changes no state and the sync
// effect never fires. Without an explicit refill the box sits empty while
// the map clusters at a strength the control is no longer showing.
await render(0.0945);
- await userEvent.clear(spinner());
- await settle();
+ await setValue('');
+ await settleFake();
expect(settings.patch).toHaveBeenCalledWith({ clusterEps: null });
expect(spinner().value).toBe('0.0945');
@@ -224,12 +258,12 @@ describe('ClusterStrengthField', () => {
// still in it. Without selecting the refilled text, typing "0.5" next
// lands "0.09450.5" and the map reclusters at a number nobody chose.
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
+ await setValue('0.25');
await settle();
settings.patch.mockClear();
spinner().focus();
- await userEvent.clear(spinner());
+ await setValue('');
await settle();
expect(spinner().value).toBe('0.0945');
@@ -241,20 +275,22 @@ describe('ClusterStrengthField', () => {
});
it('keeps every digit of a value with more precision than the box shows', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
// forDisplay trims to three significant digits. Rewriting the box with
// the trimmed value would delete the keystroke that produced it.
await render(0.0945);
- await userEvent.fill(spinner(), '0.1234');
- await settle();
+ await setValue('0.1234');
+ await settleFake();
expect(settings.patch).toHaveBeenCalledWith({ clusterEps: 0.1234 });
expect(spinner().value).toBe('0.1234');
});
it('commits an edit the dialog closes on top of', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
+ await setValue('0.25');
// Unmounted inside the debounce window, which is what closing the
// settings dialog does if the field never blurs.
@@ -265,8 +301,9 @@ describe('ClusterStrengthField', () => {
});
it('commits an edit the user clicks away from', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await render(0.0945);
- await userEvent.fill(spinner(), '0.25');
+ await setValue('0.25');
await act(() => {
spinner().blur();
});
@@ -275,10 +312,11 @@ describe('ClusterStrengthField', () => {
});
it('refuses a strength the endpoint would reject', async () => {
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] });
await render(0.0945);
- await userEvent.fill(spinner(), '9');
- await settle();
+ await setValue('9');
+ await settleFake();
expect(settings.patch).not.toHaveBeenCalled();
expect(host?.textContent).toContain('clusterStrengthRange');