Skip to content

Commit

Permalink
fix: prevent adding new item blank spaces (#784)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LinaYahya authored Sep 12, 2023
1 parent 0cb2e62 commit 0f02a64
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 7 deletions.
21 changes: 20 additions & 1 deletion cypress/e2e/item/create/createDocument.cy.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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,
);
});
});
23 changes: 18 additions & 5 deletions cypress/e2e/item/create/createFolder.cy.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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,
);
});
});

Expand Down
15 changes: 15 additions & 0 deletions cypress/fixtures/documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: '<h1>Some Title</h1>',
}),
};

export const GRAASP_DOCUMENT_PARENT_FOLDER: Item = {
...DEFAULT_FOLDER_ITEM,
id: 'bdf09f5a-5688-11eb-ae93-0242ac130002',
Expand Down
6 changes: 6 additions & 0 deletions cypress/fixtures/items.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ export const CREATED_ITEM: Partial<FolderItemType> = {
extra: { [ItemType.FOLDER]: { childrenOrder: [] } },
};

export const CREATED_BLANK_NAME_ITEM: Partial<FolderItemType> = {
name: ' ',
type: ItemType.FOLDER,
extra: { [ItemType.FOLDER]: { childrenOrder: [] } },
};

export const EDITED_FIELDS = {
name: 'new name',
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export const isItemValid = (item: Partial<DiscriminatedItem>): boolean => {
return false;
}

const shouldHaveName = Boolean(item.name);
const shouldHaveName = Boolean(item.name?.trim());

// item should have a type
let hasValidTypeProperties =
Expand Down

0 comments on commit 0f02a64

Please sign in to comment.