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
feat(upload): Add upload API #279
Merged
kentcdodds
merged 5 commits into
testing-library:master
from
vadimshvetsov:add-file-api
May 15, 2020
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
083d954
feat(upload): Add upload API
vadimshvetsov b2b7341
Remove unnecessary clear feature
vadimshvetsov dbc5675
Remove useless blur test
vadimshvetsov d3c5e06
Remove unnecessary array destruction
vadimshvetsov d3600f1
Add changeInit option
vadimshvetsov File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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 |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import React from "react"; | ||
import { cleanup, render, fireEvent } from "@testing-library/react"; | ||
import "@testing-library/jest-dom/extend-expect"; | ||
import userEvent from "../../src"; | ||
|
||
afterEach(cleanup); | ||
|
||
describe("userEvent.upload", () => { | ||
it("should fire the correct events for input", () => { | ||
const file = new File(["hello"], "hello.png", { type: "image/png" }); | ||
const events = []; | ||
const eventsHandler = jest.fn((evt) => events.push(evt.type)); | ||
const eventHandlers = { | ||
onMouseOver: eventsHandler, | ||
onMouseMove: eventsHandler, | ||
onMouseDown: eventsHandler, | ||
onFocus: eventsHandler, | ||
onMouseUp: eventsHandler, | ||
onClick: eventsHandler, | ||
}; | ||
|
||
const { getByTestId } = render( | ||
<input type="file" data-testid="element" {...eventHandlers} /> | ||
); | ||
|
||
userEvent.upload(getByTestId("element"), file); | ||
|
||
expect(events).toEqual([ | ||
"mouseover", | ||
"mousemove", | ||
"mousedown", | ||
"focus", | ||
"mouseup", | ||
"click", | ||
]); | ||
}); | ||
|
||
it("should fire the correct events with label", () => { | ||
const file = new File(["hello"], "hello.png", { type: "image/png" }); | ||
|
||
const inputEvents = []; | ||
const labelEvents = []; | ||
const eventsHandler = (events) => jest.fn((evt) => events.push(evt.type)); | ||
|
||
const getEventHandlers = (events) => ({ | ||
onMouseOver: eventsHandler(events), | ||
onMouseMove: eventsHandler(events), | ||
onMouseDown: eventsHandler(events), | ||
onFocus: eventsHandler(events), | ||
onMouseUp: eventsHandler(events), | ||
onClick: eventsHandler(events), | ||
}); | ||
|
||
const { getByTestId } = render( | ||
<> | ||
<label | ||
htmlFor="element" | ||
data-testid="label" | ||
{...getEventHandlers(labelEvents)} | ||
> | ||
Element | ||
</label> | ||
<input type="file" id="element" {...getEventHandlers(inputEvents)} /> | ||
</> | ||
); | ||
|
||
userEvent.upload(getByTestId("label"), file); | ||
|
||
expect(inputEvents).toEqual(["focus", "click"]); | ||
expect(labelEvents).toEqual([ | ||
"mouseover", | ||
"mousemove", | ||
"mousedown", | ||
"mouseup", | ||
"click", | ||
]); | ||
}); | ||
|
||
it("should upload the file", () => { | ||
const file = new File(["hello"], "hello.png", { type: "image/png" }); | ||
const { getByTestId } = render(<input type="file" data-testid="element" />); | ||
const input = getByTestId("element"); | ||
|
||
userEvent.upload(input, file); | ||
|
||
expect(input.files[0]).toStrictEqual(file); | ||
expect(input.files.item(0)).toStrictEqual(file); | ||
expect(input.files).toHaveLength(1); | ||
|
||
fireEvent.change(input, { | ||
target: { files: { item: () => {}, length: 0 } }, | ||
}); | ||
|
||
expect(input.files[0]).toBeUndefined(); | ||
expect(input.files.item[0]).toBeUndefined(); | ||
expect(input.files).toHaveLength(0); | ||
}); | ||
|
||
it("should upload multiple files", () => { | ||
const files = [ | ||
new File(["hello"], "hello.png", { type: "image/png" }), | ||
new File(["there"], "there.png", { type: "image/png" }), | ||
]; | ||
const { getByTestId } = render( | ||
<input type="file" multiple data-testid="element" /> | ||
); | ||
const input = getByTestId("element"); | ||
|
||
userEvent.upload(input, files); | ||
|
||
expect(input.files[0]).toStrictEqual(files[0]); | ||
expect(input.files.item(0)).toStrictEqual(files[0]); | ||
expect(input.files[1]).toStrictEqual(files[1]); | ||
expect(input.files.item(1)).toStrictEqual(files[1]); | ||
expect(input.files).toHaveLength(2); | ||
|
||
fireEvent.change(input, { | ||
target: { files: { item: () => {}, length: 0 } }, | ||
}); | ||
|
||
expect(input.files[0]).toBeUndefined(); | ||
expect(input.files.item[0]).toBeUndefined(); | ||
expect(input.files).toHaveLength(0); | ||
}); | ||
|
||
it("should not upload when is disabled", () => { | ||
const file = new File(["hello"], "hello.png", { type: "image/png" }); | ||
const { getByTestId } = render( | ||
<input type="file" data-testid="element" disabled /> | ||
); | ||
|
||
const input = getByTestId("element"); | ||
|
||
userEvent.upload(input, file); | ||
|
||
expect(input.files[0]).toBeUndefined(); | ||
expect(input.files.item[0]).toBeUndefined(); | ||
expect(input.files).toHaveLength(0); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1,27 +1,39 @@ | ||
// Definitions by: Wu Haotian <https://github.com/whtsky> | ||
export interface IUserOptions { | ||
allAtOnce?: boolean; | ||
delay?: number; | ||
allAtOnce?: boolean; | ||
delay?: number; | ||
} | ||
|
||
export interface ITabUserOptions { | ||
shift?: boolean; | ||
focusTrap?: Document | Element; | ||
shift?: boolean; | ||
focusTrap?: Document | Element; | ||
} | ||
|
||
export type TargetElement = Element | Window; | ||
|
||
export type FilesArgument = File | File[]; | ||
|
||
export type UploadInitArgument = { | ||
clickInit?: MouseEventInit; | ||
changeInit?: Event; | ||
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. I'm not sure in this type, took it from there |
||
}; | ||
|
||
declare const userEvent: { | ||
clear: (element: TargetElement) => void; | ||
click: (element: TargetElement, init?: MouseEventInit) => void; | ||
dblClick: (element: TargetElement) => void; | ||
selectOptions: (element: TargetElement, values: string | string[]) => void; | ||
type: ( | ||
element: TargetElement, | ||
text: string, | ||
userOpts?: IUserOptions | ||
) => Promise<void>; | ||
tab: (userOpts?: ITabUserOptions) => void; | ||
clear: (element: TargetElement) => void; | ||
click: (element: TargetElement, init?: MouseEventInit) => void; | ||
dblClick: (element: TargetElement) => void; | ||
selectOptions: (element: TargetElement, values: string | string[]) => void; | ||
upload: ( | ||
element: TargetElement, | ||
files: FilesArgument, | ||
init?: UploadInitArgument | ||
) => void; | ||
type: ( | ||
element: TargetElement, | ||
text: string, | ||
userOpts?: IUserOptions | ||
) => Promise<void>; | ||
tab: (userOpts?: ITabUserOptions) => void; | ||
}; | ||
|
||
export default userEvent; |
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.
This
files
variable depends on inputmultiple
attribute. If it's truefileOrFiles
arg assuming as array otherwise as singlefile. At first it relied onArray.isArray
check but then I thought about passing test cases with multiple files and input without attributemultiple
.