-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use button element for entire input replacement #5609
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
54a86c3
Replace the native file upload with a button
patrickpatrickpatrick 89aee09
Add aria-label to file upload button replacement
patrickpatrickpatrick 994920a
Update tests for new file upload changes
patrickpatrickpatrick 5d7fb7f
Correct focus for file upload in error summary
patrickpatrickpatrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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 |
---|---|---|
|
@@ -7,6 +7,7 @@ const inputSelector = '.govuk-file-upload' | |
const wrapperSelector = '.govuk-file-upload-wrapper' | ||
const buttonSelector = '.govuk-file-upload__button' | ||
const statusSelector = '.govuk-file-upload__status' | ||
const pseudoButtonSelector = '.govuk-file-upload__pseudo-button' | ||
|
||
describe('/components/file-upload', () => { | ||
let examples | ||
|
@@ -93,66 +94,59 @@ describe('/components/file-upload', () => { | |
}) | ||
|
||
it('renders the button with default text', async () => { | ||
const buttonElementText = await page.$eval(buttonSelector, (el) => | ||
el.innerHTML.trim() | ||
const buttonElementText = await page.$eval( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion Would it be worth checking that not only the elements this test looks for are on the page, but also inside the button itself? |
||
`${buttonSelector} ${pseudoButtonSelector}`, | ||
(el) => el.innerHTML.trim() | ||
) | ||
|
||
expect(buttonElementText).toBe('Choose file') | ||
}) | ||
}) | ||
|
||
describe('status element', () => { | ||
it('renders the status element', async () => { | ||
const statusElement = await page.$eval(statusSelector, (el) => el) | ||
|
||
expect(statusElement).toBeDefined() | ||
}) | ||
|
||
it('renders the status element with role', async () => { | ||
const statusElementRole = await page.$eval(statusSelector, (el) => | ||
el.getAttribute('role') | ||
const statusElementText = await page.$eval( | ||
`${buttonSelector} ${statusSelector}`, | ||
(el) => el.innerHTML.trim() | ||
) | ||
|
||
expect(statusElementRole).toBe('status') | ||
}) | ||
|
||
it('renders the status element with default text', async () => { | ||
const statusElementText = await page.$eval(statusSelector, (el) => | ||
el.innerHTML.trim() | ||
const buttonAriaText = await page.$eval(buttonSelector, (el) => | ||
el.getAttribute('aria-label') | ||
) | ||
|
||
expect(buttonElementText).toBe('Choose file') | ||
expect(statusElementText).toBe('No file chosen') | ||
expect(buttonAriaText).toBe( | ||
'Upload a file, Choose file, No file chosen' | ||
) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('when clicking the choose file button', () => { | ||
it('opens the file picker', async () => { | ||
// It doesn't seem to be possible to check if the file picker dialog | ||
// opens as an isolated test, so this test clicks the button, tries to | ||
// set a value in the file chooser, then checks if that value was set | ||
// on the input as expected. | ||
const testFilename = 'test.gif' | ||
await render(page, 'file-upload', examples.default) | ||
it.each([buttonSelector, pseudoButtonSelector, statusSelector])( | ||
'opens the file picker', | ||
async (selector) => { | ||
// It doesn't seem to be possible to check if the file picker dialog | ||
// opens as an isolated test, so this test clicks the button, tries to | ||
// set a value in the file chooser, then checks if that value was set | ||
// on the input as expected. | ||
const testFilename = 'test.gif' | ||
await render(page, 'file-upload', examples.default) | ||
|
||
const [fileChooser] = await Promise.all([ | ||
page.waitForFileChooser(), | ||
page.click(buttonSelector) | ||
]) | ||
const [fileChooser] = await Promise.all([ | ||
page.waitForFileChooser(), | ||
page.click(selector) | ||
]) | ||
|
||
await fileChooser.accept([testFilename]) | ||
await fileChooser.accept([testFilename]) | ||
|
||
const inputElementValue = await page.$eval( | ||
inputSelector, | ||
(el) => | ||
// @ts-ignore | ||
el.value | ||
) | ||
const inputElementValue = await page.$eval( | ||
inputSelector, | ||
(el) => | ||
// @ts-ignore | ||
el.value | ||
) | ||
|
||
// For Windows and backward compatibility, the values of file inputs | ||
// are always formatted starting with `C:\\fakepath\\` | ||
expect(inputElementValue).toBe(`C:\\fakepath\\${testFilename}`) | ||
}) | ||
// For Windows and backward compatibility, the values of file inputs | ||
// are always formatted starting with `C:\\fakepath\\` | ||
expect(inputElementValue).toBe(`C:\\fakepath\\${testFilename}`) | ||
} | ||
) | ||
}) | ||
|
||
describe('when selecting a file', () => { | ||
|
@@ -163,7 +157,7 @@ describe('/components/file-upload', () => { | |
|
||
const [fileChooser] = await Promise.all([ | ||
page.waitForFileChooser(), | ||
page.click(inputSelector) | ||
page.click(buttonSelector) | ||
romaricpascal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
]) | ||
await fileChooser.accept([testFilename]) | ||
}) | ||
|
@@ -206,7 +200,7 @@ describe('/components/file-upload', () => { | |
|
||
const [fileChooser] = await Promise.all([ | ||
page.waitForFileChooser(), | ||
page.click(inputSelector) | ||
page.click(buttonSelector) | ||
]) | ||
await fileChooser.accept(['testfile1.txt', 'testfile2.pdf']) | ||
}) | ||
|
@@ -354,11 +348,24 @@ describe('/components/file-upload', () => { | |
}) | ||
|
||
it('uses the correct translation for the choose file button', async () => { | ||
const buttonText = await page.$eval(buttonSelector, (el) => | ||
const buttonElementText = await page.$eval( | ||
pseudoButtonSelector, | ||
(el) => el.innerHTML.trim() | ||
) | ||
|
||
const statusElementText = await page.$eval(statusSelector, (el) => | ||
el.innerHTML.trim() | ||
) | ||
|
||
expect(buttonText).toBe('Dewiswch ffeil') | ||
const buttonAriaText = await page.$eval(buttonSelector, (el) => | ||
el.getAttribute('aria-label') | ||
) | ||
|
||
expect(buttonElementText).toBe('Dewiswch ffeil') | ||
expect(statusElementText).toBe("Dim ffeiliau wedi'u dewis") | ||
expect(buttonAriaText).toBe( | ||
"Llwythwch ffeil i fyny, Dewiswch ffeil, Dim ffeiliau wedi'u dewis" | ||
) | ||
}) | ||
|
||
describe('status element', () => { | ||
|
@@ -373,7 +380,7 @@ describe('/components/file-upload', () => { | |
it('uses the correct translation when multiple files are selected', async () => { | ||
const [fileChooser] = await Promise.all([ | ||
page.waitForFileChooser(), | ||
page.click(inputSelector) | ||
page.click(buttonSelector) | ||
]) | ||
await fileChooser.accept(['testfile1.txt', 'testfile2.pdf']) | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@patrickpatrickpatrick One for the PR about the styles, but given we set the
disabled
attribute on the<button>
, we should directly target the<button>
rather than use the state of the<input>
.