5180 fichiers 38.5 Mo 0 téléchargements 296 vues mis à jour il y a 6 heures ui
nitro-v36-with-renderer-v36 / src / components / login / hooks / useDraggableLoginWindow.test.tsx
useDraggableLoginWindow.test.tsx 2.7 Ko Télécharger le fichier
import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { FC, useRef } from 'react';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { loginWindowStorageKey, useDraggableLoginWindow } from './useDraggableLoginWindow';

const Harness: FC<{ id: string }> = ({ id }) => {
    const ref = useRef<HTMLDivElement>(null);
    const { style, dragging, handleProps } = useDraggableLoginWindow(id, ref);

    return (
        <div data-testid="window" ref={ref} style={style} className={dragging ? 'is-dragging' : ''}>
            <div data-testid="handle" {...handleProps}>
                <span>Title</span>
                <button type="button">Action</button>
            </div>
        </div>
    );
};

describe('useDraggableLoginWindow', () => {
    beforeEach(() => window.localStorage.clear());
    afterEach(cleanup);

    it('defaults to the styled spot when nothing is stored', () => {
        render(<Harness id="auth" />);

        const win = screen.getByTestId('window');

        expect(win.style.getPropertyValue('--login-drag-x')).toBe('0px');
        expect(win.style.getPropertyValue('--login-drag-y')).toBe('0px');
    });

    it('drags by the handle, stores per window id and restores it', () => {
        render(<Harness id="auth" />);

        const handle = screen.getByTestId('handle');
        const win = screen.getByTestId('window');

        fireEvent.pointerDown(handle, { button: 0, pointerId: 1, clientX: 50, clientY: 50 });
        fireEvent.pointerMove(handle, { pointerId: 1, clientX: 80, clientY: 20 });
        fireEvent.pointerUp(handle, { pointerId: 1, clientX: 80, clientY: 20 });

        expect(win.style.getPropertyValue('--login-drag-x')).toBe('30px');
        expect(win.style.getPropertyValue('--login-drag-y')).toBe('-30px');
        expect(JSON.parse(window.localStorage.getItem(loginWindowStorageKey('auth')))).toEqual({ x: 30, y: -30 });

        cleanup();
        render(<Harness id="auth" />);

        expect(screen.getByTestId('window').style.getPropertyValue('--login-drag-x')).toBe('30px');
    });

    it('does not start a drag from a button inside the handle', () => {
        render(<Harness id="register" />);

        const handle = screen.getByTestId('handle');
        const button = screen.getByRole('button', { name: 'Action' });

        fireEvent.pointerDown(button, { button: 0, pointerId: 1, clientX: 50, clientY: 50 });
        fireEvent.pointerMove(handle, { pointerId: 1, clientX: 90, clientY: 90 });

        expect(screen.getByTestId('window').style.getPropertyValue('--login-drag-x')).toBe('0px');
        expect(window.localStorage.getItem(loginWindowStorageKey('register'))).toBeNull();
    });
});
15Sujets
1Dépôts
20Membres
2En ligne
Profile
Inscrit
Dernière visite
Messages
0
Votes positifs
0
Votes négatifs
0

Nous utilisons des cookies pour assurer le bon fonctionnement du site et améliorer votre expérience. en savoir plus

Autoriser