|
| 1 | +import * as core from '@actions/core'; |
| 2 | +import request from 'request'; |
| 3 | +import { main } from '../src/lib'; |
| 4 | + |
| 5 | +jest.mock('@actions/core'); |
| 6 | +jest.mock('request'); |
| 7 | + |
| 8 | +describe('Comment Jira Issue Action', () => { |
| 9 | + beforeEach(() => { |
| 10 | + jest.clearAllMocks(); |
| 11 | + }); |
| 12 | + |
| 13 | + it('should comment on a Jira issue successfully', async () => { |
| 14 | + const setFailedMock = jest.spyOn(core, 'setFailed'); |
| 15 | + const getInputMock = jest.spyOn(core, 'getInput'); |
| 16 | + |
| 17 | + getInputMock.mockImplementation((name: string) => { |
| 18 | + switch (name) { |
| 19 | + case 'token': |
| 20 | + return 'fake-token'; |
| 21 | + case 'issue-key': |
| 22 | + return 'TEST-123'; |
| 23 | + case 'comment': |
| 24 | + return 'This is a test comment\nwith multiple lines\n'; |
| 25 | + case 'api-base': |
| 26 | + return 'https://jira.example.com'; |
| 27 | + default: |
| 28 | + return ''; |
| 29 | + } |
| 30 | + }); |
| 31 | + |
| 32 | + const responseMock = { |
| 33 | + statusCode: 200, |
| 34 | + statusMessage: 'OK' |
| 35 | + }; |
| 36 | + |
| 37 | + const responseBody = { id: '10000' }; |
| 38 | + |
| 39 | + let requestBody: any = null; |
| 40 | + let requestUrl: string = ''; |
| 41 | + let requestHeaders: any = null; |
| 42 | + |
| 43 | + (request as unknown as jest.Mock).mockImplementation((options, callback) => { |
| 44 | + requestUrl = options.url; |
| 45 | + requestHeaders = options.headers; |
| 46 | + requestBody = options.body; |
| 47 | + callback(null, responseMock, responseBody); |
| 48 | + }); |
| 49 | + |
| 50 | + await main(); |
| 51 | + |
| 52 | + expect(requestUrl).toBe('https://jira.example.com/rest/api/2/issue/TEST-123/comment'); |
| 53 | + expect(requestHeaders).toEqual({ |
| 54 | + 'Authorization': 'Bearer fake-token', |
| 55 | + 'Content-Type': 'application/json' |
| 56 | + }); |
| 57 | + expect(requestBody).toEqual({ |
| 58 | + body: 'This is a test comment\r\nwith multiple lines\r\n' |
| 59 | + }); |
| 60 | + expect(setFailedMock).not.toHaveBeenCalled(); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should fail if request returns an error', async () => { |
| 64 | + const setFailedMock = jest.spyOn(core, 'setFailed'); |
| 65 | + const getInputMock = jest.spyOn(core, 'getInput'); |
| 66 | + |
| 67 | + getInputMock.mockImplementation((name: string) => { |
| 68 | + switch (name) { |
| 69 | + case 'token': |
| 70 | + return 'fake-token'; |
| 71 | + case 'issue-key': |
| 72 | + return 'TEST-123'; |
| 73 | + case 'comment': |
| 74 | + return 'This is a test comment\nwith multiple lines\n'; |
| 75 | + case 'api-base': |
| 76 | + return 'https://jira.example.com'; |
| 77 | + default: |
| 78 | + return ''; |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + const err = new Error('Request failed'); |
| 83 | + |
| 84 | + (request as unknown as jest.Mock).mockImplementation((options, callback) => { |
| 85 | + callback(err, null, null); |
| 86 | + }); |
| 87 | + |
| 88 | + await main(); |
| 89 | + |
| 90 | + expect(setFailedMock).toHaveBeenCalledWith(err); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should fail if response status code is >= 400', async () => { |
| 94 | + const setFailedMock = jest.spyOn(core, 'setFailed'); |
| 95 | + const getInputMock = jest.spyOn(core, 'getInput'); |
| 96 | + |
| 97 | + getInputMock.mockImplementation((name: string) => { |
| 98 | + switch (name) { |
| 99 | + case 'token': |
| 100 | + return 'fake-token'; |
| 101 | + case 'issue-key': |
| 102 | + return 'TEST-123'; |
| 103 | + case 'comment': |
| 104 | + return 'This is a test comment\nwith multiple lines\n'; |
| 105 | + case 'api-base': |
| 106 | + return 'https://jira.example.com'; |
| 107 | + default: |
| 108 | + return ''; |
| 109 | + } |
| 110 | + }); |
| 111 | + |
| 112 | + const responseMock = { |
| 113 | + statusCode: 400, |
| 114 | + statusMessage: 'Bad Request', |
| 115 | + }; |
| 116 | + |
| 117 | + const responseBody = { |
| 118 | + detail: "Bad Request" |
| 119 | + }; |
| 120 | + |
| 121 | + (request as unknown as jest.Mock).mockImplementation((options, callback) => { |
| 122 | + callback(null, responseMock, responseBody); |
| 123 | + }); |
| 124 | + |
| 125 | + await main(); |
| 126 | + |
| 127 | + const err = new Error("400 Bad Request\n{\"detail\":\"Bad Request\"}"); |
| 128 | + |
| 129 | + expect(setFailedMock).toHaveBeenCalledWith(err); |
| 130 | + }); |
| 131 | +}); |
0 commit comments