-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…-multiline-pasting TAN-3604 Survey option multiline pasting
- Loading branch information
Showing
12 changed files
with
211 additions
and
19 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
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
61 changes: 61 additions & 0 deletions
61
...s/FormBuilder/components/FormBuilderSettings/ConfigSelectWithLocaleSwitcher/utils.test.ts
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,61 @@ | ||
import { allowMultilinePaste, updateFormOnMultlinePaste } from './utils'; | ||
|
||
describe('allowMultilinePaste', () => { | ||
it('should return true if all options from the given index are empty', () => { | ||
const options = [ | ||
{ title_multiloc: { en: 'Text' } }, | ||
{ title_multiloc: { en: '' } }, | ||
{ title_multiloc: { en: undefined } }, | ||
]; | ||
|
||
const result = allowMultilinePaste({ options, index: 1, locale: 'en' }); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if not all options from the given index are empty', () => { | ||
const options = [ | ||
{ title_multiloc: { en: 'Text' } }, | ||
{ title_multiloc: { en: '' } }, | ||
{ title_multiloc: { en: 'Something' } }, | ||
]; | ||
|
||
const result = allowMultilinePaste({ options, index: 1, locale: 'en' }); | ||
expect(result).toBe(false); | ||
}); | ||
}); | ||
|
||
describe('updateFormOnMultlinePaste', () => { | ||
it('correctly sanitizes line with summarization points', () => { | ||
const lines = ['• One', '• Two', '• Three']; | ||
|
||
const options = [{ title_multiloc: { en: '' } }]; | ||
|
||
const update = jest.fn(); | ||
const append = jest.fn(); | ||
|
||
updateFormOnMultlinePaste({ | ||
update, | ||
append, | ||
locale: 'en', | ||
lines, | ||
index: 0, | ||
options, | ||
}); | ||
|
||
expect(update).toHaveBeenCalledTimes(1); | ||
expect(update).toHaveBeenCalledWith(0, { | ||
title_multiloc: { | ||
en: 'One', | ||
}, | ||
temp_id: expect.any(String), | ||
}); | ||
|
||
expect(append).toHaveBeenCalledTimes(2); | ||
expect(append).toHaveBeenCalledWith({ | ||
title_multiloc: { | ||
en: 'Two', | ||
}, | ||
temp_id: expect.any(String), | ||
}); | ||
}); | ||
}); |
80 changes: 80 additions & 0 deletions
80
...onents/FormBuilder/components/FormBuilderSettings/ConfigSelectWithLocaleSwitcher/utils.ts
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,80 @@ | ||
import { SupportedLocale } from 'typings'; | ||
|
||
import { IOptionsType } from 'api/custom_fields/types'; | ||
|
||
import { generateTempId } from 'utils/helperUtils'; | ||
|
||
interface AllowMultilinePasteParams { | ||
options: IOptionsType[]; | ||
index: number; | ||
locale: SupportedLocale; | ||
} | ||
|
||
// Ensure that for the given locale, | ||
// the option at the given index and all | ||
// subsequent indices are empty | ||
export const allowMultilinePaste = ({ | ||
options, | ||
index, | ||
locale, | ||
}: AllowMultilinePasteParams) => { | ||
for (let i = index; i < options.length; i++) { | ||
if (options[i].title_multiloc[locale]) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
interface UpdateFormOnMultilinePasteParams { | ||
update: (index: number, newOption: IOptionsType) => void; | ||
append: (newOption: IOptionsType) => void; | ||
locale: SupportedLocale; | ||
lines: string[]; | ||
index: number; | ||
options: IOptionsType[]; | ||
} | ||
|
||
export const updateFormOnMultlinePaste = ({ | ||
update, | ||
append, | ||
locale, | ||
lines, | ||
index, | ||
options, | ||
}: UpdateFormOnMultilinePasteParams) => { | ||
lines.forEach((line, i) => { | ||
const optionIndex = index + i; | ||
const option = | ||
optionIndex >= options.length ? undefined : options[optionIndex]; | ||
|
||
if (option) { | ||
update(optionIndex, { | ||
...option, | ||
title_multiloc: { | ||
...option.title_multiloc, | ||
[locale]: sanitizeLine(line), | ||
}, | ||
...(!option.id && !option.temp_id ? { temp_id: generateTempId() } : {}), | ||
}); | ||
} else { | ||
append({ | ||
title_multiloc: { | ||
[locale]: sanitizeLine(line), | ||
}, | ||
temp_id: generateTempId(), | ||
}); | ||
} | ||
}); | ||
}; | ||
|
||
const REMOVABLE_PREFIXES = new Set(['•', '-']); | ||
|
||
const sanitizeLine = (line: string) => { | ||
const trimmedLine = line.trim(); | ||
const cleanedLine = REMOVABLE_PREFIXES.has(trimmedLine[0]) | ||
? trimmedLine.slice(1) | ||
: trimmedLine; | ||
return cleanedLine.trim(); | ||
}; |
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
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
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