Skip to content

Commit c58c3ea

Browse files
committed
test: context cover workflow_dispatch
1 parent 39b71e2 commit c58c3ea

2 files changed

Lines changed: 176 additions & 3 deletions

File tree

__tests__/common/github/__snapshots__/context.test.ts.snap

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`getGitHubContext > returns eventName 2`] = `
3+
exports[`useContext > returns context for \`pull_request\` 2`] = `
44
{
55
"action": "opened",
66
"installation": {
@@ -521,3 +521,145 @@ exports[`getGitHubContext > returns eventName 2`] = `
521521
},
522522
}
523523
`;
524+
525+
exports[`useContext > returns context for \`workflow_dispatch\` 1`] = `
526+
{
527+
"inputs": {
528+
"name": "Mona the Octocat",
529+
},
530+
"organization": {
531+
"avatar_url": "https://avatars3.githubusercontent.com/u/6811672?v=4",
532+
"description": "Working better together!",
533+
"events_url": "https://api.github.com/orgs/octo-org/events",
534+
"hooks_url": "https://api.github.com/orgs/octo-org/hooks",
535+
"id": 6811672,
536+
"issues_url": "https://api.github.com/orgs/octo-org/issues",
537+
"login": "octo-org",
538+
"members_url": "https://api.github.com/orgs/octo-org/members{/member}",
539+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=",
540+
"public_members_url": "https://api.github.com/orgs/octo-org/public_members{/member}",
541+
"repos_url": "https://api.github.com/orgs/octo-org/repos",
542+
"url": "https://api.github.com/orgs/octo-org",
543+
},
544+
"ref": "refs/heads/master",
545+
"repository": {
546+
"archive_url": "https://api.github.com/repos/octo-org/octo-repo/{archive_format}{/ref}",
547+
"archived": false,
548+
"assignees_url": "https://api.github.com/repos/octo-org/octo-repo/assignees{/user}",
549+
"blobs_url": "https://api.github.com/repos/octo-org/octo-repo/git/blobs{/sha}",
550+
"branches_url": "https://api.github.com/repos/octo-org/octo-repo/branches{/branch}",
551+
"clone_url": "https://github.com/octo-org/octo-repo.git",
552+
"collaborators_url": "https://api.github.com/repos/octo-org/octo-repo/collaborators{/collaborator}",
553+
"comments_url": "https://api.github.com/repos/octo-org/octo-repo/comments{/number}",
554+
"commits_url": "https://api.github.com/repos/octo-org/octo-repo/commits{/sha}",
555+
"compare_url": "https://api.github.com/repos/octo-org/octo-repo/compare/{base}...{head}",
556+
"contents_url": "https://api.github.com/repos/octo-org/octo-repo/contents/{+path}",
557+
"contributors_url": "https://api.github.com/repos/octo-org/octo-repo/contributors",
558+
"created_at": "2014-02-28T02:42:51Z",
559+
"custom_properties": {},
560+
"default_branch": "master",
561+
"deployments_url": "https://api.github.com/repos/octo-org/octo-repo/deployments",
562+
"description": "My first repo on GitHub!",
563+
"downloads_url": "https://api.github.com/repos/octo-org/octo-repo/downloads",
564+
"events_url": "https://api.github.com/repos/octo-org/octo-repo/events",
565+
"fork": false,
566+
"forks": 1,
567+
"forks_count": 1,
568+
"forks_url": "https://api.github.com/repos/octo-org/octo-repo/forks",
569+
"full_name": "octo-org/octo-repo",
570+
"git_commits_url": "https://api.github.com/repos/octo-org/octo-repo/git/commits{/sha}",
571+
"git_refs_url": "https://api.github.com/repos/octo-org/octo-repo/git/refs{/sha}",
572+
"git_tags_url": "https://api.github.com/repos/octo-org/octo-repo/git/tags{/sha}",
573+
"git_url": "git://github.com/octo-org/octo-repo.git",
574+
"has_downloads": true,
575+
"has_issues": true,
576+
"has_pages": false,
577+
"has_projects": true,
578+
"has_wiki": true,
579+
"homepage": "https://example.com",
580+
"hooks_url": "https://api.github.com/repos/octo-org/octo-repo/hooks",
581+
"html_url": "https://github.com/octo-org/octo-repo",
582+
"id": 17273051,
583+
"is_template": false,
584+
"issue_comment_url": "https://api.github.com/repos/octo-org/octo-repo/issues/comments{/number}",
585+
"issue_events_url": "https://api.github.com/repos/octo-org/octo-repo/issues/events{/number}",
586+
"issues_url": "https://api.github.com/repos/octo-org/octo-repo/issues{/number}",
587+
"keys_url": "https://api.github.com/repos/octo-org/octo-repo/keys{/key_id}",
588+
"labels_url": "https://api.github.com/repos/octo-org/octo-repo/labels{/name}",
589+
"language": "JavaScript",
590+
"languages_url": "https://api.github.com/repos/octo-org/octo-repo/languages",
591+
"license": null,
592+
"merges_url": "https://api.github.com/repos/octo-org/octo-repo/merges",
593+
"milestones_url": "https://api.github.com/repos/octo-org/octo-repo/milestones{/number}",
594+
"mirror_url": null,
595+
"name": "octo-repo",
596+
"node_id": "MDEwOlJlcG9zaXRvcnkxNzI3MzA1MQ==",
597+
"notifications_url": "https://api.github.com/repos/octo-org/octo-repo/notifications{?since,all,participating}",
598+
"open_issues": 23,
599+
"open_issues_count": 23,
600+
"owner": {
601+
"avatar_url": "https://avatars3.githubusercontent.com/u/6811672?v=4",
602+
"events_url": "https://api.github.com/users/octo-org/events{/privacy}",
603+
"followers_url": "https://api.github.com/users/octo-org/followers",
604+
"following_url": "https://api.github.com/users/octo-org/following{/other_user}",
605+
"gists_url": "https://api.github.com/users/octo-org/gists{/gist_id}",
606+
"gravatar_id": "",
607+
"html_url": "https://github.com/octo-org",
608+
"id": 6811672,
609+
"login": "octo-org",
610+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjY4MTE2NzI=",
611+
"organizations_url": "https://api.github.com/users/octo-org/orgs",
612+
"received_events_url": "https://api.github.com/users/octo-org/received_events",
613+
"repos_url": "https://api.github.com/users/octo-org/repos",
614+
"site_admin": false,
615+
"starred_url": "https://api.github.com/users/octo-org/starred{/owner}{/repo}",
616+
"subscriptions_url": "https://api.github.com/users/octo-org/subscriptions",
617+
"type": "Organization",
618+
"url": "https://api.github.com/users/octo-org",
619+
},
620+
"private": true,
621+
"pulls_url": "https://api.github.com/repos/octo-org/octo-repo/pulls{/number}",
622+
"pushed_at": "2018-10-10T15:58:47Z",
623+
"releases_url": "https://api.github.com/repos/octo-org/octo-repo/releases{/id}",
624+
"size": 59,
625+
"ssh_url": "git@github.com:octo-org/octo-repo.git",
626+
"stargazers_count": 0,
627+
"stargazers_url": "https://api.github.com/repos/octo-org/octo-repo/stargazers",
628+
"statuses_url": "https://api.github.com/repos/octo-org/octo-repo/statuses/{sha}",
629+
"subscribers_url": "https://api.github.com/repos/octo-org/octo-repo/subscribers",
630+
"subscription_url": "https://api.github.com/repos/octo-org/octo-repo/subscription",
631+
"svn_url": "https://github.com/octo-org/octo-repo",
632+
"tags_url": "https://api.github.com/repos/octo-org/octo-repo/tags",
633+
"teams_url": "https://api.github.com/repos/octo-org/octo-repo/teams",
634+
"topics": [],
635+
"trees_url": "https://api.github.com/repos/octo-org/octo-repo/git/trees{/sha}",
636+
"updated_at": "2018-10-10T15:58:51Z",
637+
"url": "https://api.github.com/repos/octo-org/octo-repo",
638+
"visibility": "public",
639+
"watchers": 0,
640+
"watchers_count": 0,
641+
"web_commit_signoff_required": false,
642+
},
643+
"sender": {
644+
"avatar_url": "https://avatars1.githubusercontent.com/u/21031067?v=4",
645+
"events_url": "https://api.github.com/users/Codertocat/events{/privacy}",
646+
"followers_url": "https://api.github.com/users/Codertocat/followers",
647+
"following_url": "https://api.github.com/users/Codertocat/following{/other_user}",
648+
"gists_url": "https://api.github.com/users/Codertocat/gists{/gist_id}",
649+
"gravatar_id": "",
650+
"html_url": "https://github.com/Codertocat",
651+
"id": 21031067,
652+
"login": "Codertocat",
653+
"node_id": "MDQ6VXNlcjIxMDMxMDY3",
654+
"organizations_url": "https://api.github.com/users/Codertocat/orgs",
655+
"received_events_url": "https://api.github.com/users/Codertocat/received_events",
656+
"repos_url": "https://api.github.com/users/Codertocat/repos",
657+
"site_admin": false,
658+
"starred_url": "https://api.github.com/users/Codertocat/starred{/owner}{/repo}",
659+
"subscriptions_url": "https://api.github.com/users/Codertocat/subscriptions",
660+
"type": "User",
661+
"url": "https://api.github.com/users/Codertocat",
662+
},
663+
"workflow": ".github/workflows/hello-world-workflow.yml",
664+
}
665+
`;

