|
| 1 | +import parse from "@changesets/parse"; |
| 2 | +import test from "ava"; |
| 3 | +import sinon from "sinon"; |
| 4 | +import changelogFunctions from "./index.js"; |
| 5 | + |
| 6 | +/** @type {sinon.SinonSandbox} */ |
| 7 | +let sandbox; |
| 8 | + |
| 9 | +const data = { |
| 10 | + commit: "a085003", |
| 11 | + user: "Andarist", |
| 12 | + pull: 1613, |
| 13 | + repo: "emotion-js/emotion", |
| 14 | +}; |
| 15 | + |
| 16 | +test.beforeEach((t) => { |
| 17 | + sandbox = sinon.createSandbox(); |
| 18 | + const stub = sandbox.stub("@changesets/get-github-info"); |
| 19 | + stub.getInfo.returns({ |
| 20 | + pull: data.pull, |
| 21 | + user: data.user, |
| 22 | + links: { |
| 23 | + user: `[@${data.user}](https://github.com/${data.user})`, |
| 24 | + pull: `[#${data.pull}](https://github.com/${data.repo}/pull/${data.pull})`, |
| 25 | + commit: `[\`${data.commit}\`](https://github.com/${data.repo}/commit/${data.commit})`, |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + stub.getInfoFromPullRequest.returns({ |
| 30 | + commit: data.commit, |
| 31 | + user: data.user, |
| 32 | + links: { |
| 33 | + user: `[@${data.user}](https://github.com/${data.user})`, |
| 34 | + pull: `[#${data.pull}](https://github.com/${data.repo}/pull/${data.pull})`, |
| 35 | + commit: `[\`${data.commit}\`](https://github.com/${data.repo}/commit/${data.commit})`, |
| 36 | + }, |
| 37 | + }); |
| 38 | +}); |
| 39 | + |
| 40 | +test.afterEach.always(() => { |
| 41 | + sandbox.restore(); |
| 42 | +}); |
| 43 | + |
| 44 | +/** |
| 45 | + * |
| 46 | + * @param {string} content |
| 47 | + * @param {string|undefined} commit |
| 48 | + * @returns |
| 49 | + */ |
| 50 | +const getChangeset = (content, commit) => { |
| 51 | + return [ |
| 52 | + { |
| 53 | + ...parse( |
| 54 | + `--- |
| 55 | + pkg: "minor" |
| 56 | + --- |
| 57 | +
|
| 58 | + something |
| 59 | + ${content} |
| 60 | + ` |
| 61 | + ), |
| 62 | + id: "some-id", |
| 63 | + commit, |
| 64 | + }, |
| 65 | + "minor", |
| 66 | + { repo: data.repo }, |
| 67 | + ]; |
| 68 | +}; |
| 69 | + |
| 70 | +[data.commit, "wrongcommit", undefined].forEach((commitFromChangeset) => { |
| 71 | + ["pr", "pull request", "pull"].forEach((keyword) => { |
| 72 | + test(`with commit from changeset of ${commitFromChangeset} override pr with ${keyword} keyword with #`, async (t) => { |
| 73 | + t.is( |
| 74 | + await changelogFunctions.getReleaseLine( |
| 75 | + ...getChangeset( |
| 76 | + `${keyword}: #${data.pull}`, |
| 77 | + commitFromChangeset |
| 78 | + ) |
| 79 | + ), |
| 80 | + "\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\n" |
| 81 | + ); |
| 82 | + }); |
| 83 | + |
| 84 | + test(`with commit from changeset of ${commitFromChangeset} override pr with pr ${keyword} without #`, async (t) => { |
| 85 | + t.is( |
| 86 | + await changelogFunctions.getReleaseLine( |
| 87 | + ...getChangeset( |
| 88 | + `pr: ${data.pull}`, |
| 89 | + commitFromChangeset |
| 90 | + ) |
| 91 | + ), |
| 92 | + "\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\n" |
| 93 | + ); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + test(`override commit ${commitFromChangeset} with commit keyword`, async (t) => { |
| 98 | + t.is( |
| 99 | + await changelogFunctions.getReleaseLine( |
| 100 | + ...getChangeset(`commit: ${data.commit}`, commitFromChangeset) |
| 101 | + ), |
| 102 | + "\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist)! - something\n" |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +["author", "user"].forEach((keyword) => { |
| 108 | + test(`override author with ${keyword} keyword with @`, async (t) => { |
| 109 | + t.is( |
| 110 | + await changelogFunctions.getReleaseLine( |
| 111 | + ...getChangeset( |
| 112 | + `${keyword}: @other`, |
| 113 | + data.commit |
| 114 | + ) |
| 115 | + ), |
| 116 | + "\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@other](https://github.com/other)!\n\nsomething\n" |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + test(`override author with ${keyword} keyword without @`, async (t) => { |
| 121 | + t.is( |
| 122 | + await changelogFunctions.getReleaseLine( |
| 123 | + ...getChangeset( |
| 124 | + `${keyword}: other`, |
| 125 | + data.commit |
| 126 | + ) |
| 127 | + ), |
| 128 | + "\n\n- [#1613](https://github.com/emotion-js/emotion/pull/1613) [`a085003`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@other](https://github.com/other)!\n\nsomething\n" |
| 129 | + ); |
| 130 | + }); |
| 131 | +}); |
| 132 | + |
| 133 | +test("with multiple authors", async (t) => { |
| 134 | + t.is( |
| 135 | + await changelogFunctions.getReleaseLine( |
| 136 | + ...getChangeset( |
| 137 | + ["author: @Andarist", "author: @mitchellhamilton"].join("\n"), |
| 138 | + data.commit |
| 139 | + ) |
| 140 | + ), |
| 141 | + ` |
| 142 | + - [#1613](https://github.com/emotion-js/emotion/pull/1613) [\`a085003\`](https://github.com/emotion-js/emotion/commit/a085003) Thanks [@Andarist](https://github.com/Andarist), [@mitchellhamilton](https://github.com/mitchellhamilton)!\n\nsomething`); |
| 143 | +}); |
0 commit comments