Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix User Role in People Section #2459

Open
wants to merge 16 commits into
base: develop-postgres
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/screens/UserPortal/People/People.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,22 @@ describe('Testing People Screen [User Portal]', () => {
expect(screen.queryByText('Noble Admin')).toBeInTheDocument();
expect(screen.queryByText('Noble Mittal')).not.toBeInTheDocument();
});

test('Members should be rendered with correct user type', async () => {
render(
<MockedProvider addTypename={false} link={link}>
<BrowserRouter>
<Provider store={store}>
<I18nextProvider i18n={i18nForTest}>
<People />
</I18nextProvider>
</Provider>
</BrowserRouter>
</MockedProvider>,
);

await wait();
expect(screen.queryByText('Admin')).toBeInTheDocument();
expect(screen.queryByText('User')).toBeInTheDocument();
Comment on lines +239 to +241
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Strengthen test assertions

The current assertions using queryByText are too broad and may pass even if roles appear in unintended locations.

Consider:

  1. Using more specific queries that target the intended elements
  2. Adding test data for multiple organizations to verify the original bug scenario
  3. Including negative test cases

Example structure:

// Verify role display in specific organization
await wait();
const org1Members = screen.getByTestId('org-1-members');
// Add your specific assertions based on how roles are actually displayed

// Verify role display in different organization
const org2Members = screen.getByTestId('org-2-members');
// Add your specific assertions

// Negative test case
expect(screen.queryByTestId('invalid-role')).not.toBeInTheDocument();

});
});
58 changes: 50 additions & 8 deletions src/screens/UserPortal/People/People.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@

// State for managing the number of rows per page in pagination
const [rowsPerPage, setRowsPerPage] = useState<number>(5);
const [members, setMembers] = useState([]);
const [members, setMembers] = useState<InterfaceMember[]>([]);
const [mode, setMode] = useState<number>(0);
const [updatedMembers, setUpdatedMembers] = useState<InterfaceMember[]>([]);

// Extracting organization ID from URL parameters
const { orgId: organizationId } = useParams();
Expand Down Expand Up @@ -135,23 +136,64 @@
};

useEffect(() => {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
if (data && data2) {
// Ensure organization exists
const organization = data2.organizations?.[0];
if (!organization) {
console.error('Failed to load organization data');
return;

Check warning on line 144 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L143-L144

Added lines #L143 - L144 were not covered by tests
}

interface InterfaceAdmin {
_id: string;
}
const adminIds = organization.admins.map(
(admin: InterfaceAdmin) => admin._id,
);
try {
const updatedMembers = data.organizationsMemberConnection.edges.map(
(member: InterfaceMember) => {
const isAdmin = adminIds.includes(member._id);
return {
...member,
userType: isAdmin ? 'Admin' : 'User',
};
},
);
setUpdatedMembers(updatedMembers);
setMembers(updatedMembers);
} catch (error) {
console.error('Failed to process member data:', error);

Check warning on line 166 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L166

Added line #L166 was not covered by tests
}
}
}, [data]);
}, [data, data2]);

const FILTER_MODES = {
ALL_MEMBERS: 0,
ADMINS: 1,
} as const;

/**
* Updates the list of members based on the selected filter mode.
*/
/* istanbul ignore next */
useEffect(() => {
if (mode == 0) {
if (mode === FILTER_MODES.ALL_MEMBERS) {
if (data) {
setMembers(data.organizationsMemberConnection.edges);
setMembers(updatedMembers);

Check warning on line 183 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L183

Added line #L183 was not covered by tests
}
} else if (mode == 1) {
} else if (mode === FILTER_MODES.ADMINS) {
if (data2) {
setMembers(data2.organizations[0].admins);
const organization = data2.organizations?.[0];

Check warning on line 187 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L187

Added line #L187 was not covered by tests
if (!organization) {
console.error('Organization not found');
return;

Check warning on line 190 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L189-L190

Added lines #L189 - L190 were not covered by tests
}
const admins = organization.admins.map((admin: InterfaceMember) => ({

Check warning on line 192 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L192

Added line #L192 was not covered by tests
...admin,
userType: 'Admin' as const,
}));
setMembers(admins);

Check warning on line 196 in src/screens/UserPortal/People/People.tsx

View check run for this annotation

Codecov / codecov/patch

src/screens/UserPortal/People/People.tsx#L196

Added line #L196 was not covered by tests
}
}
}, [mode]);
Expand Down
Loading