@yoursurprise/slider 3.6.0-beta.1 → 3.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/Slider.test.tsx +104 -0
package/package.json
CHANGED
package/src/Slider.test.tsx
CHANGED
|
@@ -644,5 +644,109 @@ describe('Slider', () => {
|
|
|
644
644
|
expect(ref.current?.getLastFullyVisibleSlideIndex()).toBe(2);
|
|
645
645
|
});
|
|
646
646
|
});
|
|
647
|
+
|
|
648
|
+
describe('single slide', () => {
|
|
649
|
+
type SetupOptions = { scrollLeft?: number; scrollTop?: number; orientation?: Orientation };
|
|
650
|
+
|
|
651
|
+
const setup = ({ scrollLeft = 0, scrollTop = 0, orientation = Orientation.HORIZONTAL }: SetupOptions = {}) => {
|
|
652
|
+
renderSliderWithDimensions({ scrollLeft, scrollTop }, { singleSlideView: true, orientation });
|
|
653
|
+
|
|
654
|
+
const [intersectionCallback] = getIntersectionObserverInstance();
|
|
655
|
+
const slides = screen.getAllByRole('listitem');
|
|
656
|
+
const scrollElement = screen.getByRole('list');
|
|
657
|
+
|
|
658
|
+
slides.forEach((child, i) => {
|
|
659
|
+
Object.defineProperty(child, 'clientWidth', { configurable: true, value: 1000 });
|
|
660
|
+
Object.defineProperty(child, 'offsetLeft', { value: 1000 * i });
|
|
661
|
+
Object.defineProperty(child, 'clientHeight', { configurable: true, value: 1000 });
|
|
662
|
+
Object.defineProperty(child, 'offsetTop', { value: 1000 * i });
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
return { intersectionCallback, slides, scrollElement };
|
|
666
|
+
};
|
|
667
|
+
|
|
668
|
+
it('snaps to the next slide after dragging past the threshold', async () => {
|
|
669
|
+
const { intersectionCallback, slides, scrollElement } = setup();
|
|
670
|
+
|
|
671
|
+
act(() => {
|
|
672
|
+
intersectionCallback([
|
|
673
|
+
{ intersectionRatio: 1, target: slides[0] } as unknown as IntersectionObserverEntry,
|
|
674
|
+
], mockIntersectionObserver.mock.instances[0]);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
act(() => fireEvent.mouseDown(scrollElement));
|
|
678
|
+
act(() => fireEvent.mouseMove(scrollElement, { clientX: -10, clientY: 0 }));
|
|
679
|
+
act(() => fireEvent.mouseUp(scrollElement));
|
|
680
|
+
|
|
681
|
+
await waitFor(() => {
|
|
682
|
+
expect(scrollToSpy).toHaveBeenCalledWith({ behavior: 'smooth', left: 1000 });
|
|
683
|
+
});
|
|
684
|
+
});
|
|
685
|
+
|
|
686
|
+
it('snaps to the previous slide after dragging past the threshold', async () => {
|
|
687
|
+
const { intersectionCallback, slides, scrollElement } = setup({ scrollLeft: 1000 });
|
|
688
|
+
|
|
689
|
+
act(() => {
|
|
690
|
+
intersectionCallback([
|
|
691
|
+
{ intersectionRatio: 1, target: slides[1] } as unknown as IntersectionObserverEntry,
|
|
692
|
+
], mockIntersectionObserver.mock.instances[0]);
|
|
693
|
+
});
|
|
694
|
+
|
|
695
|
+
act(() => fireEvent.mouseDown(scrollElement));
|
|
696
|
+
act(() => fireEvent.mouseMove(scrollElement, { clientX: 10, clientY: 0 }));
|
|
697
|
+
act(() => fireEvent.mouseUp(scrollElement));
|
|
698
|
+
|
|
699
|
+
await waitFor(() => {
|
|
700
|
+
expect(scrollToSpy).toHaveBeenCalledWith({ behavior: 'smooth', left: 0 });
|
|
701
|
+
});
|
|
702
|
+
});
|
|
703
|
+
|
|
704
|
+
it('snaps to the next slide after dragging past the threshold vertically', async () => {
|
|
705
|
+
const { intersectionCallback, slides, scrollElement } = setup({ orientation: Orientation.VERTICAL });
|
|
706
|
+
|
|
707
|
+
act(() => {
|
|
708
|
+
intersectionCallback([
|
|
709
|
+
{ intersectionRatio: 1, target: slides[0] } as unknown as IntersectionObserverEntry,
|
|
710
|
+
], mockIntersectionObserver.mock.instances[0]);
|
|
711
|
+
});
|
|
712
|
+
|
|
713
|
+
act(() => fireEvent.mouseDown(scrollElement));
|
|
714
|
+
act(() => fireEvent.mouseMove(scrollElement, { clientX: 0, clientY: -10 }));
|
|
715
|
+
act(() => fireEvent.mouseUp(scrollElement));
|
|
716
|
+
|
|
717
|
+
await waitFor(() => {
|
|
718
|
+
expect(scrollToSpy).toHaveBeenCalledWith({ behavior: 'smooth', top: 1000 });
|
|
719
|
+
});
|
|
720
|
+
});
|
|
721
|
+
|
|
722
|
+
it('snaps to the previous slide after dragging past the threshold vertically', async () => {
|
|
723
|
+
const { intersectionCallback, slides, scrollElement } = setup({ scrollTop: 1000, orientation: Orientation.VERTICAL });
|
|
724
|
+
|
|
725
|
+
act(() => {
|
|
726
|
+
intersectionCallback([
|
|
727
|
+
{ intersectionRatio: 1, target: slides[1] } as unknown as IntersectionObserverEntry,
|
|
728
|
+
], mockIntersectionObserver.mock.instances[0]);
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
act(() => fireEvent.mouseDown(scrollElement));
|
|
732
|
+
act(() => fireEvent.mouseMove(scrollElement, { clientX: 0, clientY: 10 }));
|
|
733
|
+
act(() => fireEvent.mouseUp(scrollElement));
|
|
734
|
+
|
|
735
|
+
await waitFor(() => {
|
|
736
|
+
expect(scrollToSpy).toHaveBeenCalledWith({ behavior: 'smooth', top: 0 });
|
|
737
|
+
});
|
|
738
|
+
});
|
|
739
|
+
|
|
740
|
+
it('does not snap when drag is below the threshold', () => {
|
|
741
|
+
const { scrollElement } = setup();
|
|
742
|
+
|
|
743
|
+
act(() => fireEvent.mouseDown(scrollElement));
|
|
744
|
+
act(() => fireEvent.mouseMove(scrollElement, { clientX: -3, clientY: 0 }));
|
|
745
|
+
act(() => fireEvent.mouseUp(scrollElement));
|
|
746
|
+
|
|
747
|
+
expect(scrollToSpy).not.toHaveBeenCalled();
|
|
748
|
+
});
|
|
749
|
+
});
|
|
750
|
+
|
|
647
751
|
});
|
|
648
752
|
|