__tests__/common/github/context.test.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import {beforeEach, describe, expect, test, vi} from 'vitest'
22

3-
describe('getGitHubContext', () => {
3+
import {stubTestEnvVars} from '@/tests/helpers/env.js'
4+
5+
describe('useContext', () => {
46
beforeEach(() => {
57
vi.resetModules()
68
})
79

8-
test('returns eventName', async () => {
10+
test('returns context for `pull_request`', async () => {
911
expect.assertions(8)
1012

1113
const {useContext} = await import('@/common/github/context.js')
@@ -31,4 +33,33 @@ describe('getGitHubContext', () => {
3133
expect(graphqlEndpoint).toBe(`https://api.github.com/graphql`)
3234
expect(ref).toBe(`mock-github-head-ref`)
3335
})
36+
37+
test('returns context for `workflow_dispatch`', async () => {
38+
expect.assertions(8)
39+
40+
stubTestEnvVars('workflow_dispatch')
41+
42+
vi.stubEnv('GITHUB_HEAD_REF', '')
43+
44+
const {useContext} = await import('@/common/github/context.js')
45+
46+
const {repo, event, branch, sha, graphqlEndpoint, ref} = useContext()
47+
48+
expect(repo).toStrictEqual({
49+
node_id: 'MDEwOlJlcG9zaXRvcnkxNzI3MzA1MQ==',
50+
owner: 'andykenward',
51+
repo: 'github-actions-cloudflare-pages'
52+
})
53+
54+
expect(event.payload).toBeDefined()
55+
expect(event.payload).toMatchSnapshot()
56+
expect(event.eventName).toBe('workflow_dispatch')
57+
58+
expect(branch).toBe(`mock-github-ref-name`)
59+
expect(sha).toBe(`mock-github-sha`)
60+
expect(graphqlEndpoint).toBe(`https://api.github.com/graphql`)
61+
expect(ref).toBe(`refs/heads/master`)
62+
63+
vi.unstubAllEnvs()
64+
})
3465
})

0 commit comments

Comments
 (0)