Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ export default defineConfig({
globals: true,
watch: false,
isolate: false,
sequence: {
concurrent: true,
},
},
plugins: [
stringPlugin(),
Expand Down
76 changes: 30 additions & 46 deletions web_src/js/features/repo-settings-branches.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {initRepoSettingsBranchesDrag} from './repo-settings-branches.ts';
import {POST} from '../modules/fetch.ts';
import {createSortable} from '../modules/sortable.ts';
import type {SortableEvent, SortableOptions} from 'sortablejs';
import type Sortable from 'sortablejs';
import type {SortableEvent} from 'sortablejs';

vi.mock('../modules/fetch.ts', () => ({
POST: vi.fn(),
Expand All @@ -12,63 +11,48 @@ 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();
});
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>
`;

describe('Repository Branch Settings', () => {
test('should initialize sortable for protected branches list', () => {
document.body.innerHTML = branchesHTML;
const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();

expect(createSortable).toHaveBeenCalledWith(
document.querySelector('#protected-branches-list'),
expect.objectContaining({
handle: '.drag-handle',
animation: 150,
}),
);
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});
});

test('should not initialize if protected branches list is not present', () => {
document.body.innerHTML = '';

document.querySelector('#protected-branches-list')?.remove();
const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();

expect(createSortable).not.toHaveBeenCalled();
expect(vi.mocked(createSortable).mock.calls.length).toBe(callsBefore);
});

test('should post new order after sorting', async () => {
test('should post new order after sorting', () => {
document.body.innerHTML = branchesHTML;
vi.mocked(POST).mockResolvedValue({ok: true} as Response);

// Mock createSortable to capture and execute the onEnd callback
vi.mocked(createSortable).mockImplementation(async (_el: HTMLElement, options: SortableOptions | undefined) => {
if (options?.onEnd) {
options.onEnd(new Event('SortableEvent') as SortableEvent);
}
return {destroy: vi.fn()} as unknown as Sortable;
});

const callsBefore = vi.mocked(createSortable).mock.calls.length;
initRepoSettingsBranchesDrag();

const onEnd = vi.mocked(createSortable).mock.calls[callsBefore][1]!.onEnd!;
onEnd(new Event('SortableEvent') as SortableEvent);
expect(POST).toHaveBeenCalledWith(
'some/repo/branches/priority',
expect.objectContaining({
data: {ids: [1, 2, 3]},
}),
expect.objectContaining({data: {ids: [1, 2, 3]}}),
);
});
});
2 changes: 2 additions & 0 deletions web_src/js/markup/render-iframe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe('navigateToIframeLink', () => {
});

test('unsafe links', () => {
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => undefined);
window.location.href = 'http://localhost:3000/';

// eslint-disable-next-line no-script-url
Expand All @@ -41,6 +42,7 @@ describe('navigateToIframeLink', () => {
expect(openSpy).toHaveBeenCalledTimes(0);
expect(assignSpy).toHaveBeenCalledTimes(0);
expect(window.location.href).toBe('http://localhost:3000/');
errorSpy.mockRestore();
vi.clearAllMocks();
});
});