From f2620ef231cd4ba38cd99d47f582f92665430437 Mon Sep 17 00:00:00 2001 From: ana-oprea Date: Thu, 15 Jun 2023 14:45:32 +0300 Subject: [PATCH 1/4] test: add unit tests for helpers and View - refs #253277 --- src/StatisticBlock/View.test.jsx | 75 ++++++++++++++ src/helpers.test.js | 161 +++++++++++++++++++++++++++++++ 2 files changed, 236 insertions(+) create mode 100644 src/StatisticBlock/View.test.jsx create mode 100644 src/helpers.test.js diff --git a/src/StatisticBlock/View.test.jsx b/src/StatisticBlock/View.test.jsx new file mode 100644 index 0000000..c7bbc51 --- /dev/null +++ b/src/StatisticBlock/View.test.jsx @@ -0,0 +1,75 @@ +import React from 'react'; +import { render } from '@testing-library/react'; +import View from './View'; +import isNumber from 'lodash/isNumber'; +import '@testing-library/jest-dom/extend-expect'; + +jest.mock('lodash/isNumber', () => jest.fn(() => true)); +jest.mock('lodash/isNaN', () => jest.fn(() => false)); + +jest.mock('react-countup', () => () =>
Mocked CountUp
); + +jest.mock('@plone/volto/components', () => ({ + UniversalLink: ({ children }) =>
{children}
, +})); + +jest.mock('@plone/volto-slate/editor/render', () => ({ + serializeNodesToText: jest.fn((nodes) => nodes[0]?.text || ''), + serializeNodes: jest.fn((nodes) => nodes[0]?.text || ''), +})); + +jest.mock('@eeacms/volto-statistic-block/helpers', () => ({ + getFieldURL: jest.fn((href) => href), + serializeToNodes: jest.fn((text) => [{ text }]), + serializeNodes: jest.fn((text) => text || ''), +})); + +describe('Statistic View Component', () => { + it('renders component with items and CountUp', () => { + const data = { + items: [ + { + label: 'Test label', + value: 'Test value', + info: 'Test info', + }, + ], + animation: { + enabled: true, + duration: 3, + decimals: 0, + }, + }; + const { getByText } = render(); + expect(getByText('Test label')).toBeInTheDocument(); + expect(getByText('Test info')).toBeInTheDocument(); + expect(getByText('Mocked CountUp')).toBeInTheDocument(); + }); + + it('renders component with items and no CountUp', () => { + const data = { + items: [ + { + label: 'Test label', + value: 'Test value', + info: 'Test info', + }, + ], + animation: { + enabled: true, + duration: 3, + decimals: 0, + }, + }; + isNumber.mockReturnValueOnce(false); + const { getByText } = render(); + expect(getByText('Test label')).toBeInTheDocument(); + expect(getByText('Test value')).toBeInTheDocument(); + expect(getByText('Test info')).toBeInTheDocument(); + }); + + it('renders component in edit mode with no items', () => { + const { getByText } = render(); + expect(getByText('Add statistic items')).toBeInTheDocument(); + }); +}); diff --git a/src/helpers.test.js b/src/helpers.test.js new file mode 100644 index 0000000..df600e4 --- /dev/null +++ b/src/helpers.test.js @@ -0,0 +1,161 @@ +import { + createParagraph, + serializeToNodes, + serializeNodes, + getFieldURL, +} from './helpers'; +import config from '@plone/volto/registry'; + +jest.mock('@plone/volto/registry', () => ({ + settings: { + slate: { + defaultBlockType: 'test_type', + }, + }, +})); + +jest.mock('@plone/volto-slate/editor/render', () => ({ + serializeNodes: jest.fn(() => 'serialized'), +})); + +describe('serializeToNodes', () => { + it('returns valid nodes array', () => { + const nodes = [ + { children: [{ text: 'test1' }] }, + { children: [{ text: 'test2' }] }, + ]; + expect(serializeToNodes(nodes)).toEqual(nodes); + }); + + it('returns single paragraph for string input', () => { + const string = 'Test string'; + const expected = [ + { + type: 'test_type', + children: [{ text: string }], + }, + ]; + expect(serializeToNodes(string)).toEqual(expected); + }); + + it('returns paragraph with empty string for non-string, non-valid nodes array input', () => { + const expected = [ + { + type: 'test_type', + children: [{ text: '' }], + }, + ]; + expect(serializeToNodes(42)).toEqual(expected); + expect(serializeToNodes(null)).toEqual(expected); + expect(serializeToNodes({})).toEqual(expected); + }); +}); + +describe('createParagraph', () => { + it('returns a paragraph with the provided text', () => { + const text = 'Hello, World!'; + const expected = { + type: config.settings.slate.defaultBlockType, + children: [{ text: text }], + }; + expect(createParagraph(text)).toEqual(expected); + }); +}); + +describe('serializeNodes', () => { + it('serializes nodes', () => { + const nodes = [ + { children: [{ text: 'foo' }] }, + { children: [{ text: 'bar' }] }, + ]; + expect(serializeNodes(nodes)).toEqual('serialized'); + }); +}); + +describe('getFieldURL', () => { + it('handles a URL type object with type and value', () => { + const data = { + '@type': 'URL', + value: 'value_url', + url: 'url_url', + href: 'href_url', + }; + expect(getFieldURL(data)).toEqual('value_url'); + }); + + it('handles an object with type and url', () => { + const data = { + '@type': 'URL', + url: 'url_url', + href: 'href_url', + }; + expect(getFieldURL(data)).toEqual('url_url'); + }); + + it('handles an object with type and href', () => { + const data = { + '@type': 'URL', + href: 'href_url', + }; + expect(getFieldURL(data)).toEqual('href_url'); + }); + + it('handles an object with type and no value, url and href', () => { + const data = { + '@type': 'URL', + }; + expect(getFieldURL(data)).toEqual({ '@type': 'URL' }); + }); + + it('handles an object without a specific type and url', () => { + const data = { + url: 'url_url', + href: 'href_url', + }; + expect(getFieldURL(data)).toEqual('url_url'); + }); + + it('handles an object without a specific type and href', () => { + const data = { + href: 'href_url', + }; + expect(getFieldURL(data)).toEqual('href_url'); + }); + + it('handles an object without a specific type and no id, url, href', () => { + const data = { + test: 'test_url', + }; + expect(getFieldURL(data)).toEqual({ + test: 'test_url', + }); + }); + + it('handles an array', () => { + const data = [ + { + '@type': 'URL', + value: 'value_url', + url: 'url_url', + href: 'href_url', + }, + { + '@id': 'id_url', + url: 'url_url', + href: 'href_url', + }, + ]; + expect(getFieldURL(data)).toEqual(['value_url', 'id_url']); + }); + + it('handles a string', () => { + const data = '/some/url'; + expect(getFieldURL(data)).toEqual('/some/url'); + }); + + it('returns the data unchanged for non-object/non-array/non-string inputs', () => { + expect(getFieldURL(42)).toEqual(42); + expect(getFieldURL(undefined)).toEqual(undefined); + expect(getFieldURL(null)).toEqual(null); + }); +}); From 1f54dc1c1708d46d69567ae66f5f249b95d5d6c8 Mon Sep 17 00:00:00 2001 From: dobri1408 <50819975+dobri1408@users.noreply.github.com> Date: Tue, 20 Jun 2023 15:56:11 +0300 Subject: [PATCH 2/4] fix: Prevent links from leaving page in edit - refs #254291 --- src/StatisticBlock/View.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StatisticBlock/View.jsx b/src/StatisticBlock/View.jsx index e3c8d66..dbc966e 100644 --- a/src/StatisticBlock/View.jsx +++ b/src/StatisticBlock/View.jsx @@ -74,7 +74,7 @@ const View = ({ data, mode }) => { className={cx(textAlign, { 'ui container': styles.align === 'full' })} > {items.map((item, index) => { - const href = getFieldURL(item.href); + const href = mode !== 'edit' ? getFieldURL(item.href) : '#'; const StatisticWrapper = href ? UniversalLink : Statistic; const valueNodes = serializeToNodes(item.value); const valueNo = Number(serializeNodesToText(valueNodes)); From 04fd659d4110614950306d3b1904962189c05831 Mon Sep 17 00:00:00 2001 From: dobri1408 <50819975+dobri1408@users.noreply.github.com> Date: Thu, 20 Jul 2023 20:14:06 +0300 Subject: [PATCH 3/4] fix: link in edit moves page - refs #254291 --- src/StatisticBlock/View.jsx | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/StatisticBlock/View.jsx b/src/StatisticBlock/View.jsx index dbc966e..4ea39c9 100644 --- a/src/StatisticBlock/View.jsx +++ b/src/StatisticBlock/View.jsx @@ -75,14 +75,17 @@ const View = ({ data, mode }) => { > {items.map((item, index) => { const href = mode !== 'edit' ? getFieldURL(item.href) : '#'; - const StatisticWrapper = href ? UniversalLink : Statistic; + const StatisticWrapper = + href && href !== '#' ? UniversalLink : Statistic; const valueNodes = serializeToNodes(item.value); const valueNo = Number(serializeNodesToText(valueNodes)); return ( Date: Mon, 24 Jul 2023 10:00:36 +0000 Subject: [PATCH 4/4] Automated release 1.3.1 --- CHANGELOG.md | 27 ++++++++++++++++++++++++--- package.json | 2 +- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d23d6d..5000917 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,14 @@ All notable changes to this project will be documented in this file. Dates are d Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). -### [1.3.0](https://github.com/eea/volto-statistic-block/compare/1.2.1...1.3.0) - 9 June 2023 +### [1.3.1](https://github.com/eea/volto-statistic-block/compare/1.3.0...1.3.1) - 24 July 2023 -#### :rocket: New Features +#### :bug: Bug Fixes + +- fix: link in edit moves page - refs #254291 [dobri1408 - [`04fd659`](https://github.com/eea/volto-statistic-block/commit/04fd659d4110614950306d3b1904962189c05831)] +- fix: Prevent links from leaving page in edit - refs #254291 [dobri1408 - [`1f54dc1`](https://github.com/eea/volto-statistic-block/commit/1f54dc1c1708d46d69567ae66f5f249b95d5d6c8)] -- feat: added text aligment option [Miu Razvan - [`10dc32d`](https://github.com/eea/volto-statistic-block/commit/10dc32ded0890409ec4e316222c9ff6001a25597)] +### [1.3.0](https://github.com/eea/volto-statistic-block/compare/1.2.1...1.3.0) - 12 June 2023 #### :bug: Bug Fixes @@ -17,6 +20,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :house: Internal changes +- chore: [JENKINS] Deprecate circularity website [valentinab25 - [`3dc3490`](https://github.com/eea/volto-statistic-block/commit/3dc34908cf22514318c3fd737d517c8261da7fd4)] #### :hammer_and_wrench: Others @@ -41,16 +45,30 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). - use countup.js@2.5.0 [Miu Razvan - [`9dad099`](https://github.com/eea/volto-statistic-block/commit/9dad099fb6519688752bf4788023712bd9719671)] - bump version [Miu Razvan - [`e886513`](https://github.com/eea/volto-statistic-block/commit/e886513e63bf480dcdb5c9e0a627b31c6b4e989d)] +- Add Sonarqube tag using industry-frontend addons list [EEA Jenkins - [`7cf6d5b`](https://github.com/eea/volto-statistic-block/commit/7cf6d5b1427a9f17bb1c740d019130e323668286)] ### [1.1.0](https://github.com/eea/volto-statistic-block/compare/1.0.1...1.1.0) - 27 March 2023 #### :hammer_and_wrench: Others +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`1ecdcec`](https://github.com/eea/volto-statistic-block/commit/1ecdcec0ef98c891d9157db9592365fc10760b59)] +- Add Sonarqube tag using ims-frontend addons list [EEA Jenkins - [`e5f76cc`](https://github.com/eea/volto-statistic-block/commit/e5f76cc8f90dfa21ba1f2bb25b2491ae0e1b102d)] +- Add Sonarqube tag using advisory-board-frontend addons list [EEA Jenkins - [`439ba7f`](https://github.com/eea/volto-statistic-block/commit/439ba7f0016eb5ba86ef267309e0ddef7b81f379)] +- Add Sonarqube tag using advisory-board-frontend addons list [EEA Jenkins - [`1e72601`](https://github.com/eea/volto-statistic-block/commit/1e72601f1f9a7e8023c9130407d9ce28f1d7b6f9)] +- Add Sonarqube tag using climate-energy-frontend addons list [EEA Jenkins - [`9ab65ad`](https://github.com/eea/volto-statistic-block/commit/9ab65ad0f5b300a14590f5f260659749f4831956)] - test(Jenkins): Run tests and cypress with latest canary @plone/volto [Alin Voinea - [`75e506f`](https://github.com/eea/volto-statistic-block/commit/75e506fd7cf92296871ddf52f9b1c82a053ec931)] +- Add Sonarqube tag using cca-frontend addons list [EEA Jenkins - [`9297e9f`](https://github.com/eea/volto-statistic-block/commit/9297e9fb8a638a1564d7d7a6d2ebbea0056d775e)] +- Add Sonarqube tag using bise-frontend addons list [EEA Jenkins - [`6dde4aa`](https://github.com/eea/volto-statistic-block/commit/6dde4aae3c87bfe434a28719479e37078db11099)] +- yarn 3 [Alin Voinea - [`b4949e1`](https://github.com/eea/volto-statistic-block/commit/b4949e18f23a7965c76ef97a5790607a587987dc)] +- Add Sonarqube tag using marine-frontend addons list [EEA Jenkins - [`8820afa`](https://github.com/eea/volto-statistic-block/commit/8820afa4ab8fcebd17e90522196c1fde8b246e21)] +- Add Sonarqube tag using clms-frontend addons list [EEA Jenkins - [`8eb5b8d`](https://github.com/eea/volto-statistic-block/commit/8eb5b8de3f87152cb57faeb960233791b328c900)] +- Add Sonarqube tag using demo-kitkat-frontend addons list [EEA Jenkins - [`58f36bd`](https://github.com/eea/volto-statistic-block/commit/58f36bd4ae9dcdccbef182789957dbf0bb97da74)] +- Add Sonarqube tag using forests-frontend addons list [EEA Jenkins - [`3bb9a21`](https://github.com/eea/volto-statistic-block/commit/3bb9a2128a88c1196258722112a1957f60d332fc)] ### [1.0.1](https://github.com/eea/volto-statistic-block/compare/1.0.0...1.0.1) - 16 November 2022 #### :hammer_and_wrench: Others - test(estlint): Fix .project.eslintrc.js [Alin Voinea - [`d81878f`](https://github.com/eea/volto-statistic-block/commit/d81878fff2ba1be4446dd330560c46d8161f8bc2)] +- Add Sonarqube tag using circularity-frontend addons list [EEA Jenkins - [`a58f436`](https://github.com/eea/volto-statistic-block/commit/a58f436c5bb052fcdd13b213895c497881e48c67)] ## [1.0.0](https://github.com/eea/volto-statistic-block/compare/0.3.0...1.0.0) - 28 October 2022 #### :nail_care: Enhancements @@ -62,6 +80,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others - Cleanup [Alin Voinea - [`6d5f4a6`](https://github.com/eea/volto-statistic-block/commit/6d5f4a6d8048689e97e567316f24902d0865de88)] +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`5fd7ba7`](https://github.com/eea/volto-statistic-block/commit/5fd7ba7a4595e157bdb0ebb0932f1d3afd64b369)] ### [0.3.0](https://github.com/eea/volto-statistic-block/compare/0.2.0...0.3.0) - 19 September 2022 #### :nail_care: Enhancements @@ -92,6 +111,7 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others - Remove useless from block title [Alin Voinea - [`a3daf0a`](https://github.com/eea/volto-statistic-block/commit/a3daf0ad63666d0cd87eef465f42f525a5a307bc)] +- Add Sonarqube tag using eea-website-frontend addons list [EEA Jenkins - [`4a9ea35`](https://github.com/eea/volto-statistic-block/commit/4a9ea35d0717941e9d96a29e46dc85717140fccf)] ### [0.1.1](https://github.com/eea/volto-statistic-block/compare/0.1.0...0.1.1) - 10 March 2022 #### :hammer_and_wrench: Others @@ -102,4 +122,5 @@ Generated by [`auto-changelog`](https://github.com/CookPete/auto-changelog). #### :hammer_and_wrench: Others +- yarn bootstrap [Alin Voinea - [`ba85706`](https://github.com/eea/volto-statistic-block/commit/ba857067b16dc04f9bc970b0c0d4afd479bb7528)] - Initial commit [Alin Voinea - [`2e19fe2`](https://github.com/eea/volto-statistic-block/commit/2e19fe2708f89887d2d79f88696e197643ea10f4)] diff --git a/package.json b/package.json index 98bb17c..b102770 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@eeacms/volto-statistic-block", - "version": "1.3.0", + "version": "1.3.1", "description": "@eeacms/volto-statistic-block: Volto add-on", "main": "src/index.js", "author": "European Environment Agency: IDM2 A-Team",