Skip to content

Commit

Permalink
SELC-4661 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
OraldoDoci committed Apr 10, 2024
1 parent 1bfbb40 commit bb51678
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 7 deletions.
7 changes: 3 additions & 4 deletions src/components/partySelectionSearch/PartySelectionSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ export default function PartySelectionSearch({
const [searchQuery, setSearchQuery] = useState('');

useEffect(() => {
// Initially, show the first batch of parties
setVisibleParties(filteredParties.slice(0, 50));
}, [filteredParties]);

Expand All @@ -74,11 +73,11 @@ export default function PartySelectionSearch({
return () => {
containerRef.current?.removeEventListener('scroll', handleScroll);
};
}, [visibleParties, filteredParties, selectedParty]); // Re-run effect when visibleParties change
}, [visibleParties, filteredParties, selectedParty]);

const loadMoreParties = () => {
const remainingParties = filteredParties.slice(visibleParties.length); // Get remaining parties
const nextBatch = remainingParties.slice(0, 50); // Load next batch from remaining parties
const remainingParties = filteredParties.slice(visibleParties.length);
const nextBatch = remainingParties.slice(0, 50);
setVisibleParties((prevParties) => [...prevParties, ...nextBatch]);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { fireEvent, getByText, render, screen } from '@testing-library/react';
import { Party } from '../../../model/Party';
import { fireEvent, getByText, render, screen, waitFor } from '@testing-library/react';
import { BaseParty, Party } from '../../../model/Party';
import PartyAccountItemSelection from '../PartyAccountItemSelection';
import PartySelectionSearch from '../PartySelectionSearch';
import './../../../locale';
import { mockedBaseParties } from '../../../services/__mocks__/partyService';
import React from 'react';
import { renderWithProviders } from '../../../utils/test-utils';

let selectedParty: Party | null = null;
let selectedParty: BaseParty | null = null;

const parties: Array<Party> = [
{
Expand Down Expand Up @@ -215,3 +218,39 @@ test('Test TOBEVALIDATED party', () => {
screen.getByText('In attesa');
}
});

test('Test disabled party', async () => {
const generateMockedParties = (N: number): Array<BaseParty> =>
Array.from({ length: N }, (_, index) => {
const partyId = `party-${index}`;
return {
partyId,
description: `Party ${index}`,
status: index % 2 === 0 ? 'ACTIVE' : 'PENDING',
userRole: index % 2 === 0 ? 'ADMIN' : 'LIMITED',
};
});

const mockedBaseParties = generateMockedParties(60);
renderWithProviders(
<PartySelectionSearch
parties={mockedBaseParties}
onPartySelectionChange={(p) => (selectedParty = p)}
selectedParty={selectedParty}
/>
);

const party40 = screen.getByText('Party 40');
expect(party40).toBeInTheDocument();

const party52 = screen.queryByText('Party 52');
expect(party52).not.toBeInTheDocument();

const input = document.getElementById('search') as HTMLInputElement;
fireEvent.change(input, { target: { value: 'Party 5' } });
expect(input.getAttribute('value')).toBe('Party 5');

expect(party40).not.toBeInTheDocument();

await waitFor(() => expect(screen.getByText('Party 52')).toBeInTheDocument());
});

0 comments on commit bb51678

Please sign in to comment.