84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { render, screen } from '@testing-library/react';
|
|
import userEvent from '@testing-library/user-event';
|
|
import { describe, it, expect, afterEach } from 'vitest';
|
|
import { ExerciseEditor } from '../src/pages/plans/plan-editor/ExerciseEditor';
|
|
import type { EditorExercise } from '../src/pages/plans/plan-editor/types';
|
|
|
|
const LONG_NAME = 'Wiosłowanie sztangą nachwytem w pełnym opadzie tułowia';
|
|
|
|
const EXERCISE: EditorExercise = {
|
|
exerciseId: 'ex-1',
|
|
sets: [{ reps: '10', loadKg: '50', restSec: '90', rir: '2' }],
|
|
};
|
|
|
|
function renderEditor() {
|
|
return render(
|
|
<ExerciseEditor
|
|
exercise={EXERCISE}
|
|
exerciseMeta={undefined}
|
|
exerciseName={() => LONG_NAME}
|
|
isTraineeMode={false}
|
|
resultsOpen={false}
|
|
onToggleResults={() => {}}
|
|
onRemove={() => {}}
|
|
onMoveUp={() => {}}
|
|
onMoveDown={() => {}}
|
|
canMoveUp={false}
|
|
canMoveDown={false}
|
|
onSetField={() => {}}
|
|
onAddSetCopy={() => {}}
|
|
onRemoveSet={() => {}}
|
|
latest={undefined}
|
|
points={[]}
|
|
onPrefill={() => {}}
|
|
/>,
|
|
);
|
|
}
|
|
|
|
/** Symulacja realnego przycięcia tekstu: scrollWidth > clientWidth (jsdom zwraca same 0). */
|
|
function mockTruncated(truncated: boolean) {
|
|
Object.defineProperty(Element.prototype, 'scrollWidth', {
|
|
configurable: true,
|
|
get: () => (truncated ? 400 : 0),
|
|
});
|
|
}
|
|
|
|
const scrollWidthDescriptor = Object.getOwnPropertyDescriptor(Element.prototype, 'scrollWidth');
|
|
|
|
afterEach(() => {
|
|
if (scrollWidthDescriptor) {
|
|
Object.defineProperty(Element.prototype, 'scrollWidth', scrollWidthDescriptor);
|
|
} else {
|
|
delete (Element.prototype as unknown as Record<string, unknown>).scrollWidth;
|
|
}
|
|
});
|
|
|
|
describe('ExerciseEditor — rozwijanie długiej nazwy ćwiczenia', () => {
|
|
it('bez przycięcia renderuje samą nazwę (bez przycisku)', () => {
|
|
mockTruncated(false);
|
|
renderEditor();
|
|
expect(screen.getByText(LONG_NAME)).toBeInTheDocument();
|
|
expect(screen.queryByRole('button', { name: LONG_NAME })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it('po przycięciu nazwa staje się przyciskiem z title i rozwija się po kliknięciu', async () => {
|
|
const user = userEvent.setup();
|
|
mockTruncated(true);
|
|
renderEditor();
|
|
|
|
const btn = screen.getByRole('button', { name: LONG_NAME });
|
|
expect(btn).toHaveAttribute('title', LONG_NAME);
|
|
expect(btn).toHaveAttribute('aria-expanded', 'false');
|
|
expect(btn).toHaveClass('truncate');
|
|
|
|
await user.click(btn);
|
|
expect(btn).toHaveAttribute('aria-expanded', 'true');
|
|
expect(btn).toHaveClass('break-words');
|
|
expect(btn).not.toHaveClass('truncate');
|
|
|
|
await user.click(btn);
|
|
expect(btn).toHaveAttribute('aria-expanded', 'false');
|
|
expect(btn).toHaveClass('truncate');
|
|
});
|
|
});
|