Skip to content

Commit

Permalink
(fix): prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-bromann committed Feb 14, 2024
1 parent 4472255 commit 9752409
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 25 deletions.
12 changes: 7 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,13 @@ export async function run(): Promise<void> {
}
}

export async function expense({ actionRepo, resendAPIKey, githubToken } = {
actionRepo: process.env.GITHUB_REPOSITORY,
resendAPIKey: process.env.RESEND_API_KEY,
githubToken: process.env.GH_TOKEN
}): Promise<void> {
export async function expense(
{ actionRepo, resendAPIKey, githubToken } = {
actionRepo: process.env.GITHUB_REPOSITORY,
resendAPIKey: process.env.RESEND_API_KEY,
githubToken: process.env.GH_TOKEN
}
): Promise<void> {
/**
* check if `GH_TOKEN` environment variable is set to interact with GitHub API
*/
Expand Down
52 changes: 32 additions & 20 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PULLS } from './__fixtures__/gh.js' assert { type: 'json' }

vi.mock('@actions/core', () => ({
setFailed: vi.fn(),
getInput: vi.fn((input) => {
getInput: vi.fn(input => {
if (input === 'prNumber') return '12121'
if (input === 'amount') return '123'
})
Expand All @@ -27,7 +27,9 @@ vi.mock('resend', () => {
})

vi.mock('@octokit/rest', async () => {
const { COMMITS, PULLS } = await import('./__fixtures__/gh.js', { assert: { type: 'json' } })
const { COMMITS, PULLS } = await import('./__fixtures__/gh.js', {
assert: { type: 'json' }
})
const listCommits = vi.fn().mockReturnValue({ data: COMMITS })
const get = vi.fn().mockReturnValue({ data: PULLS })
const createComment = vi.fn()
Expand Down Expand Up @@ -57,14 +59,20 @@ describe('run', () => {

describe('expense', () => {
it('should throw if environment variables are not set', async () => {
await expect(() => expense()).rejects.toThrow('Please export a "GH_TOKEN"')
await expect(() => expense({
githubToken: 'token'
} as any)).rejects.toThrow('Please export a "RESEND_API_KEY"')
await expect(() => expense({
githubToken: 'token',
resendAPIKey: 'token'
} as any)).rejects.toThrow('Could not get repository information')
await expect(() => expense()).rejects.toThrow(
'Please export a "GH_TOKEN"'
)
await expect(() =>
expense({
githubToken: 'token'
} as any)
).rejects.toThrow('Please export a "RESEND_API_KEY"')
await expect(() =>
expense({
githubToken: 'token',
resendAPIKey: 'token'
} as any)
).rejects.toThrow('Could not get repository information')
})

it('should properly expense a PR', async () => {
Expand Down Expand Up @@ -153,22 +161,26 @@ describe('expense', () => {
data: { ...PULLS, merge_commit_sha: undefined }
})
vi.mocked(get).mockResolvedValue(pulls)
await expect(() => expense({
githubToken: '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',
resendAPIKey: 'token',
actionRepo: 'webdriverio/webdriverio'
})).rejects.toThrow('Pull request has not been merged yet!')
await expect(() =>
expense({
githubToken: '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',
resendAPIKey: 'token',
actionRepo: 'webdriverio/webdriverio'
})
).rejects.toThrow('Pull request has not been merged yet!')
})

it('fails if PR has already been expensed', async () => {
const pulls = Object.create({
data: { ...PULLS, labels: [{ name: 'Expensable $123 💸' }] }
})
vi.mocked(get).mockResolvedValue(pulls)
await expect(() => expense({
githubToken: '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',
resendAPIKey: 'token',
actionRepo: 'webdriverio/webdriverio'
})).rejects.toThrow('Pull request has already been expensed!')
await expect(() =>
expense({
githubToken: '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',
resendAPIKey: 'token',
actionRepo: 'webdriverio/webdriverio'
})
).rejects.toThrow('Pull request has already been expensed!')
})
})

0 comments on commit 9752409

Please sign in to comment.