Skip to content

Commit

Permalink
test: increase test coverage - refs #254313
Browse files Browse the repository at this point in the history
  • Loading branch information
ana-oprea committed Aug 14, 2023
1 parent 94b58f7 commit 5facade
Show file tree
Hide file tree
Showing 4 changed files with 526 additions and 0 deletions.
84 changes: 84 additions & 0 deletions cypress/e2e/01-block-group.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ describe('Blocks Tests', () => {
.contains('Section (Group)')
.click({ force: true });

cy.contains('Block').click();

cy.get('.block-editor-group [contenteditable=true]')
.focus()
.click()
Expand Down Expand Up @@ -49,4 +51,86 @@ describe('Blocks Tests', () => {
cy.contains('My Add-on Page');
cy.contains('test2');
});

it('Add Block: Make content type and add group block to layout', () => {
cy.clearSlateTitle();
cy.getSlateTitle().type('My Add-on Page');

cy.get('.documentFirstHeading').contains('My Add-on Page');

cy.get('#toolbar-save').click();
cy.get('.user').click();
cy.get('a[href="/controlpanel"]').click();
cy.get('a[href="/controlpanel/dexterity-types"]').click();

// add the content type
cy.get('#toolbar-add').click();
cy.get('#field-title').click().type('Test Content Type');
cy.get('.actions button[aria-label="Save"]').click();

// change the layout
cy.get('.ui.dropdown.actions-test_content_type').click();
cy.get('.item.layout-test_content_type').click();
cy.contains('Enable editable Blocks').click();
cy.getSlate().click();

// Add block
cy.get('.ui.basic.icon.button.block-add-button').first().click();
cy.get('.blocks-chooser .title').contains('Common').click();
cy.get('.content.active.common .button.group')
.contains('Section (Group)')
.click({ force: true });
cy.contains('Section').click();

cy.get('.sidebar-container #field-placeholder')
.click()
.type('Test Helper Text');
cy.get(
'.sidebar-container .field-wrapper-instructions div[role="textbox"] p',
)
.click()
.type('Description Blocks');
cy.get(
'.sidebar-container .field-wrapper-allowedBlocks .react-select__value-container',
).click();
cy.get('.react-select__option')
.contains('Description')
.click({ force: true });
cy.get('.field-wrapper-ignoreSpaces input').click({ force: true });
cy.get('.field-wrapper-required input').click({ force: true });

cy.get('#toolbar-save').click();
cy.get('.ui.button.cancel').click();
cy.get('a[href="/controlpanel').click();
cy.contains('Home').click();
cy.get('#toolbar-add').click();
cy.get('#toolbar-add-test_content_type').click();
cy.get('#field-title').click().type('Test Content Type');
cy.get('.block-editor-group div[role="textbox"]')
.click()
.type('/description{enter}');
cy.get('.block-editor-group .block-editor-slate').click();
cy.get(
'.block-editor-slate .block-toolbar button[title="Add block"]',
).click();
cy.get('.blocks-chooser .field.searchbox div.ui.transparent.input input')
.click()
.focus()
.type('Description{enter}');
cy.get(
'.blocks-chooser .accordion div[aria-label="Unfold Text blocks"]',
).click();
cy.get('.ui.basic.icon.button.description').click();
cy.get('button[title="Remove block"]').click();

// delete the content type
cy.get('#toolbar-save').click();
cy.get('.user').click();
cy.get('a[href="/controlpanel"]').click();
cy.get('a[href="/controlpanel/dexterity-types"]').click();

cy.get('.ui.dropdown.actions-test_content_type').click();
cy.get('.item.delete-test_content_type').click();
cy.get('button.ui.primary.button').should('contain', 'Yes').click();
});
});
234 changes: 234 additions & 0 deletions src/components/manage/Blocks/Group/CounterComponent.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import React from 'react';
import { render } from '@testing-library/react';
import CounterComponent from './CounterComponent';
import '@testing-library/jest-dom/extend-expect';

jest.mock('@plone/volto/registry', () => ({
blocks: {
blocksConfig: {
group: {
countTextIn: ['text'],
},
},
},
}));

jest.mock('@plone/volto-slate/editor/render', () => ({
serializeNodesToText: jest.fn((nodes) =>
nodes.map((node) => node.text).join(' '),
),
}));

describe('CounterComponent', () => {
const setSidebarTab = jest.fn();
const setSelectedBlock = jest.fn();

it('should render info class when character count is less than 95% of maxChars', () => {
const { container, getByText } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: { '@type': 'text', plaintext: 'test' },
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
expect(getByText('96 characters remaining out of 100')).toBeInTheDocument();
expect(container.querySelector('.counter.info')).toBeInTheDocument();
});

it('should render warning class when character count is between 95% and 100% of maxChars', () => {
const { container, getByText } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: { '@type': 'text', plaintext: 'test'.repeat(24) },
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
expect(getByText('4 characters remaining out of 100')).toBeInTheDocument();
expect(container.querySelector('.counter.warning')).toBeInTheDocument();
});

it('should render warning class when character count is over the maxChars', () => {
const { container, getByText } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: { '@type': 'text', plaintext: 'test'.repeat(26) },
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
expect(getByText('4 characters over the limit')).toBeInTheDocument();
expect(container.querySelector('.counter.danger')).toBeInTheDocument();
});

it('should handle click event', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: {
'@type': 'text',
plaintext: 'test'.repeat(24),
ignoreSpaces: true,
},
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});

it('should handle click event with maxChar undefined', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: undefined,
data: undefined,
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});

it('should handle click event with data undefined', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: 100,
data: undefined,
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});

it('should handle click event with plaintext undefined, but values present', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: {
'@type': 'text',
plaintext: undefined,
value: [
{ text: 'test' },
{ children: [{ text: 'more text' }] },
],
},
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});

it('should handle click event with plaintext undefined and values is not an array and the type is in countTextIn', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: {
'@type': 'text',
plaintext: undefined,
value: {},
},
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});

it('should handle click event with plaintext undefined and values is not an array and the type is not in countTextIn', () => {
const { container } = render(
<CounterComponent
data={{
maxChars: 100,
data: {
blocks: {
block1: {
'@type': 'test',
plaintext: undefined,
value: {},
},
},
blocks_layout: {
items: ['block1'],
},
},
}}
setSidebarTab={setSidebarTab}
setSelectedBlock={setSelectedBlock}
/>,
);
container.querySelector('.counter').click();
expect(setSidebarTab).toHaveBeenCalledWith(1);
expect(setSelectedBlock).toHaveBeenCalled();
});
});

0 comments on commit 5facade

Please sign in to comment.