Skip to content

Commit

Permalink
[Fix] Prevent empty name category from being added (#3761)
Browse files Browse the repository at this point in the history
* Fix: Prevent empty name category from being added

* Refactor: Trim input to prevent category name with blank spaces only

* Refactor: Remove unnecessary optional chaining operator

* Test: Add E2E scenario for disable "Add" category button when empty name is presented

* Refactor: Change test scenario comment
  • Loading branch information
DenysMb authored May 25, 2024
1 parent 1429f64 commit 8c77906
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion e2e/categories.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ electronTest('categories', async (app) => {

await expect(dialog.getByText('No categories yet.')).toBeInViewport()

// add button should be disabled without a name
await expect(dialog.getByTitle('Add', { exact: true })).toBeDisabled()

// add new category
await dialog.getByPlaceholder('Add new category').fill('Great games')
await dialog.getByTitle('Add', { exact: true }).click()
Expand All @@ -23,8 +26,20 @@ electronTest('categories', async (app) => {
dialog.locator('span', { hasText: 'Great games' })
).toBeInViewport()

// rename category
// add button should be disable if trying to rename category to same name
await dialog.getByTitle('Rename "Great games"').click()
await dialog.getByLabel('Rename "Great games"').fill('Great games')
await expect(dialog.getByTitle('Add', { exact: true })).toBeDisabled()

// add button should be disabled if trying to rename category to empty string
await dialog.getByLabel('Rename "Great games"').fill('')
await expect(dialog.getByTitle('Add', { exact: true })).toBeDisabled()

// add button should be disabled if trying to rename category to empty spaces
await dialog.getByLabel('Rename "Great games"').fill(' ')
await expect(dialog.getByTitle('Add', { exact: true })).toBeDisabled()

// rename category
await dialog.getByLabel('Rename "Great games"').fill('Amazing games')
await dialog
.getByTitle('Confirm rename of "Great games" as "Amazing games"')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ function CategoryItem({
const [renameMode, setRenameMode] = useState(false)
const [newName, setNewName] = useState(name)
const [removeMode, setRemoveMode] = useState(false)
const isNewNameEmptyOrEqualsOldName =
newName.trim() === '' || newName === name

const rename = () => {
renameFunction(name, newName)
Expand Down Expand Up @@ -57,6 +59,7 @@ function CategoryItem({
'Confirm rename of "{{oldName}}" as "{{newName}}"',
{ oldName: name, newName }
)}
disabled={isNewNameEmptyOrEqualsOldName}
>
<FontAwesomeIcon icon={faCheck} />
</button>
Expand Down Expand Up @@ -157,6 +160,8 @@ function CategoriesManager() {

const [newCategoryName, setNewCategoryName] = useState('')

const isCategoryNameEmpty = newCategoryName.trim() === ''

const removeCategory = (cat: string) => {
customCategories.removeCategory(cat)
}
Expand Down Expand Up @@ -206,6 +211,7 @@ function CategoriesManager() {
className="button"
onClick={() => addCategory()}
title={t('categories-manager.add', 'Add')}
disabled={isCategoryNameEmpty}
>
<FontAwesomeIcon icon={faAdd} />
</button>
Expand Down

0 comments on commit 8c77906

Please sign in to comment.