Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karpikpl committed Apr 30, 2024
1 parent a88f53e commit a36fa6b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
61 changes: 61 additions & 0 deletions __tests__/azdo.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import fs from 'fs'
import path from 'path'
import { getWorkItemsBatch } from '../src/azdo'

const adoResponse = fs.readFileSync(
path.join(__dirname, 'test_cases/big', 'ado_response.json'),

Check failure on line 6 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `··`
'utf8'

Check failure on line 7 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `··`
)
describe('Azure DevOps API tests', () => {
beforeEach(() => {

Check failure on line 10 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `····` with `··`
jest.clearAllMocks()

Check failure on line 11 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `····`

// mock fetch

Check failure on line 13 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `····`
global.fetch = jest.fn(() =>

Check failure on line 14 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `····`

Check failure on line 14 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Functions that return promises must be async
Promise.resolve({

Check failure on line 15 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Replace `············` with `······`
json: () => Promise.resolve(JSON.parse(adoResponse)),

Check failure on line 16 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Delete `········`

Check failure on line 16 in __tests__/azdo.test.ts

View workflow job for this annotation

GitHub Actions / Lint Codebase

Functions that return promises must be async
status: 200
})
) as jest.Mock;
})

it('calls azdo API to get workitems', async () => {
const expectedAuth = Buffer.from(`:${'token'}`).toString('base64')
const ids = [11, 1, 111, 5, 14, 143, 112];

const workItems = await getWorkItemsBatch('token', 'my-org', 'my-project', ids)

expect(fetch).toHaveBeenCalledWith(
`https://dev.azure.com/my-org/my-project/_apis/wit/workitemsbatch?api-version=7.1-preview.1`,
expect.objectContaining({
"headers": expect.objectContaining({
"Authorization": `Basic ${expectedAuth}`,
}),
"body": expect.stringContaining(ids.join(',')),
}));
expect(workItems).toBeDefined()
expect((workItems?.value.map((wi: any) => wi.id))).toEqual(expect.arrayContaining(ids))
})

it('returns undefined on error', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({
status: 500
})
) as jest.Mock;

const workItems = await getWorkItemsBatch('token', 'my-org', 'my-project', [1, 2, 3])

expect(workItems).toBeUndefined()
})

it('returns undefined on catch error', async () => {
global.fetch = jest.fn(() =>
Promise.reject(new Error('error'))
) as jest.Mock;

const workItems = await getWorkItemsBatch('token', 'my-org', 'my-project', [1, 2, 3])

expect(workItems).toBeUndefined()
})
})
14 changes: 13 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ describe('action', () => {
case 'release-id':
return 'this is not a number'
default:
return ''
return 'foo'
}
})

Expand All @@ -236,4 +236,16 @@ describe('action', () => {
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'No release id provided')
expect(errorMock).not.toHaveBeenCalled()
})

it('sets a failed status when theres an exception', async () => {
// Set the action's inputs as return values from core.getInput()
getInputMock = mockInputs()
getOctokitMock.mockImplementation((token: string) => { throw Error('Could not get octokit') })

await main.run()
expect(runMock).toHaveReturned()

// Verify that all of the core library functions were called correctly
expect(setFailedMock).toHaveBeenNthCalledWith(1, 'Could not get octokit')
})
})

0 comments on commit a36fa6b

Please sign in to comment.