test(17-01): add tests for UNIT-01, UNIT-02, UNIT-03 requirements

UNIT-01: Modal renders with currentSession props
- Test privacy value displayed from currentSession
- Test description value displayed from currentSession
- Test modal title renders

UNIT-02: Save functionality
- Test onSave called with correct payload on save click
- Test onSave called with updated values after user changes

UNIT-03: Loading state
- Test save button disabled when loading
- Test cancel button disabled when loading
- Test description textarea disabled when loading
- Test "Saving..." text shown when loading
This commit is contained in:
Nuwan 2026-02-08 12:20:39 +05:30
parent b7cec1ff34
commit c3d4ffd06a
1 changed files with 112 additions and 4 deletions

View File

@ -33,9 +33,117 @@ const renderModal = (props = {}) => {
};
describe('JKSessionSettingsModal', () => {
// Placeholder test to verify setup
test('renders without crashing', () => {
renderModal();
expect(screen.getByText('Session Settings')).toBeInTheDocument();
// UNIT-01: Modal renders with currentSession props
describe('rendering with currentSession props (UNIT-01)', () => {
test('displays privacy value from currentSession', () => {
renderModal({
currentSession: {
privacy: SESSION_PRIVACY_MAP.public,
description: ''
}
});
const privacySelect = screen.getByTestId('session-privacy');
expect(privacySelect.value).toBe(String(SESSION_PRIVACY_MAP.public));
});
test('displays description value from currentSession', () => {
const testDescription = 'My test session description';
renderModal({
currentSession: {
privacy: SESSION_PRIVACY_MAP.private_approve,
description: testDescription
}
});
const descriptionInput = screen.getByRole('textbox');
expect(descriptionInput.value).toBe(testDescription);
});
test('renders modal title', () => {
renderModal();
expect(screen.getByText('Session Settings')).toBeInTheDocument();
});
});
// UNIT-02: Save button calls onSave with correct payload
describe('save functionality (UNIT-02)', () => {
test('calls onSave with privacy and description when save clicked', () => {
const mockOnSave = jest.fn();
renderModal({
currentSession: {
privacy: SESSION_PRIVACY_MAP.public,
description: 'Original description'
},
onSave: mockOnSave
});
const saveButton = screen.getByRole('button', { name: /save/i });
fireEvent.click(saveButton);
expect(mockOnSave).toHaveBeenCalledTimes(1);
expect(mockOnSave).toHaveBeenCalledWith({
privacy: SESSION_PRIVACY_MAP.public,
description: 'Original description'
});
});
test('calls onSave with updated values after user changes', () => {
const mockOnSave = jest.fn();
renderModal({
currentSession: {
privacy: SESSION_PRIVACY_MAP.private_approve,
description: ''
},
onSave: mockOnSave
});
// Change privacy
const privacySelect = screen.getByTestId('session-privacy');
fireEvent.change(privacySelect, { target: { value: String(SESSION_PRIVACY_MAP.public) } });
// Change description
const descriptionInput = screen.getByRole('textbox');
fireEvent.change(descriptionInput, { target: { value: 'New description' } });
// Click save
const saveButton = screen.getByRole('button', { name: /save/i });
fireEvent.click(saveButton);
expect(mockOnSave).toHaveBeenCalledWith({
privacy: String(SESSION_PRIVACY_MAP.public),
description: 'New description'
});
});
});
// UNIT-03: Loading state disables form interactions
describe('loading state (UNIT-03)', () => {
test('disables save button when loading', () => {
renderModal({ loading: true });
const saveButton = screen.getByRole('button', { name: /saving/i });
expect(saveButton).toBeDisabled();
});
test('disables cancel button when loading', () => {
renderModal({ loading: true });
const cancelButton = screen.getByRole('button', { name: /cancel/i });
expect(cancelButton).toBeDisabled();
});
test('disables description textarea when loading', () => {
renderModal({ loading: true });
const descriptionInput = screen.getByRole('textbox');
expect(descriptionInput).toBeDisabled();
});
test('shows "Saving..." text on save button when loading', () => {
renderModal({ loading: true });
expect(screen.getByRole('button', { name: /saving/i })).toBeInTheDocument();
});
});
});