From 0f02a64f897f5fb8b48f0466c645e3232c29cd9c Mon Sep 17 00:00:00 2001 From: Lina Ebeid Date: Tue, 12 Sep 2023 10:04:56 +0200 Subject: [PATCH] fix: prevent adding new item blank spaces (#784) * fix: validate white spaces for creating new item * test: create a test for whitespaces new item name * test: creating document with blank name led to have inactive btn --- cypress/e2e/item/create/createDocument.cy.ts | 21 +++++++++++++++++- cypress/e2e/item/create/createFolder.cy.ts | 23 +++++++++++++++----- cypress/fixtures/documents.ts | 15 +++++++++++++ cypress/fixtures/items.ts | 6 +++++ src/utils/item.ts | 2 +- 5 files changed, 60 insertions(+), 7 deletions(-) diff --git a/cypress/e2e/item/create/createDocument.cy.ts b/cypress/e2e/item/create/createDocument.cy.ts index 9f1ce9e4d..f835db31f 100644 --- a/cypress/e2e/item/create/createDocument.cy.ts +++ b/cypress/e2e/item/create/createDocument.cy.ts @@ -1,6 +1,11 @@ +import { ITEM_FORM_CONFIRM_BUTTON_ID } from '@/config/selectors'; + import { HOME_PATH, buildItemPath } from '../../../../src/config/paths'; import ITEM_LAYOUT_MODES from '../../../../src/enums/itemLayoutModes'; -import { GRAASP_DOCUMENT_ITEM } from '../../../fixtures/documents'; +import { + GRAASP_DOCUMENT_BLANK_NAME_ITEM, + GRAASP_DOCUMENT_ITEM, +} from '../../../fixtures/documents'; import { SAMPLE_ITEMS } from '../../../fixtures/items'; import { createDocument } from '../../../support/createUtils'; @@ -37,4 +42,18 @@ describe('Create Document', () => { cy.wait('@getItem').its('response.url').should('contain', id); }); }); + + it('cannot create Document with blank name', () => { + cy.setUpApi(); + cy.visit(HOME_PATH); + + cy.switchMode(ITEM_LAYOUT_MODES.LIST); + createDocument(GRAASP_DOCUMENT_BLANK_NAME_ITEM, { confirm: false }); + + cy.get(`#${ITEM_FORM_CONFIRM_BUTTON_ID}`).should( + 'have.prop', + 'disabled', + true, + ); + }); }); diff --git a/cypress/e2e/item/create/createFolder.cy.ts b/cypress/e2e/item/create/createFolder.cy.ts index bf02df2f0..54a6b555e 100644 --- a/cypress/e2e/item/create/createFolder.cy.ts +++ b/cypress/e2e/item/create/createFolder.cy.ts @@ -1,11 +1,16 @@ import { HOME_PATH, buildItemPath } from '../../../../src/config/paths'; import { CREATE_ITEM_BUTTON_ID, + ITEM_FORM_CONFIRM_BUTTON_ID, ITEM_FORM_NAME_INPUT_ID, buildItemsTableRowIdAttribute, } from '../../../../src/config/selectors'; import ITEM_LAYOUT_MODES from '../../../../src/enums/itemLayoutModes'; -import { CREATED_ITEM, SAMPLE_ITEMS } from '../../../fixtures/items'; +import { + CREATED_BLANK_NAME_ITEM, + CREATED_ITEM, + SAMPLE_ITEMS, +} from '../../../fixtures/items'; import { createFolder } from '../../../support/createUtils'; describe('Create Folder', () => { @@ -33,11 +38,19 @@ describe('Create Folder', () => { // create createFolder(CREATED_ITEM); + }); - cy.wait('@postItem').then(() => { - // expect update - cy.wait('@getItem').its('response.url').should('contain', id); - }); + it('cannot create folder with blank name in item', () => { + // create + cy.setUpApi(); + cy.visit(HOME_PATH); + createFolder(CREATED_BLANK_NAME_ITEM, { confirm: false }); + + cy.get(`#${ITEM_FORM_CONFIRM_BUTTON_ID}`).should( + 'have.prop', + 'disabled', + true, + ); }); }); diff --git a/cypress/fixtures/documents.ts b/cypress/fixtures/documents.ts index c41760950..aba89811c 100644 --- a/cypress/fixtures/documents.ts +++ b/cypress/fixtures/documents.ts @@ -19,6 +19,21 @@ export const GRAASP_DOCUMENT_ITEM: DocumentItemType = { }), }; +export const GRAASP_DOCUMENT_BLANK_NAME_ITEM: DocumentItemType = { + id: 'ecafbd2a-5688-12eb-ae91-0242ac130004', + type: ItemType.DOCUMENT, + name: ' ', + description: 'a description for graasp text', + path: 'ecafbd2a_5688_12eb_ae93_0242ac130002', + settings: {}, + createdAt: new Date(), + updatedAt: new Date(), + creator: CURRENT_USER, + extra: buildDocumentExtra({ + content: '

Some Title

', + }), +}; + export const GRAASP_DOCUMENT_PARENT_FOLDER: Item = { ...DEFAULT_FOLDER_ITEM, id: 'bdf09f5a-5688-11eb-ae93-0242ac130002', diff --git a/cypress/fixtures/items.ts b/cypress/fixtures/items.ts index c9ea49a42..82da12d75 100644 --- a/cypress/fixtures/items.ts +++ b/cypress/fixtures/items.ts @@ -43,6 +43,12 @@ export const CREATED_ITEM: Partial = { extra: { [ItemType.FOLDER]: { childrenOrder: [] } }, }; +export const CREATED_BLANK_NAME_ITEM: Partial = { + name: ' ', + type: ItemType.FOLDER, + extra: { [ItemType.FOLDER]: { childrenOrder: [] } }, +}; + export const EDITED_FIELDS = { name: 'new name', }; diff --git a/src/utils/item.ts b/src/utils/item.ts index 492f6c1b0..f56974de9 100644 --- a/src/utils/item.ts +++ b/src/utils/item.ts @@ -103,7 +103,7 @@ export const isItemValid = (item: Partial): boolean => { return false; } - const shouldHaveName = Boolean(item.name); + const shouldHaveName = Boolean(item.name?.trim()); // item should have a type let hasValidTypeProperties =