|
| 1 | +import {beforeEach, describe, expect, test, vi} from 'vitest'; |
| 2 | +import {initRepoBranchesSettings} from './repo-settings-branches.ts'; |
| 3 | +import {POST} from '../modules/fetch.ts'; |
| 4 | +import {createSortable} from '../modules/sortable.ts'; |
| 5 | + |
| 6 | +vi.mock('../modules/fetch.ts', () => ({ |
| 7 | + POST: vi.fn(), |
| 8 | +})); |
| 9 | + |
| 10 | +vi.mock('../modules/sortable.ts', () => ({ |
| 11 | + createSortable: vi.fn(), |
| 12 | +})); |
| 13 | + |
| 14 | +describe('Repository Branch Settings', () => { |
| 15 | + beforeEach(() => { |
| 16 | + document.body.innerHTML = ` |
| 17 | + <div id="protected-branches-list" data-priority-url="some/repo/branches/priority"> |
| 18 | + <div class="item" data-id="1"> |
| 19 | + <div class="flex-item"> |
| 20 | + <div class="drag-handle"></div> |
| 21 | + </div> |
| 22 | + </div> |
| 23 | + <div class="item" data-id="2"> |
| 24 | + <div class="flex-item"> |
| 25 | + <div class="drag-handle"></div> |
| 26 | + </div> |
| 27 | + </div> |
| 28 | + <div class="item" data-id="3"> |
| 29 | + <div class="flex-item"> |
| 30 | + <div class="drag-handle"></div> |
| 31 | + </div> |
| 32 | + </div> |
| 33 | + </div> |
| 34 | + `; |
| 35 | + |
| 36 | + vi.clearAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + test('should initialize sortable for protected branches list', () => { |
| 40 | + initRepoBranchesSettings(); |
| 41 | + |
| 42 | + expect(createSortable).toHaveBeenCalledWith( |
| 43 | + document.querySelector('#protected-branches-list'), |
| 44 | + expect.objectContaining({ |
| 45 | + handle: '.drag-handle', |
| 46 | + animation: 150, |
| 47 | + }), |
| 48 | + ); |
| 49 | + }); |
| 50 | + |
| 51 | + test('should not initialize if protected branches list is not present', () => { |
| 52 | + document.body.innerHTML = ''; |
| 53 | + |
| 54 | + initRepoBranchesSettings(); |
| 55 | + |
| 56 | + expect(createSortable).not.toHaveBeenCalled(); |
| 57 | + }); |
| 58 | + |
| 59 | + test('should post new order after sorting', async () => { |
| 60 | + vi.mocked(POST).mockResolvedValue({ok: true} as Response); |
| 61 | + |
| 62 | + // Mock createSortable to capture and execute the onEnd callback |
| 63 | + vi.mocked(createSortable).mockImplementation((_el, options) => { |
| 64 | + options.onEnd(); |
| 65 | + return {destroy: vi.fn()}; |
| 66 | + }); |
| 67 | + |
| 68 | + initRepoBranchesSettings(); |
| 69 | + |
| 70 | + expect(POST).toHaveBeenCalledWith( |
| 71 | + 'some/repo/branches/priority', |
| 72 | + expect.objectContaining({ |
| 73 | + data: {ids: [1, 2, 3]}, |
| 74 | + }), |
| 75 | + ); |
| 76 | + }); |
| 77 | +}); |
0 commit comments