|
1 |
| -import fetchMock from "fetch-mock"; |
2 | 1 | import { Octokit } from "@octokit/core";
|
| 2 | +import fetchMock from "fetch-mock"; |
3 | 3 |
|
4 |
| -import { restEndpointMethods, legacyRestEndpointMethods } from "../src"; |
| 4 | +import sinon from "sinon"; |
| 5 | +import { legacyRestEndpointMethods, restEndpointMethods } from "../src"; |
| 6 | +import { Api } from "../src/types"; |
5 | 7 |
|
6 | 8 | describe("REST API endpoint methods", () => {
|
7 | 9 | it("README example", async () => {
|
@@ -175,6 +177,100 @@ describe("REST API endpoint methods", () => {
|
175 | 177 | return octokit.rest.apps.listInstallations();
|
176 | 178 | });
|
177 | 179 |
|
| 180 | + describe("mocking", () => { |
| 181 | + let octokit: Octokit & Api; |
| 182 | + |
| 183 | + beforeEach(() => { |
| 184 | + const networkMock = fetchMock |
| 185 | + .sandbox() |
| 186 | + .getOnce( |
| 187 | + "https://api.github.com/repos/octokit/plugin-rest-endpoint-methods/issues/1/labels", |
| 188 | + [{ name: "mocked from network" }], |
| 189 | + ); |
| 190 | + |
| 191 | + const MyOctokit = Octokit.plugin(restEndpointMethods); |
| 192 | + octokit = new MyOctokit({ |
| 193 | + request: { |
| 194 | + fetch: networkMock, |
| 195 | + }, |
| 196 | + }); |
| 197 | + }); |
| 198 | + |
| 199 | + afterEach(async () => { |
| 200 | + const restoredResult = await octokit.rest.issues.listLabelsOnIssue({ |
| 201 | + owner: "octokit", |
| 202 | + repo: "plugin-rest-endpoint-methods", |
| 203 | + issue_number: 1, |
| 204 | + }); |
| 205 | + expect(restoredResult.data[0].name).toBe("mocked from network"); |
| 206 | + }); |
| 207 | + |
| 208 | + it("allows mocking with sinon.stub", async () => { |
| 209 | + const stub = sinon |
| 210 | + .stub(octokit.rest.issues, "listLabelsOnIssue") |
| 211 | + .resolves({ data: [{ name: "mocked from sinon" }] } as Awaited< |
| 212 | + ReturnType<typeof octokit.rest.issues.listLabelsOnIssue> |
| 213 | + >); |
| 214 | + |
| 215 | + const sinonResult = await octokit.rest.issues.listLabelsOnIssue({ |
| 216 | + owner: "octokit", |
| 217 | + repo: "plugin-rest-endpoint-methods", |
| 218 | + issue_number: 1, |
| 219 | + }); |
| 220 | + expect(sinonResult.data[0].name).toBe("mocked from sinon"); |
| 221 | + |
| 222 | + stub.restore(); |
| 223 | + }); |
| 224 | + |
| 225 | + it("allows mocking with jest.spyOn", async () => { |
| 226 | + jest |
| 227 | + .spyOn(octokit.rest.issues, "listLabelsOnIssue") |
| 228 | + .mockResolvedValueOnce({ |
| 229 | + data: [{ name: "mocked from jest" }], |
| 230 | + } as Awaited<ReturnType<typeof octokit.rest.issues.listLabelsOnIssue>>); |
| 231 | + |
| 232 | + const jestResult = await octokit.rest.issues.listLabelsOnIssue({ |
| 233 | + owner: "octokit", |
| 234 | + repo: "plugin-rest-endpoint-methods", |
| 235 | + issue_number: 1, |
| 236 | + }); |
| 237 | + expect(jestResult.data[0].name).toBe("mocked from jest"); |
| 238 | + |
| 239 | + jest.restoreAllMocks(); |
| 240 | + }); |
| 241 | + |
| 242 | + it("allows manually replacing a method", async () => { |
| 243 | + const oldImplementation = octokit.rest.issues.listLabelsOnIssue; |
| 244 | + |
| 245 | + octokit.rest.issues.listLabelsOnIssue = (async () => { |
| 246 | + return { |
| 247 | + data: [{ name: "mocked from custom implementation" }], |
| 248 | + } as Awaited<ReturnType<typeof octokit.rest.issues.listLabelsOnIssue>>; |
| 249 | + }) as unknown as typeof oldImplementation; |
| 250 | + |
| 251 | + const customResult = await octokit.rest.issues.listLabelsOnIssue({ |
| 252 | + owner: "octokit", |
| 253 | + repo: "plugin-rest-endpoint-methods", |
| 254 | + issue_number: 1, |
| 255 | + }); |
| 256 | + expect(customResult.data[0].name).toBe( |
| 257 | + "mocked from custom implementation", |
| 258 | + ); |
| 259 | + |
| 260 | + delete (octokit.rest.issues as any).listLabelsOnIssue; |
| 261 | + octokit.rest.issues.listLabelsOnIssue = oldImplementation; |
| 262 | + }); |
| 263 | + }); |
| 264 | + |
| 265 | + it("lists all methods (e.g. with tab-tab in a REPL)", async () => { |
| 266 | + const MyOctokit = Octokit.plugin(restEndpointMethods); |
| 267 | + const octokit = new MyOctokit(); |
| 268 | + |
| 269 | + const methods = Object.keys(octokit.rest.issues); |
| 270 | + |
| 271 | + expect(methods).toContain("listLabelsOnIssue"); |
| 272 | + }); |
| 273 | + |
178 | 274 | // besides setting `octokit.rest.*`, the plugin exports legacyRestEndpointMethods
|
179 | 275 | // which is also setting the same methods directly on `octokit.*` for legacy reasons.
|
180 | 276 | // We will deprecate the `octokit.*` methods in future, but for now we just make sure they are set
|
|
0 commit comments