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

Closed
Show file tree
Hide file tree
Changes from 7 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.getByText('Admin')).toBeInTheDocument();
expect(screen.getByText('User')).toBeInTheDocument();
});
Copy link

@coderabbitai coderabbitai bot Nov 23, 2024

Choose a reason for hiding this comment

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

⚠️ Potential issue

Strengthen the test coverage for role display functionality

The current test case is insufficient for verifying the role display bug fix:

  1. It only checks for the presence of role text without verifying if roles are correctly associated with specific users
  2. It doesn't test the specific scenario where roles weren't displaying in certain organizations
  3. Missing negative test cases

Consider enhancing the test with these scenarios:

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();
  
  // Verify specific user-role associations
  const nobleElement = screen.getByText('Noble Admin');
  expect(nobleElement.closest('tr')).toHaveTextContent('Admin');
  
  const regularUserElement = screen.getByText('Noble Mittal');
  expect(regularUserElement.closest('tr')).toHaveTextContent('User');
  
  // Test role visibility across different organizations
  // This would require additional mock data for different orgs
});

test('Should handle missing role data gracefully', async () => {
  // Add test with mock data missing role information
});

Copy link
Author

Choose a reason for hiding this comment

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

? This will not work roles are not in table

Copy link

Choose a reason for hiding this comment

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

@aditya-bansal-7, thank you for the clarification. Since roles are not displayed in the table, the suggested test adjustments won't be applicable. If there's anything else I can assist with, please let me know.


✏️ Learnings added
Learnt from: aditya-bansal-7
PR: PalisadoesFoundation/talawa-admin#2459
File: src/screens/UserPortal/People/People.test.tsx:226-242
Timestamp: 2024-11-24T19:35:44.807Z
Learning: In the `People` component, roles are not displayed in the table, so tests related to role display should account for this.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

});
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 @@ export default function people(): JSX.Element {

// 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 @@ export default function people(): JSX.Element {
};

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;
}

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);
}
}
}, [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);
}
} else if (mode == 1) {
} else if (mode === FILTER_MODES.ADMINS) {
if (data2) {
setMembers(data2.organizations[0].admins);
const organization = data2.organizations?.[0];
if (!organization) {
console.error('Organization not found');
return;
}
const admins = organization.admins.map((admin: InterfaceMember) => ({
...admin,
userType: 'Admin' as const,
}));
setMembers(admins);
}
}
}, [mode]);
Expand Down
Loading