Skip to content

Commit

Permalink
Tests for counting datasets within and Organization.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Scott committed Mar 24, 2021
1 parent 7b6e1f0 commit ee20ec3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 23 deletions.
36 changes: 34 additions & 2 deletions src/components/Organization/index.jsx
Original file line number Diff line number Diff line change
@@ -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 = <img alt={name || 'Organization Image'} src={imageUrl} />;
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 (
<div className="dc-org-block" style={{ textAlign: alignment }}>
<Link to={link} className="dc-org-image" alt="Organization Logo">
Expand All @@ -20,10 +35,26 @@ function Organization(props) {
{description}
</div>
)}

{posts && posts.dataset !== 'undefined' ? countDatasetsByName("State Economic Council", posts.dataset) : null}

<PublisherDatasetCountByName datasetCount={datasetCount} />
</div>
);
}

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: "",
Expand All @@ -33,6 +64,7 @@ Organization.defaultProps = {

Organization.propTypes = {
alignment: PropTypes.string,
datasetCount: PropTypes.string,
name: PropTypes.string,
description: PropTypes.string,
imageUrl: PropTypes.string,
Expand Down
23 changes: 22 additions & 1 deletion src/components/Organization/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,32 @@
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('<Organization />', () => {
test('renders a heading', () => {
render(<Organization name="DKAN" />);
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(<Organization name="DKAN" />);
expect(screen.getByText('datasets')).toBeInTheDocument();
});
});
20 changes: 0 additions & 20 deletions src/components/PublisherDatasetCountByName/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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('<PublisherDatasetCountByName />', () => {

test('If no dataset renders, just a link to the page.', () => {
const val = countDatasetsByName('No matching organization', datasets);
expect(val).toBeNull();
render(<PublisherDatasetCountByName name="Non matching organization." />);
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(<PublisherDatasetCountByName name="State Economic Council" datasetCount="3" />);
expect(screen.getByText('3x datasets')).toBeInTheDocument();
});

test('Dataset count with just one item.',() => {
const val = countDatasetsByName('State Economic Council', datasets);
render(<PublisherDatasetCountByName name="State Economic Council" datasetCount="1" />);
});

Expand Down

0 comments on commit ee20ec3

Please sign in to comment.