Files

59 lines
2.2 KiB
TypeScript
Raw Permalink Normal View History

import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
2024-11-27 05:41:06 +01:00
import {POST} from '../modules/fetch.ts';
import {createSortable} from '../modules/sortable.ts';
2026-03-30 18:17:16 +02:00
import type {SortableEvent} from 'sortablejs';
2024-11-27 05:41:06 +01:00
vi.mock('../modules/fetch.ts', () => ({
POST: vi.fn(),
}));
vi.mock('../modules/sortable.ts', () => ({
createSortable: vi.fn(),
}));
2026-03-30 18:17:16 +02:00
const branchesHTML = `
<div id="protected-branches-list" data-update-priority-url="some/repo/branches/priority">
<div class="flex-item tw-items-center item" data-id="1">
<div class="drag-handle"></div>
</div>
<div class="flex-item tw-items-center item" data-id="2">
<div class="drag-handle"></div>
</div>
<div class="flex-item tw-items-center item" data-id="3">
<div class="drag-handle"></div>
</div>
</div>
`;
2024-11-27 05:41:06 +01:00
2026-03-30 18:17:16 +02:00
describe('Repository Branch Settings', () => {
2024-11-27 05:41:06 +01:00
test('should initialize sortable for protected branches list', () => {
2026-03-30 18:17:16 +02:00
document.body.innerHTML = branchesHTML;
const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();
2026-03-30 18:17:16 +02:00
const newCalls = vi.mocked(createSortable).mock.calls.slice(callsBefore);
expect(newCalls).toHaveLength(1);
expect(newCalls[0][0]).toBe(document.querySelector('#protected-branches-list'));
expect(newCalls[0][1]).toMatchObject({handle: '.drag-handle', animation: 150});
2024-11-27 05:41:06 +01:00
});
test('should not initialize if protected branches list is not present', () => {
2026-03-30 18:17:16 +02:00
document.querySelector('#protected-branches-list')?.remove();
const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();
2026-03-30 18:17:16 +02:00
expect(vi.mocked(createSortable).mock.calls.length).toBe(callsBefore);
2024-11-27 05:41:06 +01:00
});
2026-03-30 18:17:16 +02:00
test('should post new order after sorting', () => {
document.body.innerHTML = branchesHTML;
2024-11-27 05:41:06 +01:00
vi.mocked(POST).mockResolvedValue({ok: true} as Response);
2026-03-30 18:17:16 +02:00
const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();
2026-03-30 18:17:16 +02:00
const onEnd = vi.mocked(createSortable).mock.calls[callsBefore][1]!.onEnd!;
onEnd(new Event('SortableEvent') as SortableEvent);
2024-11-27 05:41:06 +01:00
expect(POST).toHaveBeenCalledWith(
'some/repo/branches/priority',
2026-03-30 18:17:16 +02:00
expect.objectContaining({data: {ids: [1, 2, 3]}}),
2024-11-27 05:41:06 +01:00
);
});
});