gitea源码

repo-settings-branches.test.ts 2.2KB

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