Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@
your browser.
</div>
</noscript>
<div
id="download-alert"
class="alert alert-warning alert-dismissible fade show d-none"
role="alert"
tabindex="-1">
<strong>Having trouble?</strong>
If the generated workflow does not automatically appear in your workflow
editor, manually download
<a href="#" id="download-alert-link">matlab.yml</a>
and save it to the
<code>.github/workflows</code>
folder of your repository.
<button
type="button"
class="btn-close"
data-bs-dismiss="alert"
aria-label="Close"></button>
</div>
<main class="container py-5">
<div class="text-center mt-4 mb-4">
<img
Expand Down
25 changes: 25 additions & 0 deletions public/scripts/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,36 @@ function handleFormSubmit(e) {
url += `/${repoInfo.owner}/${repoInfo.repo}/new/main?filename=${filePath}&value=${encoded}`;

window.navigateTo(url);

showDownloadAlert();
}

function showDownloadAlert() {
const alert = document.getElementById("download-alert");
alert.classList.remove("d-none");
alert.focus();
}

function handleDownloadClick(e) {
e.preventDefault();

const workflow = generateWorkflowWithFormInputs();

const blob = new Blob([workflow], { type: "text/yaml" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "matlab.yml";
a.click();
URL.revokeObjectURL(url);
}

document
.getElementById("generate-form")
.addEventListener("submit", handleFormSubmit);
document
.getElementById("download-alert-link")
.addEventListener("click", handleDownloadClick);

document.querySelectorAll('[data-bs-toggle="tooltip"]').forEach((el) => {
new bootstrap.Tooltip(el);
Expand Down
9 changes: 9 additions & 0 deletions public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,12 @@ p.lead.text-muted {
.bi-question-circle {
color: #0076a8;
}

#download-alert {
text-align: center;
position: fixed;
top: 5px;
left: 2%;
width: 96%;
z-index: 2000;
}
29 changes: 28 additions & 1 deletion tests/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ beforeEach(async () => {
<input type="checkbox" id="build-across-platforms" />
<button type="submit" id="generate-button"></button>
</form>
<a id="downloadButton"></a>
<a id="download-alert-link"></a>
<div id="download-alert" class="d-none"></div>
`;

await import("../public/scripts/main.js");
Expand Down Expand Up @@ -98,3 +99,29 @@ test("advanced options are passed to generateWorkflow", async () => {
jsyaml: window.jsyaml,
});
});

test("download link triggers file download", () => {
const repoInput = document.getElementById("repo");
repoInput.value = "owner/repo";

window.jsyaml = { dump: () => "yaml-content" };

const mockCreateObjectURL = jest.fn(() => "blob:url");
const mockRevokeObjectURL = jest.fn();
global.URL.createObjectURL = mockCreateObjectURL;
global.URL.revokeObjectURL = mockRevokeObjectURL;

const a = document.createElement("a");
document.body.appendChild(a);
jest.spyOn(document, "createElement").mockImplementation((tag) => {
if (tag === "a") return a;
return document.createElement(tag);
});
const clickSpy = jest.spyOn(a, "click");

document.getElementById("download-alert-link").click();

expect(mockCreateObjectURL).toHaveBeenCalled();
expect(clickSpy).toHaveBeenCalled();
expect(mockRevokeObjectURL).toHaveBeenCalled();
});