2024-11-27 04:41:06 +00:00
|
|
|
import {beforeEach, describe, expect, test, vi} from 'vitest';
|
2024-11-28 02:15:59 +00:00
|
|
|
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
|
2024-11-27 04:41:06 +00:00
|
|
|
import {POST} from '../modules/fetch.ts';
|
|
|
|
import {createSortable} from '../modules/sortable.ts';
|
2024-12-15 21:02:32 +00:00
|
|
|
import type {SortableEvent} from 'sortablejs';
|
2024-11-27 04:41:06 +00:00
|
|
|
|
|
|
|
vi.mock('../modules/fetch.ts', () => ({
|
|
|
|
POST: vi.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
vi.mock('../modules/sortable.ts', () => ({
|
|
|
|
createSortable: vi.fn(),
|
|
|
|
}));
|
|
|
|
|
|
|
|
describe('Repository Branch Settings', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
document.body.innerHTML = `
|
|
|
|
<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>
|
|
|
|
`;
|
|
|
|
|
|
|
|
vi.clearAllMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should initialize sortable for protected branches list', () => {
|
2024-11-28 02:15:59 +00:00
|
|
|
initRepoSettingsBranchesDrag();
|
2024-11-27 04:41:06 +00:00
|
|
|
|
|
|
|
expect(createSortable).toHaveBeenCalledWith(
|
|
|
|
document.querySelector('#protected-branches-list'),
|
|
|
|
expect.objectContaining({
|
|
|
|
handle: '.drag-handle',
|
|
|
|
animation: 150,
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not initialize if protected branches list is not present', () => {
|
|
|
|
document.body.innerHTML = '';
|
|
|
|
|
2024-11-28 02:15:59 +00:00
|
|
|
initRepoSettingsBranchesDrag();
|
2024-11-27 04:41:06 +00:00
|
|
|
|
|
|
|
expect(createSortable).not.toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should post new order after sorting', async () => {
|
|
|
|
vi.mocked(POST).mockResolvedValue({ok: true} as Response);
|
|
|
|
|
|
|
|
// Mock createSortable to capture and execute the onEnd callback
|
2024-12-15 21:02:32 +00:00
|
|
|
vi.mocked(createSortable).mockImplementation(async (_el: Element, options) => {
|
|
|
|
options.onEnd(new Event('SortableEvent') as SortableEvent);
|
2024-11-27 04:41:06 +00:00
|
|
|
return {destroy: vi.fn()};
|
|
|
|
});
|
|
|
|
|
2024-11-28 02:15:59 +00:00
|
|
|
initRepoSettingsBranchesDrag();
|
2024-11-27 04:41:06 +00:00
|
|
|
|
|
|
|
expect(POST).toHaveBeenCalledWith(
|
|
|
|
'some/repo/branches/priority',
|
|
|
|
expect.objectContaining({
|
|
|
|
data: {ids: [1, 2, 3]},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|