Files
notifications-admin/tests/javascripts/fileUpload.test.js

187 lines
4.9 KiB
JavaScript
Raw Normal View History

2019-08-12 17:00:39 +01:00
const helpers = require('./support/helpers.js');
2025-04-01 13:03:12 -04:00
const { announceUploadStatusFromElement } = require('../../app/assets/javascripts/fileUpload.js');
2019-08-12 17:00:39 +01:00
afterAll(() => {
require('./support/teardown.js');
});
2025-04-01 13:03:12 -04:00
2019-08-12 17:00:39 +01:00
describe('File upload', () => {
let form;
let fileUpload;
beforeEach(() => {
// set up DOM
document.body.innerHTML = `
<form method="post" enctype="multipart/form-data" class="" data-module="file-upload">
<label class="file-upload-label" for="file">
<span class="visually-hidden">Upload a PNG logo</span>
</label>
<input class="file-upload-field" id="file" name="file" type="file">
<label class="file-upload-button" for="file">
Upload logo
</label>
<label class="file-upload-filename" for="file"></label>
2025-07-02 18:14:22 -04:00
<button type="submit" class="usa-button file-upload-submit">Submit</button>
2019-08-12 17:00:39 +01:00
</form>`;
form = document.querySelector('form');
uploadControl = form.querySelector('input[type=file]');
});
afterEach(() => {
document.body.innerHTML = '';
});
test("If the page loads, from a new or existing navigation, the form should reset", () => {
form.reset = jest.fn(() => {});
// start module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2019-08-12 17:00:39 +01:00
helpers.triggerEvent(window, 'pageshow');
expect(form.reset).toHaveBeenCalled();
});
describe("If the state of the upload form control changes", () => {
beforeEach(() => {
form.submit = jest.fn(() => {});
// start module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2019-08-12 17:00:39 +01:00
helpers.triggerEvent(uploadControl, 'change', { eventInit: { bubbles: true } });
});
test("The form should submit", () => {
expect(form.submit).toHaveBeenCalled();
});
test("It should display a disabled Uploading button", () => {
const uploadingButton = form.querySelector("button.uploading-button");
expect(uploadingButton).not.toBeNull();
expect(uploadingButton.textContent).toMatch(/Uploading/);
expect(uploadingButton.getAttribute('aria-disabled')).toBe("true");
2019-08-12 17:00:39 +01:00
});
});
});
2025-04-01 13:03:12 -04:00
describe('File upload "upload-trigger" click handler', () => {
let form;
beforeEach(() => {
document.body.innerHTML = `
<form method="post" enctype="multipart/form-data" data-module="file-upload">
<button type="button" data-module="upload-trigger" data-file-input-id="test-file-input">Upload your file</button>
<input type="file" id="test-file-input" style="display:none;">
</form>
`;
form = document.querySelector('form');
// Register the module
Removed all govuk css (#2814) * Removed all govuk css * Updated reference files * Removing govuk js * Fixed casing for modules, removed unused page * Got more reference images * Updated template page * Removed govuk padding util * Updated hint to uswds hint * More govuk cleanup * Commiting backstopjs ref files * Fixed all unit tests that broke due to brittleness around govuk styling * Added new ref images * Final removal of govuk * Officially removed all govuk references * Updated reference file * Updated link to button * UI modernization * Cleanup * removed govuk escaping tests since they are no longer needed * Fix CodeQL security issue in escapeElementName function - Escape backslashes first before other special characters - Prevents potential double-escaping vulnerability - Addresses CodeQL alert about improper string escaping * Found more govuk removal. Fixed unit tests * Add missing pipeline check to pre-commit * updated test * Updated e2e test * More update to e2e test * Fixed another e2e test * Simple PR comments addressed * More updates * Updated backstop ref files * Refactored folder selection for non-admins * Updated redundant line * Updated tests to include correct mocks * Added more ref files * Addressing carlos comments * Addressing Carlo comments, cleanup of window initing * More cleanup and addressing carlo comments * Fixing a11 scan * Fixed a few issues with javascript * Fixed for pr * Fixing e2e tests * Tweaking e2e test * Added more ref files and cleaned up urls.js * Fixed bug with creating new template * Removed brittle test - addressed code ql comment * e2e race condition fix * More e2e test fixes * Updated e2e tests to not wait for text sent * Updated test to not wait for button click response * Made tear down more resilent if staging is down * reverted e2e test to what was working before main merge * Updated backstopRef images * Updated gulp to include job-polling differently
2025-10-06 09:38:54 -04:00
window.NotifyModules.start();
2025-04-01 13:03:12 -04:00
});
afterEach(() => {
document.body.innerHTML = '';
});
test('clicking upload trigger simulates file input click', () => {
const uploadButton = form.querySelector('[data-module="upload-trigger"]');
const fileInput = document.getElementById('test-file-input');
// Spy on fileInput.click
const clickSpy = jest.spyOn(fileInput, 'click').mockImplementation(() => {});
// Trigger the click
helpers.triggerEvent(uploadButton, 'click');
expect(clickSpy).toHaveBeenCalled();
clickSpy.mockRestore();
});
});
describe('announceUploadStatusFromElement', () => {
beforeEach(() => {
jest.useFakeTimers();
document.body.innerHTML = `
<div id="upload-status-live" aria-live="assertive" role="status" class="usa-sr-only"></div>
`;
});
afterEach(() => {
jest.useRealTimers();
document.body.innerHTML = '';
});
test('announces error message from #upload-error', () => {
document.body.innerHTML += `
<span id="upload-error" class="usa-sr-only">File upload failed</span>
`;
const srRegion = document.getElementById('upload-status-live');
// Call the function
announceUploadStatusFromElement();
// Confirm it clears first
expect(srRegion.textContent).toBe('');
// Fast-forward the timer
jest.advanceTimersByTime(300);
2025-04-01 13:03:12 -04:00
// Confirm it updates after delay
expect(srRegion.textContent).toBe('File upload failed\u00A0');
2025-04-01 13:03:12 -04:00
});
test('announces success message from #upload-success if no error is present', () => {
document.body.innerHTML += `
<span id="upload-success" class="usa-sr-only">File upload successful</span>
`;
const srRegion = document.getElementById('upload-status-live');
announceUploadStatusFromElement();
expect(srRegion.textContent).toBe('');
jest.advanceTimersByTime(301);
2025-04-01 13:03:12 -04:00
expect(srRegion.textContent).toBe('File upload successful\u00A0');
2025-04-01 13:03:12 -04:00
});
test('does nothing if neither success nor error is present', () => {
const srRegion = document.getElementById('upload-status-live');
srRegion.textContent = 'Old message';
announceUploadStatusFromElement();
// Should not clear or update if no message element is found
expect(srRegion.textContent).toBe('Old message');
jest.advanceTimersByTime(300);
2025-04-01 13:03:12 -04:00
// Still unchanged
expect(srRegion.textContent).toBe('Old message');
});
});