|
| 1 | +import { createMockNotificationForRepoName } from '../../__mocks__/notifications-mocks'; |
| 2 | +import { |
| 3 | + mockGitHubCloudAccount, |
| 4 | + mockGitHubEnterpriseServerAccount, |
| 5 | +} from '../../__mocks__/state-mocks'; |
| 6 | +import type { AccountNotifications } from '../../types'; |
| 7 | +import { getNewNotifications } from './utils'; |
| 8 | + |
| 9 | +describe('renderer/utils/notifications/utils.ts', () => { |
| 10 | + describe('getNewNotifications', () => { |
| 11 | + it('returns all notifications when previous is empty', () => { |
| 12 | + const newNotifications: AccountNotifications[] = [ |
| 13 | + { |
| 14 | + account: mockGitHubCloudAccount, |
| 15 | + notifications: [ |
| 16 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 17 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 18 | + createMockNotificationForRepoName('3', 'some/repo'), |
| 19 | + ], |
| 20 | + error: null, |
| 21 | + }, |
| 22 | + ]; |
| 23 | + |
| 24 | + const result = getNewNotifications([], newNotifications); |
| 25 | + |
| 26 | + expect(result).toHaveLength(3); |
| 27 | + expect(result.map((n) => n.id)).toEqual(['1', '2', '3']); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns empty array when new is empty', () => { |
| 31 | + const previousNotifications: AccountNotifications[] = [ |
| 32 | + { |
| 33 | + account: mockGitHubCloudAccount, |
| 34 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 35 | + error: null, |
| 36 | + }, |
| 37 | + ]; |
| 38 | + |
| 39 | + const result = getNewNotifications(previousNotifications, []); |
| 40 | + |
| 41 | + expect(result).toHaveLength(0); |
| 42 | + }); |
| 43 | + |
| 44 | + it('returns empty array when both are empty', () => { |
| 45 | + const result = getNewNotifications([], []); |
| 46 | + |
| 47 | + expect(result).toHaveLength(0); |
| 48 | + }); |
| 49 | + |
| 50 | + it('returns only new notifications, filtering out existing ones', () => { |
| 51 | + const previousNotifications: AccountNotifications[] = [ |
| 52 | + { |
| 53 | + account: mockGitHubCloudAccount, |
| 54 | + notifications: [ |
| 55 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 56 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 57 | + ], |
| 58 | + error: null, |
| 59 | + }, |
| 60 | + ]; |
| 61 | + |
| 62 | + const newNotifications: AccountNotifications[] = [ |
| 63 | + { |
| 64 | + account: mockGitHubCloudAccount, |
| 65 | + notifications: [ |
| 66 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 67 | + createMockNotificationForRepoName('3', 'some/repo'), |
| 68 | + createMockNotificationForRepoName('4', 'some/repo'), |
| 69 | + ], |
| 70 | + error: null, |
| 71 | + }, |
| 72 | + ]; |
| 73 | + |
| 74 | + const result = getNewNotifications( |
| 75 | + previousNotifications, |
| 76 | + newNotifications, |
| 77 | + ); |
| 78 | + |
| 79 | + expect(result).toHaveLength(2); |
| 80 | + expect(result.map((n) => n.id)).toEqual(['3', '4']); |
| 81 | + }); |
| 82 | + |
| 83 | + it('returns empty array when all notifications already exist', () => { |
| 84 | + const previousNotifications: AccountNotifications[] = [ |
| 85 | + { |
| 86 | + account: mockGitHubCloudAccount, |
| 87 | + notifications: [ |
| 88 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 89 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 90 | + createMockNotificationForRepoName('3', 'some/repo'), |
| 91 | + ], |
| 92 | + error: null, |
| 93 | + }, |
| 94 | + ]; |
| 95 | + |
| 96 | + const newNotifications: AccountNotifications[] = [ |
| 97 | + { |
| 98 | + account: mockGitHubCloudAccount, |
| 99 | + notifications: [ |
| 100 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 101 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 102 | + createMockNotificationForRepoName('3', 'some/repo'), |
| 103 | + ], |
| 104 | + error: null, |
| 105 | + }, |
| 106 | + ]; |
| 107 | + |
| 108 | + const result = getNewNotifications( |
| 109 | + previousNotifications, |
| 110 | + newNotifications, |
| 111 | + ); |
| 112 | + |
| 113 | + expect(result).toHaveLength(0); |
| 114 | + }); |
| 115 | + |
| 116 | + it('handles multiple accounts correctly', () => { |
| 117 | + const previousNotifications: AccountNotifications[] = [ |
| 118 | + { |
| 119 | + account: mockGitHubCloudAccount, |
| 120 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 121 | + error: null, |
| 122 | + }, |
| 123 | + { |
| 124 | + account: mockGitHubEnterpriseServerAccount, |
| 125 | + notifications: [createMockNotificationForRepoName('10', 'some/repo')], |
| 126 | + error: null, |
| 127 | + }, |
| 128 | + ]; |
| 129 | + |
| 130 | + const newNotifications: AccountNotifications[] = [ |
| 131 | + { |
| 132 | + account: mockGitHubCloudAccount, |
| 133 | + notifications: [ |
| 134 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 135 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 136 | + ], |
| 137 | + error: null, |
| 138 | + }, |
| 139 | + { |
| 140 | + account: mockGitHubEnterpriseServerAccount, |
| 141 | + notifications: [ |
| 142 | + createMockNotificationForRepoName('10', 'some/repo'), |
| 143 | + createMockNotificationForRepoName('11', 'some/repo'), |
| 144 | + ], |
| 145 | + error: null, |
| 146 | + }, |
| 147 | + ]; |
| 148 | + |
| 149 | + const result = getNewNotifications( |
| 150 | + previousNotifications, |
| 151 | + newNotifications, |
| 152 | + ); |
| 153 | + |
| 154 | + expect(result).toHaveLength(2); |
| 155 | + expect(result.map((n) => n.id)).toEqual(['2', '11']); |
| 156 | + }); |
| 157 | + |
| 158 | + it('treats new account as having all new notifications', () => { |
| 159 | + const previousNotifications: AccountNotifications[] = [ |
| 160 | + { |
| 161 | + account: mockGitHubCloudAccount, |
| 162 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 163 | + error: null, |
| 164 | + }, |
| 165 | + ]; |
| 166 | + |
| 167 | + const newNotifications: AccountNotifications[] = [ |
| 168 | + { |
| 169 | + account: mockGitHubCloudAccount, |
| 170 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 171 | + error: null, |
| 172 | + }, |
| 173 | + { |
| 174 | + account: mockGitHubEnterpriseServerAccount, |
| 175 | + notifications: [ |
| 176 | + createMockNotificationForRepoName('10', 'some/repo'), |
| 177 | + createMockNotificationForRepoName('11', 'some/repo'), |
| 178 | + ], |
| 179 | + error: null, |
| 180 | + }, |
| 181 | + ]; |
| 182 | + |
| 183 | + const result = getNewNotifications( |
| 184 | + previousNotifications, |
| 185 | + newNotifications, |
| 186 | + ); |
| 187 | + |
| 188 | + expect(result).toHaveLength(2); |
| 189 | + expect(result.map((n) => n.id)).toEqual(['10', '11']); |
| 190 | + }); |
| 191 | + |
| 192 | + it('handles account with no notifications', () => { |
| 193 | + const previousNotifications: AccountNotifications[] = [ |
| 194 | + { |
| 195 | + account: mockGitHubCloudAccount, |
| 196 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 197 | + error: null, |
| 198 | + }, |
| 199 | + ]; |
| 200 | + |
| 201 | + const newNotifications: AccountNotifications[] = [ |
| 202 | + { |
| 203 | + account: mockGitHubCloudAccount, |
| 204 | + notifications: [], |
| 205 | + error: null, |
| 206 | + }, |
| 207 | + ]; |
| 208 | + |
| 209 | + const result = getNewNotifications( |
| 210 | + previousNotifications, |
| 211 | + newNotifications, |
| 212 | + ); |
| 213 | + |
| 214 | + expect(result).toHaveLength(0); |
| 215 | + }); |
| 216 | + |
| 217 | + it('preserves notification order from input', () => { |
| 218 | + const previousNotifications: AccountNotifications[] = [ |
| 219 | + { |
| 220 | + account: mockGitHubCloudAccount, |
| 221 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 222 | + error: null, |
| 223 | + }, |
| 224 | + ]; |
| 225 | + |
| 226 | + const newNotifications: AccountNotifications[] = [ |
| 227 | + { |
| 228 | + account: mockGitHubCloudAccount, |
| 229 | + notifications: [ |
| 230 | + createMockNotificationForRepoName('5', 'some/repo'), |
| 231 | + createMockNotificationForRepoName('3', 'some/repo'), |
| 232 | + createMockNotificationForRepoName('4', 'some/repo'), |
| 233 | + ], |
| 234 | + error: null, |
| 235 | + }, |
| 236 | + ]; |
| 237 | + |
| 238 | + const result = getNewNotifications( |
| 239 | + previousNotifications, |
| 240 | + newNotifications, |
| 241 | + ); |
| 242 | + |
| 243 | + expect(result).toHaveLength(3); |
| 244 | + expect(result.map((n) => n.id)).toEqual(['5', '3', '4']); |
| 245 | + }); |
| 246 | + |
| 247 | + it('handles removed account gracefully', () => { |
| 248 | + const previousNotifications: AccountNotifications[] = [ |
| 249 | + { |
| 250 | + account: mockGitHubCloudAccount, |
| 251 | + notifications: [createMockNotificationForRepoName('1', 'some/repo')], |
| 252 | + error: null, |
| 253 | + }, |
| 254 | + { |
| 255 | + account: mockGitHubEnterpriseServerAccount, |
| 256 | + notifications: [createMockNotificationForRepoName('10', 'some/repo')], |
| 257 | + error: null, |
| 258 | + }, |
| 259 | + ]; |
| 260 | + |
| 261 | + const newNotifications: AccountNotifications[] = [ |
| 262 | + { |
| 263 | + account: mockGitHubCloudAccount, |
| 264 | + notifications: [ |
| 265 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 266 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 267 | + ], |
| 268 | + error: null, |
| 269 | + }, |
| 270 | + // enterprise account removed |
| 271 | + ]; |
| 272 | + |
| 273 | + const result = getNewNotifications( |
| 274 | + previousNotifications, |
| 275 | + newNotifications, |
| 276 | + ); |
| 277 | + |
| 278 | + expect(result).toHaveLength(1); |
| 279 | + expect(result[0].id).toBe('2'); |
| 280 | + }); |
| 281 | + |
| 282 | + it('handles multiple new notifications across multiple accounts', () => { |
| 283 | + const previousNotifications: AccountNotifications[] = []; |
| 284 | + |
| 285 | + const newNotifications: AccountNotifications[] = [ |
| 286 | + { |
| 287 | + account: mockGitHubCloudAccount, |
| 288 | + notifications: [ |
| 289 | + createMockNotificationForRepoName('1', 'some/repo'), |
| 290 | + createMockNotificationForRepoName('2', 'some/repo'), |
| 291 | + ], |
| 292 | + error: null, |
| 293 | + }, |
| 294 | + { |
| 295 | + account: mockGitHubEnterpriseServerAccount, |
| 296 | + notifications: [ |
| 297 | + createMockNotificationForRepoName('10', 'some/repo'), |
| 298 | + createMockNotificationForRepoName('11', 'some/repo'), |
| 299 | + ], |
| 300 | + error: null, |
| 301 | + }, |
| 302 | + ]; |
| 303 | + |
| 304 | + const result = getNewNotifications( |
| 305 | + previousNotifications, |
| 306 | + newNotifications, |
| 307 | + ); |
| 308 | + |
| 309 | + expect(result).toHaveLength(4); |
| 310 | + expect(result.map((n) => n.id)).toEqual(['1', '2', '10', '11']); |
| 311 | + }); |
| 312 | + }); |
| 313 | +}); |
0 commit comments