|
| 1 | +import { createPullRequest } from './feature'; |
| 2 | +import { AzureDevOpsError } from '../../../shared/errors'; |
| 3 | + |
| 4 | +describe('createPullRequest unit', () => { |
| 5 | + // Test for required fields validation |
| 6 | + test('should throw error when title is not provided', async () => { |
| 7 | + // Arrange - mock connection, never used due to validation error |
| 8 | + const mockConnection: any = { |
| 9 | + getGitApi: jest.fn(), |
| 10 | + }; |
| 11 | + |
| 12 | + // Act & Assert |
| 13 | + await expect( |
| 14 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 15 | + title: '', |
| 16 | + sourceRefName: 'refs/heads/feature-branch', |
| 17 | + targetRefName: 'refs/heads/main', |
| 18 | + }), |
| 19 | + ).rejects.toThrow('Title is required'); |
| 20 | + }); |
| 21 | + |
| 22 | + test('should throw error when source branch is not provided', async () => { |
| 23 | + // Arrange - mock connection, never used due to validation error |
| 24 | + const mockConnection: any = { |
| 25 | + getGitApi: jest.fn(), |
| 26 | + }; |
| 27 | + |
| 28 | + // Act & Assert |
| 29 | + await expect( |
| 30 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 31 | + title: 'Test PR', |
| 32 | + sourceRefName: '', |
| 33 | + targetRefName: 'refs/heads/main', |
| 34 | + }), |
| 35 | + ).rejects.toThrow('Source branch is required'); |
| 36 | + }); |
| 37 | + |
| 38 | + test('should throw error when target branch is not provided', async () => { |
| 39 | + // Arrange - mock connection, never used due to validation error |
| 40 | + const mockConnection: any = { |
| 41 | + getGitApi: jest.fn(), |
| 42 | + }; |
| 43 | + |
| 44 | + // Act & Assert |
| 45 | + await expect( |
| 46 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 47 | + title: 'Test PR', |
| 48 | + sourceRefName: 'refs/heads/feature-branch', |
| 49 | + targetRefName: '', |
| 50 | + }), |
| 51 | + ).rejects.toThrow('Target branch is required'); |
| 52 | + }); |
| 53 | + |
| 54 | + // Test for error propagation |
| 55 | + test('should propagate custom errors when thrown internally', async () => { |
| 56 | + // Arrange |
| 57 | + const mockConnection: any = { |
| 58 | + getGitApi: jest.fn().mockImplementation(() => { |
| 59 | + throw new AzureDevOpsError('Custom error'); |
| 60 | + }), |
| 61 | + }; |
| 62 | + |
| 63 | + // Act & Assert |
| 64 | + await expect( |
| 65 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 66 | + title: 'Test PR', |
| 67 | + sourceRefName: 'refs/heads/feature-branch', |
| 68 | + targetRefName: 'refs/heads/main', |
| 69 | + }), |
| 70 | + ).rejects.toThrow(AzureDevOpsError); |
| 71 | + |
| 72 | + await expect( |
| 73 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 74 | + title: 'Test PR', |
| 75 | + sourceRefName: 'refs/heads/feature-branch', |
| 76 | + targetRefName: 'refs/heads/main', |
| 77 | + }), |
| 78 | + ).rejects.toThrow('Custom error'); |
| 79 | + }); |
| 80 | + |
| 81 | + test('should wrap unexpected errors in a friendly error message', async () => { |
| 82 | + // Arrange |
| 83 | + const mockConnection: any = { |
| 84 | + getGitApi: jest.fn().mockImplementation(() => { |
| 85 | + throw new Error('Unexpected error'); |
| 86 | + }), |
| 87 | + }; |
| 88 | + |
| 89 | + // Act & Assert |
| 90 | + await expect( |
| 91 | + createPullRequest(mockConnection, 'TestProject', 'TestRepo', { |
| 92 | + title: 'Test PR', |
| 93 | + sourceRefName: 'refs/heads/feature-branch', |
| 94 | + targetRefName: 'refs/heads/main', |
| 95 | + }), |
| 96 | + ).rejects.toThrow('Failed to create pull request: Unexpected error'); |
| 97 | + }); |
| 98 | +}); |
0 commit comments