-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathinitializeBranchProtectionSettings.test.ts
166 lines (152 loc) · 4.14 KB
/
initializeBranchProtectionSettings.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { Octokit } from "octokit";
import { describe, expect, it, MockInstance, vi } from "vitest";
import { Options } from "../shared/types.js";
import { initializeBranchProtectionSettings } from "./initializeGitHubRepository/initializeBranchProtectionSettings.js";
const createMockOctokit = (request: MockInstance) =>
({
request,
}) as unknown as Octokit;
const stubOptions = {
access: "public",
description: "",
directory: "",
email: {
github: "",
npm: "",
},
mode: "create",
owner: "",
repository: "",
title: "",
} satisfies Options;
describe("migrateBranchProtectionSettings", () => {
it("does not throw when the request receives a non-error response", async () => {
const mockRequest = vi.fn().mockResolvedValue({ status: 200 });
await expect(
initializeBranchProtectionSettings(
createMockOctokit(mockRequest),
stubOptions,
),
).resolves.not.toThrow();
expect(mockRequest.mock.calls).toMatchInlineSnapshot(`
[
[
"PUT /repos///branches/main/protection",
{
"allow_deletions": false,
"allow_force_pushes": true,
"allow_fork_pushes": false,
"allow_fork_syncing": true,
"block_creations": false,
"branch": "main",
"enforce_admins": false,
"owner": "",
"repo": "",
"required_conversation_resolution": true,
"required_linear_history": false,
"required_pull_request_reviews": null,
"required_status_checks": {
"checks": [
{
"context": "Build",
},
{
"context": "Compliance",
},
{
"context": "Lint",
},
{
"context": "Lint Knip",
},
{
"context": "Lint Markdown",
},
{
"context": "Lint Packages",
},
{
"context": "Lint Spelling",
},
{
"context": "Prettier",
},
{
"context": "Test",
},
],
"strict": false,
},
"restrictions": null,
},
],
]
`);
});
it("returns false when the request receives a 403 response", async () => {
const mockRequest = vi.fn().mockRejectedValue({ status: 403 });
const actual = await initializeBranchProtectionSettings(
createMockOctokit(mockRequest),
stubOptions,
);
expect(actual).toBe(false);
});
it("throws the error when the request throws with a non-403 response", async () => {
const error = { status: 404 };
const mockRequest = vi.fn().mockRejectedValue(error);
await expect(() =>
initializeBranchProtectionSettings(
createMockOctokit(mockRequest),
stubOptions,
),
).rejects.toBe(error);
});
it("doesn't create workflows for excluded options when specified", async () => {
const mockRequest = vi.fn().mockResolvedValue({ status: 200 });
await initializeBranchProtectionSettings(createMockOctokit(mockRequest), {
...stubOptions,
excludeCompliance: true,
excludeLintKnip: true,
excludeLintMd: true,
excludeLintPackages: true,
excludeLintSpelling: true,
excludeTests: true,
});
expect(mockRequest.mock.calls).toMatchInlineSnapshot(`
[
[
"PUT /repos///branches/main/protection",
{
"allow_deletions": false,
"allow_force_pushes": true,
"allow_fork_pushes": false,
"allow_fork_syncing": true,
"block_creations": false,
"branch": "main",
"enforce_admins": false,
"owner": "",
"repo": "",
"required_conversation_resolution": true,
"required_linear_history": false,
"required_pull_request_reviews": null,
"required_status_checks": {
"checks": [
{
"context": "Build",
},
{
"context": "Lint",
},
{
"context": "Prettier",
},
],
"strict": false,
},
"restrictions": null,
},
],
]
`);
});
});