-
Notifications
You must be signed in to change notification settings - Fork 180
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): refine file upload component
adds focus state, file name truncation, test, and story for FileUpload component closes PLAT-292
- Loading branch information
1 parent
a88f03b
commit a2c6fa4
Showing
4 changed files
with
87 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react' | ||
import testFile from './__tests__/test-file.png' | ||
import { FileUpload } from '.' | ||
|
||
import type { StoryFn, Meta } from '@storybook/react' | ||
|
||
const file = new File([testFile], 'a-file-to-test.png') | ||
const handleClick = (): void => console.log('clicked the file') | ||
|
||
export default { | ||
title: 'App/Molecules/FileUpload', | ||
component: FileUpload, | ||
} as Meta | ||
|
||
const FileUploadTemplate: StoryFn< | ||
React.ComponentProps<typeof FileUpload> | ||
> = args => <FileUpload {...args} /> | ||
|
||
export const FileUploadComponent = FileUploadTemplate.bind({}) | ||
FileUploadComponent.args = { | ||
file, | ||
fileError: null, | ||
handleClick, | ||
} | ||
|
||
export const FileUploadError = FileUploadTemplate.bind({}) | ||
FileUploadError.args = { | ||
file, | ||
fileError: 'a terrible file', | ||
handleClick, | ||
} |
42 changes: 42 additions & 0 deletions
42
app/src/molecules/FileUpload/__tests__/FileUpload.test.tsx
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,42 @@ | ||
import * as React from 'react' | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { screen } from '@testing-library/react' | ||
|
||
import { renderWithProviders } from '../../../__testing-utils__' | ||
import { i18n } from '../../../i18n' | ||
import { FileUpload } from '..' | ||
import testFile from './test-file.png' | ||
|
||
const render = (props: React.ComponentProps<typeof FileUpload>) => { | ||
return renderWithProviders(<FileUpload {...props} />, { | ||
i18nInstance: i18n, | ||
})[0] | ||
} | ||
|
||
const handleClick = vi.fn() | ||
|
||
describe('FileUpload', () => { | ||
let props: React.ComponentProps<typeof FileUpload> | ||
|
||
beforeEach(() => { | ||
const file = new File([testFile], 'a-file-to-test.png') | ||
|
||
props = { | ||
file, | ||
fileError: null, | ||
handleClick, | ||
} | ||
}) | ||
it('renders file upload', () => { | ||
render(props) | ||
screen.getByText('a-file-to-test.png') | ||
const removeFile = screen.getByLabelText('remove_file') | ||
removeFile.click() | ||
expect(handleClick).toBeCalled() | ||
}) | ||
|
||
it('renders file upload error', () => { | ||
render({ ...props, fileError: 'oops this is a bad file' }) | ||
screen.getByText('oops this is a bad file') | ||
}) | ||
}) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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