Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent adding new item blank spaces #784

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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