Skip to content

Commit

Permalink
Add some safety for missing metadata that was causing build errors (#108
Browse files Browse the repository at this point in the history
)

* Add some safety for missing metadata that was causing build errors; fix #106

* Improve method for adding items to topic summary/landing page

* Update yarn.lock

* Update tests

* Update yarn lock
  • Loading branch information
nonprofittechy authored Nov 5, 2024
1 parent b996ef2 commit 6556268
Show file tree
Hide file tree
Showing 35 changed files with 495 additions and 247 deletions.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
52 changes: 24 additions & 28 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
import '@testing-library/jest-dom';
import { render, screen } from '@testing-library/react';
import Page from '../src/app/page';
import RootLayout from '../src/app/layout';
import { fetchInterviews } from '../src/data/fetchInterviewData';
const { getTopicNames } = require('../src/data/helpers/index');

describe('Layout component', () => {
beforeEach(() => {
render(<RootLayout />);
});
describe('getTopicNames', () => {
const legalTopics = [
{
name: 'housing',
codes: ['HO-01-00-00-00', 'HO-02-00-00-00'],
},
{
name: 'family',
codes: ['FA-00-00-00-00', 'FA-01-00-00-00'],
},
{
name: 'employment',
codes: ['EM-01-00-00-00'],
},
];

it('should contain a navigation bar', () => {
const navElement = screen.getByRole('navigation');
expect(navElement).toBeInTheDocument();
});
// Match the import path used in getTopicNames
jest.mock('../src/config/topics.config', () => ({
legalTopics,
}));

it('should contain a footer', () => {
const footerElement = screen.getByRole('contentinfo');
expect(footerElement).toBeInTheDocument();
it('should return the exact matching topic', () => {
const topicCodes = ['HO-02-00-00-00'];
const result = getTopicNames(topicCodes);
expect(result).toEqual(['housing']);
});
});

// Mock the fetchInterviews function from your data fetching module
jest.mock('../src/data/fetchInterviewData', () => ({
fetchInterviews: jest.fn(() =>
Promise.resolve({
interviewsByTopic: {
Topic1: [{ id: 1, name: 'Interview 1' }],
Topic2: [{ id: 2, name: 'Interview 2' }],
},
})
),
}));
43 changes: 43 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@testing-library/react": "^15.0.6",
"@types/bootstrap": "^5.2.6",
"@types/eslint": "^8",
"@types/jest": "^29.5.14",
"eslint": "8",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-prettier": "^5.1.3",
Expand Down
8 changes: 2 additions & 6 deletions src/app/components/TopicCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const TopicCard = ({
const [isExpanded, setIsExpanded] = useState(false);
const visibilityClass = index > 8 ? 'hidden' : '';
const displayInterviews = isExpanded
? interviews.slice(0, Math.min(10, interviews.length))
? interviews.slice(0, Math.min(20, interviews.length))
: interviews.slice(0, 3);
const remainingCount = interviews.length > 10 ? interviews.length - 10 : 0;
const cardClassName = isSpot ? 'spot-topic-card-parent' : 'topic-card-parent';
Expand Down Expand Up @@ -90,11 +90,7 @@ const TopicCard = ({
if (interview.metadata && interview.metadata.title) {
return (
<Link
key={
toUrlFriendlyString(interview.metadata.title) +
'-' +
index
}
key={`${toUrlFriendlyString(interview.filename)}-${topic.name}-${index}`}
className={
styles.FormTag +
' form-tag btn btn-outline-secondary border-2 align-self-start text-start'
Expand Down
4 changes: 4 additions & 0 deletions src/app/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export const toUrlFriendlyString = (title: string) => {
// if title isn't a string, return a default
if (typeof title !== 'string') {
return 'Untitled';
}
return title
.toLowerCase()
.replace(/[^a-z0-9]+/g, '-') // Replace special characters with hyphens
Expand Down
14 changes: 1 addition & 13 deletions src/config/topics.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const legalTopics: Topic[] = [
priority: 100,
},
{
codes: ['FA-00-00-00-00'],
codes: ['FA-00-00-00-00', 'ES-03-00-00-00'],
name: 'family',
long_name: 'Family and safety',
icon: 'people-roof',
Expand Down Expand Up @@ -185,15 +185,3 @@ export const legalTopics: Topic[] = [
priority: 0,
},
];

export function findParentTopic(tag: string) {
// Remove trailing sections of "-00" from the tag
const cleanedTag = tag.replace(/(-00)+$/, '');

// Find the first topic that starts with the same characters as the cleaned tag
const parentTopic = legalTopics.find((topic) =>
cleanedTag.startsWith(topic.codes[0].replace(/(-00)+$/, ''))
);

return parentTopic; // This will be undefined if no matching topic is found
}
Loading

0 comments on commit 6556268

Please sign in to comment.