Skip to content

Commit

Permalink
Validate min and max length on export annotations filename
Browse files Browse the repository at this point in the history
  • Loading branch information
acelaya committed Aug 22, 2023
1 parent f23ae27 commit fbbfe73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
21 changes: 14 additions & 7 deletions src/sidebar/components/ShareDialog/ExportAnnotations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export type ExportAnnotationsProps = {
toastMessenger: ToastMessengerService;
};

// TODO: Validate user-entered filename
// TODO: does the Input need a label?

/**
Expand All @@ -39,7 +38,9 @@ function ExportAnnotations({
return <LoadingSpinner />;
}

const exportAnnotations = () => {
const exportAnnotations = (e: Event) => {
e.preventDefault();

try {
const filename = `${inputRef.current!.value}.json`;
const exportData = annotationsExporter.buildExportContent(
Expand All @@ -57,21 +58,27 @@ function ExportAnnotations({
};

return (
<>
<form
className="space-y-3"
onSubmit={exportAnnotations}
data-testid="export-form"
>
{exportCount > 0 ? (
<>
<p data-testid="export-count">
<label data-testid="export-count" htmlFor="export-filename">
Export{' '}
<strong>
{exportCount} {pluralize('annotation', exportCount)}
</strong>{' '}
in a file named:
</p>
</label>
<Input
data-testid="export-filename"
id="export-filename"
defaultValue={suggestedFilename({ groupName: group?.name })}
elementRef={inputRef}
required
maxLength={250}
/>
</>
) : (
Expand All @@ -90,12 +97,12 @@ function ExportAnnotations({
data-testid="export-button"
variant="primary"
disabled={!exportCount}
onClick={exportAnnotations}
type="submit"
>
Export
</Button>
</CardActions>
</>
</form>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ describe('ExportAnnotations', () => {
assert.equal(input.prop('defaultValue'), 'suggested-filename');
});

describe('export button clicked', () => {
const clickExportButton = wrapper =>
wrapper.find('button[data-testid="export-button"]').simulate('click');
describe('export form submitted', () => {
const submitExportForm = wrapper =>
wrapper.find('[data-testid="export-form"]').simulate('submit');

it('builds an export file from the non-draft annotations', () => {
const wrapper = createComponent();
Expand All @@ -123,7 +123,7 @@ describe('ExportAnnotations', () => {
];
fakeStore.savedAnnotations.returns(annotationsToExport);

clickExportButton(wrapper);
submitExportForm(wrapper);

assert.calledOnce(fakeAnnotationsExporter.buildExportContent);
assert.calledWith(
Expand All @@ -139,7 +139,7 @@ describe('ExportAnnotations', () => {
wrapper.find('input[data-testid="export-filename"]').getDOMNode().value =
'my-filename';

clickExportButton(wrapper);
submitExportForm(wrapper);

assert.calledOnce(fakeDownloadJSONFile);
assert.calledWith(
Expand All @@ -157,7 +157,7 @@ describe('ExportAnnotations', () => {

const wrapper = createComponent();

clickExportButton(wrapper);
submitExportForm(wrapper);

assert.notCalled(fakeDownloadJSONFile);
assert.calledOnce(fakeAnnotationsExporter.buildExportContent);
Expand Down

0 comments on commit fbbfe73

Please sign in to comment.