Files
shields/services/github/github-issue-detail.spec.js
T
Lesley MurfinandAI Agent 8958f79de9 [GithubIssueDetail] Support draft state on pull request detail badge (#12180)
* feat(github): support draft state on pull request detail badge

* test(github): update sazerac unit tests for draft property in GithubIssueDetail

---------

Co-authored-by: AI Agent <lesley@mtl-02.dedi.leaseweb.net>
2026-10-11 13:53:33 +00:00

304 lines
7.6 KiB
JavaScript

import { expect } from 'chai'
import { test, given } from 'sazerac'
import { age, formatDate } from '../date.js'
import { metric } from '../text-formatters.js'
import { InvalidResponse } from '../index.js'
import GithubIssueDetail from './github-issue-detail.service.js'
import { issueStateColor, commentsColor } from './github-helpers.js'
describe('GithubIssueDetail', function () {
test(GithubIssueDetail.render, () => {
given({
property: 'state',
value: { state: 'open', draft: false },
number: '12',
isPR: true,
}).expect({
label: 'pull request 12',
message: 'open',
color: issueStateColor('open'),
})
given({
property: 'state',
value: { state: 'open', draft: true },
number: '13',
isPR: true,
}).expect({
label: 'pull request 13',
message: 'draft',
color: 'gray',
})
given({
property: 'state',
value: { state: 'closed' },
number: '15',
isPR: false,
}).expect({
label: 'issue 15',
message: 'closed',
color: issueStateColor('closed'),
})
given({
property: 'state',
value: { state: 'not planned' },
number: '93',
isPR: false,
}).expect({
label: 'issue 93',
message: 'not planned',
color: issueStateColor('not planned'),
})
given({
property: 'state',
value: { state: 'duplicate' },
number: '95',
isPR: false,
}).expect({
label: 'issue 95',
message: 'duplicate',
color: issueStateColor('duplicate'),
})
given({
property: 'title',
value: 'refactor [FooService]',
number: '232',
isPR: true,
}).expect({
label: 'pull request 232',
message: 'refactor [FooService]',
})
given({
property: 'title',
value: 'Packagist: invalid response data',
number: '345',
isPR: false,
}).expect({
label: 'issue 345',
message: 'Packagist: invalid response data',
})
given({
property: 'author',
value: 'calebcartwright',
}).expect({
label: 'author',
message: 'calebcartwright',
})
given({
property: 'label',
value: { names: ['feature'], colors: ['a2eeef'] },
}).expect({
color: 'a2eeef',
message: 'feature',
label: 'label',
})
given({
property: 'label',
value: { names: ['service-badge', 'bug'], colors: ['a2eeef', 'ee0701'] },
}).expect({
color: undefined,
message: 'service-badge | bug',
label: 'label',
})
given({ property: 'comments', value: 27 }).expect({
label: 'comments',
message: metric(27),
color: commentsColor('closed'),
})
given({
property: 'age',
value: '2019-04-01T20:09:31Z',
}).expect({
label: 'created',
message: formatDate('2019-04-01T20:09:31Z'),
color: age('2019-04-01T20:09:31Z'),
})
given({
property: 'last-update',
value: '2019-04-02T20:09:31Z',
}).expect({
label: 'updated',
message: formatDate('2019-04-02T20:09:31Z'),
color: age('2019-04-02T20:09:31Z'),
})
given({
property: 'milestone',
value: 'MS 1',
}).expect({
label: 'milestone',
message: 'MS 1',
color: 'informational',
})
})
test(GithubIssueDetail.prototype.transform, () => {
given({
property: 'state',
json: { state: 'closed' },
}).expect({
// Since it's a PR, the "merged" value is not crucial here.
value: { state: 'closed', merged: false, draft: false },
isPR: false,
})
given({
property: 'state',
json: { state: 'closed', state_reason: 'not_planned' },
}).expect({
value: { state: 'not planned', merged: false, draft: false },
isPR: false,
})
given({
property: 'state',
json: { state: 'closed', state_reason: 'duplicate' },
}).expect({
value: { state: 'duplicate', merged: false, draft: false },
isPR: false,
})
given({
property: 'state',
json: { state: 'closed', state_reason: 'other_reason' },
}).expect({
value: { state: 'closed', merged: false, draft: false },
isPR: false,
})
given({
property: 'state',
json: { state: 'closed', pull_request: { merged_at: null } },
}).expect({
value: { state: 'closed', merged: false, draft: false },
isPR: true,
})
given({
property: 'state',
json: {
state: 'closed',
pull_request: { merged_at: '2025-01-01T00:00:00Z' },
},
}).expect({
value: { state: 'closed', merged: true, draft: false },
isPR: true,
})
given({
property: 'state',
issueKind: 'pulls',
json: { state: 'closed', merged_at: null },
}).expect({
value: { state: 'closed', merged: false, draft: false },
isPR: true,
})
given({
property: 'state',
issueKind: 'pulls',
json: { state: 'closed', merged_at: 'I am not null' },
}).expect({
value: { state: 'closed', merged: true, draft: false },
isPR: true,
})
given({
property: 'state',
issueKind: 'pulls',
json: { state: 'open', draft: true },
}).expect({
value: { state: 'open', merged: false, draft: true },
isPR: true,
})
given({
property: 'title',
json: { pull_request: {}, title: 'refactor [Codecov]' },
}).expect({
value: 'refactor [Codecov]',
isPR: true,
})
given({
property: 'author',
json: { user: { login: 'dependabot' } },
}).expect({
value: 'dependabot',
isPR: false,
})
given({
property: 'label',
json: {
pull_request: {},
labels: [
{ name: 'service-badge', color: 'a2eeef' },
{ name: 'bug', color: 'ee0701' },
],
},
}).expect({
value: {
names: ['service-badge', 'bug'],
colors: ['a2eeef', 'ee0701'],
},
isPR: true,
})
given({
property: 'label',
json: { labels: [{ name: 'bug', color: 'ee0701' }] },
}).expect({
value: {
names: ['bug'],
colors: ['ee0701'],
},
isPR: false,
})
given({
property: 'comments',
json: { comments: 100 },
}).expect({
value: 100,
isPR: false,
})
given({
property: 'age',
json: { created_at: '2019-04-01T20:09:31Z' },
}).expect({
value: '2019-04-01T20:09:31Z',
isPR: false,
})
given({
property: 'last-update',
json: { updated_at: '2019-04-02T20:09:31Z' },
}).expect({
value: '2019-04-02T20:09:31Z',
isPR: false,
})
given({
property: 'milestone',
json: { milestone: { title: 'MS 1' } },
}).expect({
value: 'MS 1',
isPR: false,
})
})
context('transform()', function () {
it('throws InvalidResponse error when issue has no labels', function () {
try {
GithubIssueDetail.prototype.transform({
property: 'label',
json: { labels: [] },
})
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.prettyMessage).to.equal('no labels found')
}
})
})
context('transform()', function () {
it('throws InvalidResponse error when issue has no milestone', function () {
try {
GithubIssueDetail.prototype.transform({
property: 'milestone',
json: { milestone: null },
})
expect.fail('Expected to throw')
} catch (e) {
expect(e).to.be.an.instanceof(InvalidResponse)
expect(e.prettyMessage).to.equal('no milestone')
}
})
})
})