Skip to content

Commit

Permalink
Merge pull request #8 from n0th1ng-else/test-3
Browse files Browse the repository at this point in the history
  • Loading branch information
n0th1ng-else authored Nov 16, 2021
2 parents 765a126 + 4ae5fc0 commit b366e1e
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ const getStrictGithubStrategyCommit = async (commits) => {
return prCommit;
}

throw new Error("Commit is not consistent with the pull request description");
throw new Error(
"The pull request description is not equal to the first commit body"
);
};

const STRATEGY = {
Expand Down
290 changes: 284 additions & 6 deletions src/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ describe("utils", () => {
);
});

describe("github", () => {
describe("github strategy", () => {
it("returns commit data if there is a single commit", () => {
setEnv();
const strategy = "github";

const commits = [
{
subject: "Commit title",
Expand All @@ -76,8 +78,23 @@ describe("utils", () => {
});
});

it("returns pull data if there is more than 1 commit", () => {
it("returns pull request data if there is more than 1 commit", () => {
setEnv();
const strategy = "github";

const commits = [
{
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
},
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() =>
Promise.resolve({
data: {
Expand All @@ -86,7 +103,21 @@ describe("utils", () => {
},
})
);

return getCommit(strategy, commits).then((commit) => {
expect(commit).toEqual({
subject: "pr title",
body: "* Commit title 1\n\ndescription 1\n\n* Commit title 2\n\ndescription 2",
message:
"pr title\n\n* Commit title 1\n\ndescription 1\n\n* Commit title 2\n\ndescription 2",
});
});
});

it("throws an error when the github API is not available", () => {
setEnv();
const strategy = "github";

const commits = [
{
subject: "Commit title 1",
Expand All @@ -100,34 +131,248 @@ describe("utils", () => {
},
];

setMockResult(() => Promise.reject(new Error("Where is it?")));

return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
},
(err) => {
expect(err.message).toBe("Where is it?");
}
);
});
});

describe("pull-request strategy", () => {
it("returns pull request data if there is a single commit", () => {
setEnv();
const strategy = "pull-request";

const commits = [
{
subject: "Commit title",
body: "description",
message: "Commit title\n\ndescription",
},
];

setMockResult(() =>
Promise.resolve({
data: {
title: "pr title",
body: "pr body",
},
})
);

return getCommit(strategy, commits).then((commit) => {
expect(commit).toEqual({
subject: "pr title",
body: "* Commit title 1\n\ndescription 1\n\n* Commit title 2\n\ndescription 2",
message:
"pr title\n\n* Commit title 1\n\ndescription 1\n\n* Commit title 2\n\ndescription 2",
body: "pr body",
message: "pr title\n\npr body",
});
});
});

it("returns pull request data if there is more than 1 commit", () => {
setEnv();
const strategy = "pull-request";

const commits = [
{
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
},
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() =>
Promise.resolve({
data: {
title: "pr title",
body: "pr body",
},
})
);

return getCommit(strategy, commits).then((commit) => {
expect(commit).toEqual({
subject: "pr title",
body: "pr body",
message: "pr title\n\npr body",
});
});
});

it("throws an error when the github API is not available", () => {
setEnv();
const strategy = "pull-request";

const commits = [
{
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
},
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() => Promise.reject(new Error("Where is it?")));

const strategy = "github";
return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
},
(err) => {
expect(err.message).toBe("Where is it?");
}
);
});

it("throws an error when the github API is not available (single commit)", () => {
setEnv();
const strategy = "pull-request";

const commits = [
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() => Promise.reject(new Error("Where is it?")));

return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
},
(err) => {
expect(err.message).toBe("Where is it?");
}
);
});
});

describe("strict-pull-request strategy", () => {
it("returns pull request data if there is a single commit", () => {
setEnv();
const strategy = "strict-pull-request";

const commits = [
{
subject: "Commit title",
body: "description",
message: "Commit title\n\ndescription",
},
];

setMockResult(() =>
Promise.resolve({
data: {
title: "Commit title",
body: "description",
},
})
);

return getCommit(strategy, commits).then((commit) => {
expect(commit).toEqual({
subject: "Commit title",
body: "description",
message: "Commit title\n\ndescription",
});
});
});

it("returns pull request data if there is more than 1 commit", () => {
setEnv();
const strategy = "strict-pull-request";

const commits = [
{
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
},
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() =>
Promise.resolve({
data: {
title: "Commit title 1",
body: "description 1",
},
})
);

return getCommit(strategy, commits).then((commit) => {
expect(commit).toEqual({
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
});
});
});

it("throws an error when the github API is not available", () => {
setEnv();
const strategy = "strict-pull-request";

const commits = [
{
subject: "Commit title 1",
body: "description 1",
message: "Commit title 1\n\ndescription 1",
},
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() => Promise.reject(new Error("Where is it?")));

return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
},
(err) => {
expect(err.message).toBe("Where is it?");
}
);
});

it("throws an error when the github API is not available (single commit)", () => {
setEnv();
const strategy = "strict-pull-request";

const commits = [
{
subject: "Commit title 2",
body: "description 2",
message: "Commit title 2\n\ndescription 2",
},
];

setMockResult(() => Promise.reject(new Error("Where is it?")));

return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
Expand All @@ -137,6 +382,39 @@ describe("utils", () => {
}
);
});

it("throws an error when the pull request description is not eq to the first commit body", () => {
setEnv();
const strategy = "strict-pull-request";

const commits = [
{
subject: "Commit title",
body: "description",
message: "Commit title\n\ndescription",
},
];

setMockResult(() =>
Promise.resolve({
data: {
title: "pr title",
body: "pr body",
},
})
);

return getCommit(strategy, commits).then(
() => {
throw new Error("Should not be there");
},
(err) => {
expect(err.message).toBe(
"The pull request description is not equal to the first commit body"
);
}
);
});
});
});
});

0 comments on commit b366e1e

Please sign in to comment.