-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
205 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 0 additions & 108 deletions
108
client-report/src/components/lists/participantGroups.js
This file was deleted.
Oops, something went wrong.
128 changes: 128 additions & 0 deletions
128
client-report/src/components/lists/participantGroups.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// // Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
import React, { useState, useEffect } from "react"; | ||
import Group from "./participantGroup.jsx"; | ||
import * as globals from "../globals.js"; | ||
import Metadata from "./metadata.jsx"; | ||
|
||
const ParticipantGroups = ({ | ||
conversation, | ||
ptptCount, | ||
math, | ||
comments, | ||
style, | ||
voteColors, | ||
formatTid, | ||
demographics, | ||
groupNames, | ||
badTids, | ||
repfulAgreeTidsByGroup, | ||
repfulDisageeTidsByGroup, | ||
report, | ||
}) => { | ||
const [isLoading, setIsLoading] = useState(true); | ||
const [groups, setGroups] = useState([]); | ||
|
||
useEffect(() => { | ||
if (!conversation || !math || !comments) return; | ||
|
||
const processGroups = () => { | ||
const processedGroups = Object.keys(math["repness"]).map((gid) => { // Use Object.keys and map | ||
gid = Number(gid); | ||
|
||
let otherGroupVotes = { | ||
votes: [], | ||
"n-members": 0, | ||
}; | ||
|
||
const MAX_CLUSTERS = 50; | ||
const temp = math["group-votes"]; | ||
|
||
for (let ogid = 0; ogid < MAX_CLUSTERS; ogid++) { | ||
if (ogid === gid || !temp[ogid]) { | ||
continue; | ||
} | ||
otherGroupVotes["n-members"] += temp[ogid]["n-members"]; | ||
const commentVotes = temp[ogid].votes; | ||
|
||
Object.keys(commentVotes || {}).forEach((tid) => { // Use Object.keys and forEach | ||
tid = Number(tid); | ||
const voteObj = commentVotes[tid]; | ||
if (voteObj) { | ||
if (!otherGroupVotes.votes[tid]) { | ||
otherGroupVotes.votes[tid] = { A: 0, D: 0, S: 0 }; | ||
} | ||
otherGroupVotes.votes[tid].A += voteObj.A; | ||
otherGroupVotes.votes[tid].D += voteObj.D; | ||
otherGroupVotes.votes[tid].S += voteObj.S; | ||
} | ||
}); | ||
} | ||
|
||
return ( | ||
<Group | ||
key={gid} | ||
comments={comments} | ||
gid={gid} | ||
conversation={conversation} | ||
demographicsForGroup={demographics[gid]} | ||
groupComments={math["repness"][gid]} // Access directly | ||
groupName={groupNames[gid]} | ||
groupVotesForThisGroup={math["group-votes"][gid]} | ||
groupVotesForOtherGroups={otherGroupVotes} | ||
formatTid={formatTid} | ||
ptptCount={ptptCount} | ||
groupNames={groupNames} | ||
badTids={badTids} | ||
repfulAgreeTidsByGroup={repfulAgreeTidsByGroup} | ||
repfulDisageeTidsByGroup={repfulDisageeTidsByGroup} | ||
math={math} | ||
report={report} | ||
voteColors={voteColors} | ||
/> | ||
); | ||
}); | ||
|
||
setGroups(processedGroups); | ||
setIsLoading(false); | ||
}; | ||
|
||
processGroups(); | ||
}, [conversation, math, comments]); | ||
|
||
const styles = { | ||
base: {}, | ||
...style, | ||
}; | ||
|
||
return ( | ||
<div style={styles}> | ||
<div> | ||
<p style={globals.primaryHeading}> Opinion Groups </p> | ||
<p style={globals.paragraph}> | ||
Across {ptptCount} total participants, {math && Object.keys(math["group-votes"])?.length}{" "} | ||
opinion groups emerged. There are two factors that define an opinion | ||
group. First, each opinion group is made up of a number of participants | ||
who tended to vote similarly on multiple statements. Second, each group | ||
of participants who voted similarly will have also voted distinctly | ||
differently from other groups. | ||
</p> | ||
<Metadata | ||
math={math} | ||
comments={comments} | ||
conversation={conversation} | ||
ptptCount={ptptCount} | ||
voteColors={voteColors} | ||
formatTid={formatTid} | ||
/> | ||
{isLoading ? ( | ||
<div>Loading Groups</div> | ||
) : ( | ||
groups | ||
)} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ParticipantGroups; |
70 changes: 70 additions & 0 deletions
70
client-report/src/components/lists/participantGroups.test.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import ParticipantGroups from './ParticipantGroups'; | ||
import * as globals from '../globals'; // Mock globals if necessary | ||
import Metadata from './metadata.jsx'; // Mock Metadata | ||
import Group from './participantGroup.jsx'; // Mock Group | ||
import '@testing-library/jest-dom'; | ||
|
||
jest.mock('../globals', () => ({ | ||
primaryHeading: { fontSize: '20px' }, | ||
paragraph: { fontSize: '16px' }, | ||
})); | ||
|
||
jest.mock('./metadata.jsx', () => () => <div data-testid="mock-metadata" />); | ||
jest.mock('./participantGroup.jsx', () => ({ groupName }) => ( | ||
<div data-testid={`mock-group-${groupName}`}>{groupName}</div> | ||
)); | ||
|
||
describe('ParticipantGroups Component', () => { | ||
const mockProps = { | ||
conversation: {}, | ||
ptptCount: 100, | ||
math: { | ||
"group-votes": { | ||
0: { "n-members": 20, votes: { 1: { A: 10, D: 5, S: 5 }, 2: { A: 15, D: 0, S: 5 } } }, | ||
1: { "n-members": 30, votes: { 1: { A: 5, D: 15, S: 10 }, 3: { A: 20, D: 5, S: 5 } } }, | ||
}, | ||
repness: { | ||
0: { someData: 'group0data' }, | ||
1: { someData: 'group1data' }, | ||
}, | ||
}, | ||
comments: {}, | ||
voteColors: {}, | ||
formatTid: jest.fn((tid) => `TID${tid}`), | ||
demographics: { | ||
0: {someDemo: "demo0"}, | ||
1: {someDemo: "demo1"} | ||
}, | ||
groupNames: { | ||
0: "Group A", | ||
1: "Group B" | ||
}, | ||
badTids: [], | ||
repfulAgreeTidsByGroup: {}, | ||
repfulDisageeTidsByGroup: {}, | ||
report: {}, | ||
style: { backgroundColor: 'lightgray' } | ||
}; | ||
|
||
it('renders loading message when data is missing', () => { | ||
render(<ParticipantGroups />); | ||
expect(screen.getByText('Loading Groups')).toBeInTheDocument(); | ||
}); | ||
|
||
it('renders component with groups when data is present', () => { | ||
render(<ParticipantGroups {...mockProps} />); | ||
|
||
expect(screen.getByText('Opinion Groups')).toBeInTheDocument(); | ||
expect(screen.getByText(/Across 100 total participants, 2 opinion groups emerged/)).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-metadata')).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-group-Group A')).toBeInTheDocument(); | ||
expect(screen.getByTestId('mock-group-Group B')).toBeInTheDocument(); | ||
}); | ||
|
||
it("renders correct number of groups", () => { | ||
render(<ParticipantGroups {...mockProps} />); | ||
expect(screen.getAllByTestId(/mock-group-/).length).toBe(2); | ||
}) | ||
}); |