Skip to content

Conversation

@bar-goldenberg
Copy link

No description provided.

Comment on lines 126 to 128
clearMediaQueryListenersForInteraction(interactionId: string): void {
this._removeMediaQueryListener(interactionId);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably delete this one

mql.removeEventListener('change', handler);
this.mediaQueryListeners.delete(interactionId);
}
this.clearMediaQueryListenersForInteraction(interactionId);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
this.clearMediaQueryListenersForInteraction(interactionId);
this._removeMediaQueryListener(interactionId);

Comment on lines 18 to 21
function _reconcile(root: IInteractElement, key: string): void {
remove(key);
add(root, key);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make this function a static method of Interact, can be named update. This should solve the import problem

id: string,
mql: MediaQueryList,
key: string,
handler: () => void,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace this param with sourceRoot, and the handler can be hard-coded here as interact.update(sourceRoot, sourceKey)

return;
}

mql.addEventListener('change', handler);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
mql.addEventListener('change', handler);
mql.addEventListener('change', () => Interact.update(sourceRoot, sourceKey));

add(root, key);
}

function _setupMediaQueryListener(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since all the logic here is tied to the instance, I suggest moving this function to be a method of the Interact instance itself. This should simplify everything, I think

let matchMediaMock: ReturnType<typeof vi.fn>;

// Helper to create mock MediaQueryList objects
const createMockMQL = (query: string, initialMatches = false) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mock already exists at the top.
Also, since we're running in JSDOM env we may be able to remove some of the mocks

Comment on lines 2184 to 2190
matchMediaMock = vi.fn().mockImplementation((query: string) => createMockMQL(query));

Object.defineProperty(window, 'matchMedia', {
writable: true,
configurable: true,
value: matchMediaMock,
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already has a mock at the top, so first thing would be to reuse that.

listenerEntry!.handler(mockEvent);

// Verify reconcile was triggered (clearInteractionStateForKey is called during reconcile)
expect(clearStateSpy).toHaveBeenCalledWith('responsive-button');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You actually have a spy on a method that's a part of the inner implementation.
If we're doing a unit test it's better to check that the top level function that's in charge of this logic to be invoked. This case the _reconcile function itself. If this becomes a method of Interact it will be more testable and we can observe that directly.

};

beforeEach(() => {
Interact.destroy();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't be necessary

Suggested change
Interact.destroy();

Base automatically changed from create-project-interact to master November 26, 2025 13:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants