-
Notifications
You must be signed in to change notification settings - Fork 147
fix(InputFile): inputの挙動に寄せる #5786
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
Closed
Closed
Changes from all commits
Commits
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
132 changes: 132 additions & 0 deletions
132
packages/smarthr-ui/src/components/InputFile/InputFile.test.tsx
This file contains hidden or 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,132 @@ | ||
| import { render, screen } from '@testing-library/react' | ||
| import { userEvent } from 'storybook/test' | ||
|
|
||
| import { IntlProvider } from '../../intl' | ||
| import { FormControl } from '../FormControl' | ||
|
|
||
| import { InputFile } from './InputFile' | ||
|
|
||
| describe('InputFile', () => { | ||
| const file1 = new File(['foo'], 'foo.txt', { type: 'text/plain' }) | ||
| const file2 = new File(['bar'], 'bar.txt', { type: 'text/plain' }) | ||
|
|
||
| it('ファイル選択時にonChangeが発火すること', async () => { | ||
| const onChange = vi.fn() | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" onChange={onChange} /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, file1) | ||
| expect(onChange).toHaveBeenCalledOnce() | ||
| expect(onChange).toHaveBeenCalledWith([file1]) | ||
| }) | ||
|
|
||
| it('multipleでファイル選択時にonChangeが発火すること', async () => { | ||
| const onChange = vi.fn() | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" multiple onChange={onChange} /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, [file1, file2]) | ||
| expect(onChange).toHaveBeenCalledOnce() | ||
| expect(onChange).toHaveBeenCalledWith([file1, file2]) | ||
| }) | ||
|
|
||
| it('multiplyAppendableでファイル選択時にonChangeが発火すること', async () => { | ||
| const onChange = vi.fn() | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" multiplyAppendable onChange={onChange} /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, file1) | ||
| expect(onChange).toHaveBeenCalledOnce() | ||
| expect(onChange).toHaveBeenCalledWith([file1]) | ||
| }) | ||
|
|
||
| it('ファイル選択後、ファイルリストが表示されること', async () => { | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, file1) | ||
| expect(screen.getByText(file1.name)).toBeInTheDocument() | ||
| }) | ||
|
|
||
| it('ファイル選択後、削除するとinputのvalueには存在しないこと', async () => { | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, file1) | ||
| expect(input.files).toHaveLength(1) | ||
| const deleteButton = screen.getByRole('button', { name: '削除' }) | ||
| await userEvent.click(deleteButton) | ||
| expect(input.files).toHaveLength(0) | ||
| }) | ||
|
|
||
| it('multipleで複数ファイル選択後、1つ削除すると削除したもののみinputのvalueには存在しないこと', async () => { | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" multiple /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, [file1, file2]) | ||
| expect(input.files).toHaveLength(2) | ||
| const deleteButton = screen.getByRole('button', { name: '削除' }) | ||
| await userEvent.click(deleteButton) | ||
| expect(input.files).toHaveLength(1) | ||
| }) | ||
|
|
||
| it('ファイル削除後、ファイルリストに存在しないこと', async () => { | ||
| await render( | ||
| <IntlProvider locale="ja"> | ||
| <form> | ||
| <FormControl title="input file"> | ||
| <InputFile name="test" label="input file" /> | ||
| </FormControl> | ||
| </form> | ||
| </IntlProvider>, | ||
| ) | ||
| const input = screen.getByLabelText('input file') | ||
| await userEvent.upload(input, file1) | ||
| expect(screen.getByText(file1.name)).toBeInTheDocument() | ||
| const deleteButton = screen.getByRole('button', { name: '削除' }) | ||
| await userEvent.click(deleteButton) | ||
| expect(screen.queryByText(file1.name)).not.toBeInTheDocument() | ||
| }) | ||
| }) |
This file contains hidden or 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 hidden or 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 +1,104 @@ | ||
| import '@testing-library/jest-dom/vitest' | ||
|
|
||
| // DataTransferのモックを追加 | ||
| global.DataTransfer = class DataTransfer { | ||
| constructor() { | ||
| this._items = [] | ||
| this._files = [] | ||
| this._dropEffect = 'none' | ||
| this._effectAllowed = 'uninitialized' | ||
| this._types = [] | ||
| this._data = new Map() | ||
| } | ||
|
|
||
| get items() { | ||
| return { | ||
| add: (data, type) => { | ||
| this._items.push({ data, type }) | ||
| this._types.push(type) | ||
| if (data instanceof File) { | ||
| this._files.push(data) | ||
| } | ||
| return this._items.length - 1 | ||
| }, | ||
| clear: () => { | ||
| this._items = [] | ||
| this._files = [] | ||
| this._types = [] | ||
| this._data.clear() | ||
| }, | ||
| remove: (index) => { | ||
| if (index >= 0 && index < this._items.length) { | ||
| const item = this._items[index] | ||
| this._items.splice(index, 1) | ||
| this._types.splice(index, 1) | ||
| if (item.data instanceof File) { | ||
| const fileIndex = this._files.indexOf(item.data) | ||
| if (fileIndex > -1) { | ||
| this._files.splice(fileIndex, 1) | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| get length() { | ||
| return this._items.length | ||
| } | ||
| } | ||
| } | ||
|
|
||
| get files() { | ||
| return this._files | ||
| } | ||
|
|
||
| get types() { | ||
| return [...this._types] | ||
| } | ||
|
|
||
| get dropEffect() { | ||
| return this._dropEffect | ||
| } | ||
|
|
||
| set dropEffect(value) { | ||
| if (['none', 'copy', 'link', 'move'].includes(value)) { | ||
| this._dropEffect = value | ||
| } | ||
| } | ||
|
|
||
| get effectAllowed() { | ||
| return this._effectAllowed | ||
| } | ||
|
|
||
| set effectAllowed(value) { | ||
| if (['none', 'copy', 'copyLink', 'copyMove', 'link', 'linkMove', 'move', 'all', 'uninitialized'].includes(value)) { | ||
| this._effectAllowed = value | ||
| } | ||
| } | ||
|
|
||
| clearData(format) { | ||
| if (format) { | ||
| this._data.delete(format) | ||
| const index = this._types.indexOf(format) | ||
| if (index > -1) { | ||
| this._types.splice(index, 1) | ||
| } | ||
| } else { | ||
| this._data.clear() | ||
| this._types.length = 0 | ||
| } | ||
| } | ||
|
|
||
| getData(format) { | ||
| return this._data.get(format) || '' | ||
| } | ||
|
|
||
| setData(format, data) { | ||
| this._data.set(format, data) | ||
| if (!this._types.includes(format)) { | ||
| this._types.push(format) | ||
| } | ||
| } | ||
|
|
||
| setDragImage(element, x, y) { | ||
| // モックでは何もしない | ||
| } | ||
| } |
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.
お試ししていた中で見つけたので共有です!
複数ファイル選択した状態で、1つ削除したときに、valueを空にすると残りのfilesも認識されなくなるみたいです😭
(filesには入ってるけど、取得されなくなる)
valueに値は入れられないし、悩ましいですね・・。
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.
試しに
valueを空にした後、updateFiles(newFiles)を呼び出したらうまく行ったのでご参考まで・・!https://github.com/kufu/smarthr-ui/pull/5805/files#diff-ce35f53c675c0805c9951c077709a72ba2fc7123a8fba5e614744a695a6fd4ceR208