Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
🧹 Add tests that run the whole action (#149)
- Loading branch information
Patrick Ellis
authored and
GitHub
committed
Jun 4, 2021
1 parent
b884ad6
commit c9d0dfc
Showing
4 changed files
with
151 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
export const context = { | ||
payload: { | ||
pull_request: { | ||
number: 123, | ||
}, | ||
}, | ||
repo: { | ||
owner: "monalisa", | ||
repo: "helloworld", | ||
}, | ||
}; | ||
|
||
const mockApi = { | ||
issues: { | ||
addLabels: jest.fn(), | ||
removeLabel: jest.fn(), | ||
}, | ||
paginate: jest.fn(), | ||
pulls: { | ||
get: jest.fn().mockResolvedValue({}), | ||
listFiles: { | ||
endpoint: { | ||
merge: jest.fn().mockReturnValue({}), | ||
}, | ||
}, | ||
}, | ||
repos: { | ||
getContents: jest.fn(), | ||
}, | ||
}; | ||
|
||
export const GitHub = jest.fn().mockImplementation(() => mockApi); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
touched-a-pdf-file: | ||
- any: ['*.pdf'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import { run } from "../src/labeler"; | ||
import { GitHub } from "@actions/github"; | ||
import * as core from "@actions/core"; | ||
|
||
const fs = jest.requireActual("fs"); | ||
|
||
jest.mock("@actions/core"); | ||
jest.mock("@actions/github"); | ||
|
||
const gh = new GitHub("_"); | ||
const addLabelsMock = jest.spyOn(gh.issues, "addLabels"); | ||
const removeLabelMock = jest.spyOn(gh.issues, "removeLabel"); | ||
const reposMock = jest.spyOn(gh.repos, "getContents"); | ||
const paginateMock = jest.spyOn(gh, "paginate"); | ||
const getPullMock = jest.spyOn(gh.pulls, "get"); | ||
|
||
const yamlFixtures = { | ||
"only_pdfs.yml": fs.readFileSync("__tests__/fixtures/only_pdfs.yml"), | ||
}; | ||
|
||
afterAll(() => jest.restoreAllMocks()); | ||
|
||
describe("run", () => { | ||
it("adds labels to PRs that match our glob patterns", async () => { | ||
usingLabelerConfigYaml("only_pdfs.yml"); | ||
mockGitHubResponseChangedFiles("foo.pdf"); | ||
|
||
await run(); | ||
|
||
expect(removeLabelMock).toHaveBeenCalledTimes(0); | ||
expect(addLabelsMock).toHaveBeenCalledTimes(1); | ||
expect(addLabelsMock).toHaveBeenCalledWith({ | ||
owner: "monalisa", | ||
repo: "helloworld", | ||
issue_number: 123, | ||
labels: ["touched-a-pdf-file"], | ||
}); | ||
}); | ||
|
||
it("does not add labels to PRs that do not match our glob patterns", async () => { | ||
usingLabelerConfigYaml("only_pdfs.yml"); | ||
mockGitHubResponseChangedFiles("foo.txt"); | ||
|
||
await run(); | ||
|
||
expect(removeLabelMock).toHaveBeenCalledTimes(0); | ||
expect(addLabelsMock).toHaveBeenCalledTimes(0); | ||
}); | ||
|
||
it("(with sync-labels: true) it deletes preexisting PR labels that no longer match the glob pattern", async () => { | ||
let mockInput = { | ||
"repo-token": "foo", | ||
"configuration-path": "bar", | ||
"sync-labels": true, | ||
}; | ||
|
||
jest | ||
.spyOn(core, "getInput") | ||
.mockImplementation((name: string, ...opts) => mockInput[name]); | ||
|
||
usingLabelerConfigYaml("only_pdfs.yml"); | ||
mockGitHubResponseChangedFiles("foo.txt"); | ||
getPullMock.mockResolvedValue(<any>{ | ||
data: { | ||
labels: [{ name: "touched-a-pdf-file" }], | ||
}, | ||
}); | ||
|
||
await run(); | ||
|
||
expect(addLabelsMock).toHaveBeenCalledTimes(0); | ||
expect(removeLabelMock).toHaveBeenCalledTimes(1); | ||
expect(removeLabelMock).toHaveBeenCalledWith({ | ||
owner: "monalisa", | ||
repo: "helloworld", | ||
issue_number: 123, | ||
name: "touched-a-pdf-file", | ||
}); | ||
}); | ||
|
||
it("(with sync-labels: false) it issues no delete calls even when there are preexisting PR labels that no longer match the glob pattern", async () => { | ||
let mockInput = { | ||
"repo-token": "foo", | ||
"configuration-path": "bar", | ||
"sync-labels": false, | ||
}; | ||
|
||
jest | ||
.spyOn(core, "getInput") | ||
.mockImplementation((name: string, ...opts) => mockInput[name]); | ||
|
||
usingLabelerConfigYaml("only_pdfs.yml"); | ||
mockGitHubResponseChangedFiles("foo.txt"); | ||
getPullMock.mockResolvedValue(<any>{ | ||
data: { | ||
labels: [{ name: "touched-a-pdf-file" }], | ||
}, | ||
}); | ||
|
||
await run(); | ||
|
||
expect(addLabelsMock).toHaveBeenCalledTimes(0); | ||
expect(removeLabelMock).toHaveBeenCalledTimes(0); | ||
}); | ||
}); | ||
|
||
function usingLabelerConfigYaml(fixtureName: keyof typeof yamlFixtures): void { | ||
reposMock.mockResolvedValue(<any>{ | ||
data: { content: yamlFixtures[fixtureName], encoding: "utf8" }, | ||
}); | ||
} | ||
|
||
function mockGitHubResponseChangedFiles(...files: string[]): void { | ||
const returnValue = files.map((f) => ({ filename: f })); | ||
paginateMock.mockReturnValue(<any>returnValue); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters