Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/UniqueProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ export interface UniqueProviderProps {
postTriggerProps?: (options: UniqueShowOptions) => UniqueShowOptions;
}

const UniqueProvider = ({ children, postTriggerProps }: UniqueProviderProps) => {
const UniqueProvider = ({
children,
postTriggerProps,
}: UniqueProviderProps) => {
const [trigger, open, options, onTargetVisibleChanged] = useTargetState();

// ========================== Options ===========================
Expand Down Expand Up @@ -144,6 +147,11 @@ const UniqueProvider = ({ children, postTriggerProps }: UniqueProviderProps) =>
[],
);

// =========================== Align ============================
React.useEffect(() => {
onAlign();
}, [mergedOptions?.target]);

// =========================== Motion ===========================
const onPrepare = useEvent(() => {
onAlign();
Expand Down
79 changes: 79 additions & 0 deletions tests/unique.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ describe('Trigger.Unique', () => {
afterEach(() => {
cleanup();
jest.useRealTimers();
jest.restoreAllMocks();
});

it('moving will not hide the popup', async () => {
Expand Down Expand Up @@ -285,4 +286,82 @@ describe('Trigger.Unique', () => {
'custom-post-options-class',
);
});

it('should call onAlign when target changes', async () => {
const mockOnAlign = jest.fn();

// Mock useAlign to return our mock onAlign function
const useAlignModule = require('../src/hooks/useAlign');
const originalUseAlign = useAlignModule.default;

jest.spyOn(useAlignModule, 'default').mockImplementation((...args) => {
const originalResult = originalUseAlign(...args);
// Replace onAlign with our mock
return [
originalResult[0], // ready
originalResult[1], // offsetX
originalResult[2], // offsetY
originalResult[3], // offsetR
originalResult[4], // offsetB
originalResult[5], // arrowX
originalResult[6], // arrowY
originalResult[7], // scaleX
originalResult[8], // scaleY
originalResult[9], // alignInfo
mockOnAlign, // onAlign - our mock function
];
});
Comment on lines +297 to +313
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The mock implementation for useAlign is quite verbose. While the comments are helpful, the approach of manually listing each element of the returned array is brittle if the hook's return signature changes. You can make this more concise and maintainable.

    jest.spyOn(useAlignModule, 'default').mockImplementation((...args) => {
      const result = originalUseAlign(...args);
      // The `onAlign` function is the 11th element in the returned array (index 10).
      return [...result.slice(0, 10), mockOnAlign];
    });


// Test component with two controlled triggers
const TestComponent = () => {
const [trigger1Open, setTrigger1Open] = React.useState(true);
const [trigger2Open, setTrigger2Open] = React.useState(false);

return (
<div>
<button
className="switch-trigger-btn"
onClick={() => {
// Switch which trigger is open - this changes the target
setTrigger1Open(!trigger1Open);
setTrigger2Open(!trigger2Open);
}}
>
Switch Trigger
</button>
<UniqueProvider>
<Trigger
popupVisible={trigger1Open}
popup={<div>Trigger 1 Popup</div>}
unique
>
<div className="trigger-1">Trigger 1</div>
</Trigger>
<Trigger
popupVisible={trigger2Open}
popup={<div>Trigger 2 Popup</div>}
unique
>
<div className="trigger-2">Trigger 2</div>
</Trigger>
</UniqueProvider>
</div>
);
};

const { container } = render(<TestComponent />);

// Wait for initial render
await awaitFakeTimer();

// Clear any initial calls
mockOnAlign.mockClear();

// Switch triggers - this should change the target and call onAlign
fireEvent.click(container.querySelector('.switch-trigger-btn'));
await awaitFakeTimer();

// Verify onAlign was called due to target change
expect(mockOnAlign).toHaveBeenCalled();
});
});
Loading