generated from actions/typescript-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for limitFetcher and processThreshold.
- Loading branch information
1 parent
f82dcd7
commit cc36195
Showing
5 changed files
with
197 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* Unit tests for src/processThreshold.ts | ||
*/ | ||
|
||
import * as core from '@actions/core' | ||
import nock from 'nock'; // Import the nock library for mocking HTTP requests | ||
import { fetchRateLimit } from '../src/limitFetcher'; | ||
import { expect } from '@jest/globals' | ||
|
||
// Mock the setFailed function from '@actions/core' | ||
jest.mock('@actions/core', () => ({ | ||
setFailed: jest.fn(), | ||
})); | ||
|
||
describe('limitFetcher.ts', () => { | ||
beforeAll(() => { | ||
// Set up a mock for the GitHub API | ||
nock('https://api.github.com') | ||
.get('/rate_limit') | ||
.reply(200, { | ||
data: { | ||
resources: { | ||
core: { | ||
limit: 5000, | ||
remaining: 4300, | ||
reset: 1696896000, | ||
used: 700, | ||
}, | ||
search: { | ||
limit: 30, | ||
remaining: 18, | ||
reset: 1696896400, | ||
used: 12, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
afterAll(() => { | ||
// Clean up the nock mocks | ||
nock.cleanAll(); | ||
}); | ||
|
||
it('should fetch and return the core rate limit', async () => { | ||
const token = 'some_fake_github_token'; | ||
const resource = 'core'; | ||
|
||
const result = await fetchRateLimit(token, resource); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.limit).toBe(5000); | ||
expect(result.remaining).toBe(4300); | ||
expect(result.reset).toBe(1696896000); | ||
}); | ||
|
||
it('should fetch and return the search rate limit', async () => { | ||
const token = 'some_fake_github_token'; | ||
const resource = 'search'; | ||
|
||
const result = await fetchRateLimit(token, resource); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.limit).toBe(30); | ||
expect(result.remaining).toBe(18); | ||
expect(result.reset).toBe(1696896400); | ||
}); | ||
|
||
it('should set a failed message on API error', async () => { | ||
// Mock the GitHub API to return an error (e.g., 404 Not Found) | ||
nock('https://api.github.com') | ||
.get('/rate_limit') | ||
.reply(404); | ||
|
||
const token = 'some_fake_github_token'; | ||
const resource = 'core'; | ||
|
||
await fetchRateLimit(token, resource); | ||
|
||
expect(core.setFailed).toHaveBeenCalledWith('Github API rateLimit could not be retrieved.'); | ||
}); | ||
|
||
}); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Unit tests for src/processThreshold.ts | ||
*/ | ||
|
||
import * as core from '@actions/core' | ||
import { processThreshold } from '../src/processThreshold'; | ||
import { expect } from '@jest/globals' | ||
|
||
// Mock the setFailed function from '@actions/core' | ||
jest.mock('@actions/core', () => ({ | ||
setFailed: jest.fn(), | ||
})); | ||
|
||
|
||
describe('processThreshold.ts', () => { | ||
it('should process an absolute threshold correctly', () => { | ||
const thresholdAsString = '26'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.thresholdAsAbsolute).toBe(26); | ||
expect(result.thresholdAsFraction).toBeUndefined(); | ||
}); | ||
|
||
it('should process a fractional threshold correctly', () => { | ||
const thresholdAsString = '0.3'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.thresholdAsAbsolute).toBeUndefined(); | ||
expect(result.thresholdAsFraction).toBe(0.3); | ||
}); | ||
|
||
it('should process a percent threshold correctly', () => { | ||
const thresholdAsString = '60%'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.thresholdAsAbsolute).toBeUndefined(); | ||
expect(result.thresholdAsFraction).toBe(0.6); | ||
}); | ||
|
||
it('should set an error for an invalid threshold (negative number)', () => { | ||
const thresholdAsString = '-0.1'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).toHaveBeenCalledWith( | ||
'The threshold must be a positive number, but -0.1 was provided.' | ||
); | ||
expect(result.thresholdAsAbsolute).toBeUndefined(); | ||
expect(result.thresholdAsFraction).toBeUndefined(); | ||
}); | ||
|
||
it('should handle invalid input (not a number)', () => { | ||
const thresholdAsString = 'invalid'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).toHaveBeenCalledWith( | ||
'The threshold must be a positive number, but invalid was provided.' | ||
); | ||
expect(result.thresholdAsAbsolute).toBeUndefined(); | ||
expect(result.thresholdAsFraction).toBeUndefined(); | ||
}); | ||
|
||
it('should ignore a blank between number and percent sign', () => { | ||
const thresholdAsString = '50 %'; | ||
const result = processThreshold(thresholdAsString); | ||
|
||
expect(core.setFailed).not.toHaveBeenCalled(); | ||
expect(result.thresholdAsAbsolute).toBeUndefined(); | ||
expect(result.thresholdAsFraction).toBe(50); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters