Skip to content

Commit

Permalink
Merge pull request stakwork#291 from Fog3211/test-ticketModalPage_Edi…
Browse files Browse the repository at this point in the history
…tAction

[Tests][Component] Add tests to frontend/app/src/pages/tickets/TicketModalPage.tsx For Edit Actions
  • Loading branch information
elraphty authored Feb 26, 2024
2 parents 00ae1d0 + bf8562d commit 435b926
Showing 1 changed file with 240 additions and 4 deletions.
244 changes: 240 additions & 4 deletions src/pages/tickets/__tests__/TicketModalPage.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import '@testing-library/jest-dom';
import { act, render, waitFor, fireEvent, screen, within } from '@testing-library/react';
import {
act,
render,
waitFor,
fireEvent,
screen,
within,
getByTestId,
getAllByText,
queryByText
} from '@testing-library/react';
import { user } from '__test__/__mockData__/user';
import { mockBountiesMutated, newBounty } from 'bounties/__mock__/mockBounties.data';
import { DollarConverter, formatSat, getSessionValue, satToUsd } from 'helpers';
Expand All @@ -8,9 +18,10 @@ import { MemoryRouter, Route } from 'react-router-dom';
import { mainStore } from 'store/main';
import { useIsMobile } from 'hooks';
import { uiStore } from 'store/ui';
import { unpaidString } from 'people/widgetViews/summaries/constants';
import userEvent from '@testing-library/user-event';
import { TicketModalPage } from '../TicketModalPage';
import { withCreateModal } from '../../../components/common/withCreateModal';
import { unpaidString } from 'people/widgetViews/summaries/constants';

jest.mock('hooks', () => ({
useIsMobile: jest.fn()
Expand Down Expand Up @@ -130,7 +141,7 @@ describe('TicketModalPage Component', () => {
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route
path="/bounty/:bountyId"
render={(props) => <TicketModalPage setConnectPerson={connectFn} {...props} />}
render={(props: any) => <TicketModalPage setConnectPerson={connectFn} {...props} />}
/>
</MemoryRouter>
);
Expand Down Expand Up @@ -487,7 +498,7 @@ describe('TicketModalPage Component', () => {
expect(getByText(DollarConverter(mockBountiesMutated[1].body.price))).toBeInTheDocument();
// usd conversion
expect(
getByText(satToUsd(parseInt(mockBountiesMutated[1].body.price)) + ' USD')
getByText(`${satToUsd(parseInt(mockBountiesMutated[1].body.price))} USD`)
).toBeInTheDocument();
screen.debug(undefined, Infinity);
// Estimated Hours
Expand Down Expand Up @@ -596,4 +607,229 @@ describe('TicketModalPage Component', () => {
});
});
});

it('test that if a bounty is open and I am the creator of the bounty, I should be able to invite a bounty hunter', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);
uiStore.setMeInfo(user);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);
jest.spyOn(mainStore, 'getBountyIndexById').mockReturnValue(Promise.resolve(1234));
jest.spyOn(mainStore, 'saveBounty').mockResolvedValue();

await act(async () => {
const { getByText } = render(
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route path="/bounty/:bountyId" component={TicketModalPage} />
</MemoryRouter>
);

await waitFor(() => getByText('Not Assigned'));
fireEvent.click(getByText('Not Assigned'));

await waitFor(() => {
expect(getByText('Assign Developer')).toBeInTheDocument();
});
});
});

it('test that when I invite a new hunter, the hunter should be the assignee for that bounty', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);
uiStore.setMeInfo(user);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);
jest.spyOn(mainStore, 'getBountyIndexById').mockReturnValue(Promise.resolve(1234));
jest.spyOn(mainStore, 'saveBounty').mockResolvedValue();
jest.spyOn(mainStore, 'getPeopleByNameAliasPubkey').mockResolvedValue(
Promise.resolve([
{
id: 1,
owner_alias: 'TEST_NAME_1',
extras: {
coding_languages: [
{ value: 'R', label: 'R' },
{ value: 'C++', label: 'C++' }
]
}
} as any
])
);

await act(async () => {
const { getByText } = render(
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route path="/bounty/:bountyId" component={TicketModalPage} />
</MemoryRouter>
);

await waitFor(() => getByText('Not Assigned'));
fireEvent.click(getByText('Not Assigned'));
await waitFor(() => {
expect(getByText('Assign Developer')).toBeInTheDocument();
});

fireEvent.click(screen.getByPlaceholderText('Type to search ...'));
await userEvent.type(screen.getByPlaceholderText('Type to search ...'), 'TEST_NAME');

await waitFor(() => getByText('TEST_NAME_1'));

fireEvent.click(screen.getAllByText('Assign')[0]);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);

await waitFor(() => {
expect(screen.queryByText('Assign Developer')).toBe(null);
expect(mainStore.saveBounty).toHaveBeenCalled();
expect(mainStore.getBountyById).toHaveBeenCalled();
});

expect(screen.getByText('Edit')).toBeInTheDocument();
expect(screen.getByText('TEST_NAME_1')).toBeInTheDocument();
});
});

it('test that the creator of a bounty can click on the Edit Bounty, and the Edit Modal should pop up', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);
uiStore.setMeInfo(user);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);
jest.spyOn(mainStore, 'getBountyIndexById').mockReturnValue(Promise.resolve(1234));
jest.spyOn(mainStore, 'saveBounty').mockResolvedValue();

await act(async () => {
const { getByText, getByTestId } = render(
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route path="/bounty/:bountyId" component={TicketModalPage} />
</MemoryRouter>
);

await waitFor(() => getByText('Edit'));
fireEvent.click(getByText('Edit'));
await waitFor(() => getByText('Edit Bounty'));
expect(getByText('Edit Bounty')).toBeInTheDocument();
expect(getByText('Save')).toBeInTheDocument();
expect(getByText('Cancel')).toBeInTheDocument();
expect(getByTestId('testid-modal')).toBeInTheDocument();
});
});

it('test that when I click Save on the Edit Modal, an Edit action is carried out.', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);
uiStore.setMeInfo(user);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);
jest.spyOn(mainStore, 'getBountyIndexById').mockReturnValue(Promise.resolve(1234));
jest.spyOn(mainStore, 'saveBounty').mockResolvedValue();

await act(async () => {
const { getByText } = render(
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route path="/bounty/:bountyId" component={TicketModalPage} />
</MemoryRouter>
);

await waitFor(() => getByText('Edit'));
fireEvent.click(getByText('Edit'));
await waitFor(() => getByText('Edit Bounty'));
expect(getByText('Edit Bounty')).toBeInTheDocument();

expect(getByText('Save')).toBeInTheDocument();
fireEvent.click(getByText('Save'));

await waitFor(() => getByText('Edit'));

await waitFor(() => {
expect(mainStore.saveBounty).toHaveBeenCalled();
});
});
});

it('test that when I click on Cancel on the Edit Modal, it hides the Edit Modal and shows the Bounty Modal', async () => {
(useIsMobile as jest.Mock).mockReturnValue(false);
uiStore.setMeInfo(user);

jest.spyOn(mainStore, 'getBountyById').mockReturnValue(
Promise.resolve([
{
...newBounty,
person: { ...newBounty.person, owner_alias: user.alias },
body: {
...mockBountiesMutated[1].body,
owner: user
}
}
])
);
jest.spyOn(mainStore, 'getBountyIndexById').mockReturnValue(Promise.resolve(1234));

await act(async () => {
const { getByText, queryByText } = render(
<MemoryRouter initialEntries={['/bounty/1234']}>
<Route path="/bounty/:bountyId" component={TicketModalPage} />
</MemoryRouter>
);

await waitFor(() => getByText('Edit'));
fireEvent.click(getByText('Edit'));
await waitFor(() => getByText('Edit Bounty'));
fireEvent.click(getByText('Cancel'));
await waitFor(() => getByText('Edit'));
expect(queryByText('Edit Bounty')).not.toBeInTheDocument();
expect(queryByText('Edit')).toBeInTheDocument();
});
});
});

0 comments on commit 435b926

Please sign in to comment.