|
| 1 | +import { createMockNotificationForRepoName } from '../../__mocks__/notifications-mocks'; |
| 2 | +import { mockSettings } from '../../__mocks__/state-mocks'; |
| 3 | +import { GroupBy } from '../../types'; |
| 4 | +import type { Notification } from '../../typesGitHub'; |
| 5 | +import { |
| 6 | + getFlattenedNotificationsByRepo, |
| 7 | + groupNotificationsByRepository, |
| 8 | + isGroupByRepository, |
| 9 | +} from './group'; |
| 10 | + |
| 11 | +describe('renderer/utils/notifications/group.ts', () => { |
| 12 | + describe('isGroupByRepository', () => { |
| 13 | + it('returns true when groupBy is REPOSITORY', () => { |
| 14 | + const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY }; |
| 15 | + expect(isGroupByRepository(settings)).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('returns false when groupBy is DATE', () => { |
| 19 | + const settings = { ...mockSettings, groupBy: GroupBy.DATE }; |
| 20 | + expect(isGroupByRepository(settings)).toBe(false); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe('groupNotificationsByRepository', () => { |
| 25 | + it('groups notifications by repository full_name', () => { |
| 26 | + const notifications: Notification[] = [ |
| 27 | + createMockNotificationForRepoName('1', 'owner/repo-a'), |
| 28 | + createMockNotificationForRepoName('2', 'owner/repo-b'), |
| 29 | + createMockNotificationForRepoName('3', 'owner/repo-a'), |
| 30 | + ]; |
| 31 | + |
| 32 | + const result = groupNotificationsByRepository(notifications); |
| 33 | + |
| 34 | + expect(result.size).toBe(2); |
| 35 | + expect(result.get('owner/repo-a')?.map((n) => n.id)).toEqual(['1', '3']); |
| 36 | + expect(result.get('owner/repo-b')?.map((n) => n.id)).toEqual(['2']); |
| 37 | + }); |
| 38 | + |
| 39 | + it('preserves first-seen repository order', () => { |
| 40 | + const notifications: Notification[] = [ |
| 41 | + createMockNotificationForRepoName('1', 'owner/repo-c'), |
| 42 | + createMockNotificationForRepoName('2', 'owner/repo-a'), |
| 43 | + createMockNotificationForRepoName('3', 'owner/repo-b'), |
| 44 | + createMockNotificationForRepoName('4', 'owner/repo-a'), |
| 45 | + ]; |
| 46 | + |
| 47 | + const result = groupNotificationsByRepository(notifications); |
| 48 | + const keys = Array.from(result.keys()); |
| 49 | + |
| 50 | + expect(keys).toEqual(['owner/repo-c', 'owner/repo-a', 'owner/repo-b']); |
| 51 | + }); |
| 52 | + |
| 53 | + it('skips notifications without repository data', () => { |
| 54 | + const notifications: Notification[] = [ |
| 55 | + createMockNotificationForRepoName('1', 'owner/repo-a'), |
| 56 | + createMockNotificationForRepoName('2', null), |
| 57 | + createMockNotificationForRepoName('3', 'owner/repo-a'), |
| 58 | + ]; |
| 59 | + |
| 60 | + const result = groupNotificationsByRepository(notifications); |
| 61 | + |
| 62 | + expect(result.size).toBe(1); |
| 63 | + expect(result.get('owner/repo-a')?.map((n) => n.id)).toEqual(['1', '3']); |
| 64 | + }); |
| 65 | + |
| 66 | + it('returns empty map when no notifications', () => { |
| 67 | + const notifications: Notification[] = []; |
| 68 | + |
| 69 | + const result = groupNotificationsByRepository(notifications); |
| 70 | + |
| 71 | + expect(result.size).toBe(0); |
| 72 | + }); |
| 73 | + |
| 74 | + it('returns empty map when all notifications lack repository data', () => { |
| 75 | + const notifications: Notification[] = [ |
| 76 | + createMockNotificationForRepoName('1', null), |
| 77 | + createMockNotificationForRepoName('2', null), |
| 78 | + ]; |
| 79 | + |
| 80 | + const result = groupNotificationsByRepository(notifications); |
| 81 | + |
| 82 | + expect(result.size).toBe(0); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('getFlattenedNotificationsByRepo', () => { |
| 87 | + it('returns repository-grouped order when groupBy is REPOSITORY', () => { |
| 88 | + const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY }; |
| 89 | + const notifications: Notification[] = [ |
| 90 | + createMockNotificationForRepoName('1', 'owner/repo-b'), |
| 91 | + createMockNotificationForRepoName('2', 'owner/repo-a'), |
| 92 | + createMockNotificationForRepoName('3', 'owner/repo-b'), |
| 93 | + createMockNotificationForRepoName('4', 'owner/repo-a'), |
| 94 | + ]; |
| 95 | + |
| 96 | + const result = getFlattenedNotificationsByRepo(notifications, settings); |
| 97 | + |
| 98 | + // First repo-b notifications, then repo-a notifications |
| 99 | + expect(result.map((n) => n.id)).toEqual(['1', '3', '2', '4']); |
| 100 | + }); |
| 101 | + |
| 102 | + it('returns natural account order when groupBy is DATE', () => { |
| 103 | + const settings = { ...mockSettings, groupBy: GroupBy.DATE }; |
| 104 | + const notifications: Notification[] = [ |
| 105 | + createMockNotificationForRepoName('1', 'owner/repo-b'), |
| 106 | + createMockNotificationForRepoName('2', 'owner/repo-a'), |
| 107 | + createMockNotificationForRepoName('3', 'owner/repo-b'), |
| 108 | + ]; |
| 109 | + |
| 110 | + const result = getFlattenedNotificationsByRepo(notifications, settings); |
| 111 | + |
| 112 | + // Natural order preserved |
| 113 | + expect(result.map((n) => n.id)).toEqual(['1', '2', '3']); |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns empty array when no notifications', () => { |
| 117 | + const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY }; |
| 118 | + const notifications: Notification[] = []; |
| 119 | + |
| 120 | + const result = getFlattenedNotificationsByRepo(notifications, settings); |
| 121 | + |
| 122 | + expect(result).toEqual([]); |
| 123 | + }); |
| 124 | + |
| 125 | + it('handles notifications without repository data when grouped', () => { |
| 126 | + const settings = { ...mockSettings, groupBy: GroupBy.REPOSITORY }; |
| 127 | + const notifications: Notification[] = [ |
| 128 | + createMockNotificationForRepoName('1', 'owner/repo-a'), |
| 129 | + createMockNotificationForRepoName('2', null), |
| 130 | + createMockNotificationForRepoName('3', 'owner/repo-a'), |
| 131 | + ]; |
| 132 | + |
| 133 | + const result = getFlattenedNotificationsByRepo(notifications, settings); |
| 134 | + |
| 135 | + // Only notifications with repository data are included when grouped |
| 136 | + expect(result.map((n) => n.id)).toEqual(['1', '3']); |
| 137 | + }); |
| 138 | + |
| 139 | + it('preserves notifications without repository data when not grouped', () => { |
| 140 | + const settings = { ...mockSettings, groupBy: GroupBy.DATE }; |
| 141 | + const notifications: Notification[] = [ |
| 142 | + createMockNotificationForRepoName('1', 'owner/repo-a'), |
| 143 | + createMockNotificationForRepoName('2', null), |
| 144 | + createMockNotificationForRepoName('3', 'owner/repo-a'), |
| 145 | + ]; |
| 146 | + |
| 147 | + const result = getFlattenedNotificationsByRepo(notifications, settings); |
| 148 | + |
| 149 | + // All notifications preserved in natural order |
| 150 | + expect(result.map((n) => n.id)).toEqual(['1', '2', '3']); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments