From 64ae85d86cd002d664fc031b077ec70ed5864afe Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Wed, 17 Mar 2021 15:18:58 +0100 Subject: [PATCH 1/5] Publisher dataset count by name tests. --- .../PublisherDatasetCountByName/index.jsx | 28 +++++++++++++ .../PublisherDatasetCountByName/index.test.js | 42 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/components/PublisherDatasetCountByName/index.jsx create mode 100644 src/components/PublisherDatasetCountByName/index.test.js diff --git a/src/components/PublisherDatasetCountByName/index.jsx b/src/components/PublisherDatasetCountByName/index.jsx new file mode 100644 index 00000000..27a87db7 --- /dev/null +++ b/src/components/PublisherDatasetCountByName/index.jsx @@ -0,0 +1,28 @@ +import React from 'react'; +import PropTypes from 'prop-types'; +import { Link } from "@reach/router"; + +const PublisherDatasetCountByName = (props) => { + const { name, searchUrl, datasetCount} = props; + const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; + let str; + if (datasetCount) { + str = (datasetCount === 1) ? '1 dataset' : `${datasetCount}x datasets`; + } else { + str = 'datasets'; + } + return ( + + {str} + + ); +}; + + +export default PublisherDatasetCountByName; + +PublisherDatasetCountByName.propTypes = { + name: PropTypes.string, + searchUrl: PropTypes.string, + datasetCount: PropTypes.string +}; diff --git a/src/components/PublisherDatasetCountByName/index.test.js b/src/components/PublisherDatasetCountByName/index.test.js new file mode 100644 index 00000000..663d3cfb --- /dev/null +++ b/src/components/PublisherDatasetCountByName/index.test.js @@ -0,0 +1,42 @@ +import React from 'react'; +import { render, screen } from '@testing-library/react'; +import '@testing-library/jest-dom/extend-expect'; +import PublisherDatasetCountByName from './index'; +import {find} from 'lodash'; + + const datasets = [ + { + "name": "State Economic Council", + "total": "3" + } +]; + +const countDatasetsByName = (publisher, datasets) => { + const result = find(datasets, { 'name': publisher }); + if (typeof result !== 'undefined') { + return result; + } + return null; +}; + +describe('', () => { + + test('If no dataset renders, just a link to the page.', () => { + const val = countDatasetsByName('No matching organization', datasets); + expect(val).toBeNull(); + render(); + expect(screen.getByText('datasets')).toBeInTheDocument(); + }); + + test('If there is a publisher with datasets render the dataset count.',() => { + const val = countDatasetsByName('State Economic Council', datasets); + render(); + expect(screen.getByText('3x datasets')).toBeInTheDocument(); + }); + + test('Dataset count with just one item.',() => { + const val = countDatasetsByName('State Economic Council', datasets); + render(); + }); + +}); From 7b6e1f09601635fa322fb59cd4d1e379e0980a3a Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Wed, 17 Mar 2021 17:09:48 +0100 Subject: [PATCH 2/5] Adding Docx for Publisher Dataset Count (by name) component. --- .../PublisherDatasetCountByName/doc.mdx | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/components/PublisherDatasetCountByName/doc.mdx diff --git a/src/components/PublisherDatasetCountByName/doc.mdx b/src/components/PublisherDatasetCountByName/doc.mdx new file mode 100644 index 00000000..ab90d1e8 --- /dev/null +++ b/src/components/PublisherDatasetCountByName/doc.mdx @@ -0,0 +1,38 @@ +--- +name: Publisher Dataset Count (by name). +menu: Components +route: /components/publisher-dataset-count-by-name +--- + +import { Playground, Props } from 'docz' +import PublisherDatasetCountByName from './index' + +# Publisher dataset count (by name). + +## Properties + + + +### With no datasetCount. + + + + +## Basic usage +### With a count of 1. + + + + +With a count of more than 1. + + + From e9d67f2a416868f70a2f19f01b947d13a7c898da Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Mon, 22 Mar 2021 13:19:21 +0100 Subject: [PATCH 3/5] Tests for counting datasets within and Organization. --- src/components/Organization/index.jsx | 36 +++++++++++++++++-- src/components/Organization/index.test.jsx | 28 ++++++++++++++- .../PublisherDatasetCountByName/index.test.js | 20 ----------- 3 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/components/Organization/index.jsx b/src/components/Organization/index.jsx index 5a116cb2..f3060a8b 100644 --- a/src/components/Organization/index.jsx +++ b/src/components/Organization/index.jsx @@ -1,12 +1,27 @@ -import React from "react"; +import React, { useState, useEffect } from 'react'; import PropTypes from "prop-types"; import { Link } from "@reach/router"; +import PublisherDatasetCountByName from "../PublisherDatasetCountByName"; +import axios from 'axios'; function Organization(props) { - const { name, description, imageUrl, searchUrl, alignment } = props; + const { name, description, imageUrl, searchUrl, alignment, datasetCount } = props; const image = {name; const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; + const [posts, setPosts] = useState(); + + const fetchData = async () => { + const { data } = await axios.get('http://demo.getdkan.org/data.json'); + console.log(data); + console.log("Name", name); + setPosts(data); + }; + + useEffect(() => { + fetchData(); + }, []); + return (
@@ -20,10 +35,26 @@ function Organization(props) { {description}
)} + + {posts && posts.dataset !== 'undefined' ? countDatasetsByName("State Economic Council", posts.dataset) : null} + + ); } +export const countDatasetsByName = (publisher, datasets) => { + // console.log("datasets", datasets); + // return "foobar"; + const publishers = datasets.map((data, index, arr) => {return data.publisher; }); + const result = publishers.filter((p) => {return p.name === publisher;}) + console.log("RES: ", result); + if (typeof result !== 'undefined' && result.length) { + return result.length; + } + return null; +}; + Organization.defaultProps = { alignment: "center", name: "", @@ -33,6 +64,7 @@ Organization.defaultProps = { Organization.propTypes = { alignment: PropTypes.string, + datasetCount: PropTypes.string, name: PropTypes.string, description: PropTypes.string, imageUrl: PropTypes.string, diff --git a/src/components/Organization/index.test.jsx b/src/components/Organization/index.test.jsx index 798c3bf5..fc0d5ce5 100644 --- a/src/components/Organization/index.test.jsx +++ b/src/components/Organization/index.test.jsx @@ -1,11 +1,37 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; -import Organization from './index'; +import Organization, {countDatasetsByName} from './index'; +import PublisherDatasetCountByName from '../PublisherDatasetCountByName'; + +const data = + [{"publisher": { + "@type": "org:Organization", + "name": "State Economic Council" + }}, + {"publisher": { + "@type": "org:Organization", + "name": "State Economic Council" + }}]; + describe('', () => { test('renders a heading', () => { render(); expect(screen.getByRole('heading', 'DKAN')).toBeInTheDocument(); }); + + test('Has a publisher name.', () => { + expect(data[0]['publisher']['name']).toEqual("State Economic Council"); + }); + + test('renders with a dataset link with no count', () => { + render(); + expect(screen.getByText('datasets')).toBeInTheDocument(); + }); + + test('Calculates a count from data',() => { + expect(countDatasetsByName("State Economic Council", data)).toEqual(2); + }); + }); diff --git a/src/components/PublisherDatasetCountByName/index.test.js b/src/components/PublisherDatasetCountByName/index.test.js index 663d3cfb..79f10aef 100644 --- a/src/components/PublisherDatasetCountByName/index.test.js +++ b/src/components/PublisherDatasetCountByName/index.test.js @@ -2,40 +2,20 @@ import React from 'react'; import { render, screen } from '@testing-library/react'; import '@testing-library/jest-dom/extend-expect'; import PublisherDatasetCountByName from './index'; -import {find} from 'lodash'; - - const datasets = [ - { - "name": "State Economic Council", - "total": "3" - } -]; - -const countDatasetsByName = (publisher, datasets) => { - const result = find(datasets, { 'name': publisher }); - if (typeof result !== 'undefined') { - return result; - } - return null; -}; describe('', () => { test('If no dataset renders, just a link to the page.', () => { - const val = countDatasetsByName('No matching organization', datasets); - expect(val).toBeNull(); render(); expect(screen.getByText('datasets')).toBeInTheDocument(); }); test('If there is a publisher with datasets render the dataset count.',() => { - const val = countDatasetsByName('State Economic Council', datasets); render(); expect(screen.getByText('3x datasets')).toBeInTheDocument(); }); test('Dataset count with just one item.',() => { - const val = countDatasetsByName('State Economic Council', datasets); render(); }); From 3db3a0998c38a8ccc71a588c500cb6d631ed16cd Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Mon, 22 Mar 2021 16:40:46 +0100 Subject: [PATCH 4/5] Update an Organization so that it will accept a component with a dataset count, and query and endpoint for count data. --- src/components/Organization/index.jsx | 30 +++++++++++++-------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/components/Organization/index.jsx b/src/components/Organization/index.jsx index f3060a8b..35b67b99 100644 --- a/src/components/Organization/index.jsx +++ b/src/components/Organization/index.jsx @@ -5,17 +5,15 @@ import PublisherDatasetCountByName from "../PublisherDatasetCountByName"; import axios from 'axios'; function Organization(props) { - const { name, description, imageUrl, searchUrl, alignment, datasetCount } = props; + const { name, description, imageUrl, searchUrl, alignment} = props; const image = {name; const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; - - const [posts, setPosts] = useState(); + const [dataObj, setDataObj] = useState(); const fetchData = async () => { - const { data } = await axios.get('http://demo.getdkan.org/data.json'); - console.log(data); - console.log("Name", name); - setPosts(data); + axios.get('http://demo.getdkan.org/data.json') + .then(res => (setDataObj(res.data))) + .catch(err => (console.log("Error, check URL/Cors.", err))); }; useEffect(() => { @@ -36,21 +34,24 @@ function Organization(props) { )} - {posts && posts.dataset !== 'undefined' ? countDatasetsByName("State Economic Council", posts.dataset) : null} - - + {dataObj && dataObj.dataset !== 'undefined' ? + : + + } ); } export const countDatasetsByName = (publisher, datasets) => { - // console.log("datasets", datasets); - // return "foobar"; const publishers = datasets.map((data, index, arr) => {return data.publisher; }); - const result = publishers.filter((p) => {return p.name === publisher;}) - console.log("RES: ", result); + const result = publishers.filter((p) => {return p.name === publisher;}); if (typeof result !== 'undefined' && result.length) { return result.length; + } return null; }; @@ -64,7 +65,6 @@ Organization.defaultProps = { Organization.propTypes = { alignment: PropTypes.string, - datasetCount: PropTypes.string, name: PropTypes.string, description: PropTypes.string, imageUrl: PropTypes.string, From d0d2c0a72d4d35e1d1ea0d7a6dd0d8df1ce5e8f7 Mon Sep 17 00:00:00 2001 From: Alex Scott Date: Mon, 22 Mar 2021 19:30:46 +0100 Subject: [PATCH 5/5] Adding organizationEndpoint as a property for organizations. --- src/components/Organization/index.jsx | 20 +++++++++++++++---- .../PublisherDatasetCountByName/index.jsx | 1 - src/components/PublisherList/index.jsx | 4 +++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/components/Organization/index.jsx b/src/components/Organization/index.jsx index 35b67b99..ff78568b 100644 --- a/src/components/Organization/index.jsx +++ b/src/components/Organization/index.jsx @@ -5,15 +5,26 @@ import PublisherDatasetCountByName from "../PublisherDatasetCountByName"; import axios from 'axios'; function Organization(props) { - const { name, description, imageUrl, searchUrl, alignment} = props; + const { name, + description, + imageUrl, + searchUrl, + alignment, + organizationEndpoint} = props; + const image = {name; const link = searchUrl ? searchUrl : `search/?publisher__name=${name}`; const [dataObj, setDataObj] = useState(); const fetchData = async () => { - axios.get('http://demo.getdkan.org/data.json') - .then(res => (setDataObj(res.data))) - .catch(err => (console.log("Error, check URL/Cors.", err))); + const endpoint = organizationEndpoint ? organizationEndpoint.replace("api/1", "data.json") : null; + if (endpoint) { + axios.get(endpoint) + .then(res => (setDataObj(res.data))) + .catch(err => (console.log("Error, check URL/Cors.", err))); + } else { + console.log("No search endpoint defined for Organization/s, so no dataset info available."); + } }; useEffect(() => { @@ -69,6 +80,7 @@ Organization.propTypes = { description: PropTypes.string, imageUrl: PropTypes.string, searchUrl: PropTypes.string, + organizationEndpoint: PropTypes.string, }; export default Organization; diff --git a/src/components/PublisherDatasetCountByName/index.jsx b/src/components/PublisherDatasetCountByName/index.jsx index 27a87db7..8626eae6 100644 --- a/src/components/PublisherDatasetCountByName/index.jsx +++ b/src/components/PublisherDatasetCountByName/index.jsx @@ -18,7 +18,6 @@ const PublisherDatasetCountByName = (props) => { ); }; - export default PublisherDatasetCountByName; PublisherDatasetCountByName.propTypes = { diff --git a/src/components/PublisherList/index.jsx b/src/components/PublisherList/index.jsx index c7b12146..0e2ae376 100644 --- a/src/components/PublisherList/index.jsx +++ b/src/components/PublisherList/index.jsx @@ -4,7 +4,7 @@ import Organization from '../Organization'; function PublisherList(props) { const { - items, className, + items, className, organizationEndpoint } = props; let content = (
); @@ -15,6 +15,7 @@ function PublisherList(props) { key={item.identifier} imageUrl={item.imageUrl} description={item.description} + organizationEndpoint={organizationEndpoint} searchUrl={item.searchUrl} alignment={item.alignment} /> @@ -40,6 +41,7 @@ PublisherList.propTypes = { identifier: PropTypes.string, imageUrl: PropTypes.string, searchUrl: PropTypes.string, + organizationEndpoint: PropTypes.string, })).isRequired, className: PropTypes.string, };