Skip to content

Commit

Permalink
Merge remote-tracking branch 'clearcontracts/main' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Nov 17, 2024
2 parents 484b8c6 + cb91543 commit f6320fc
Show file tree
Hide file tree
Showing 134 changed files with 4,721 additions and 1,082 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ This voting app will be hosted through the end of February, to provide ample tim
4. Download [PostgreSQL](https://www.postgresql.org/) in your preferred method and initiate it (I personally use the [Postgress.app](https://postgresapp.com/downloads.html) method)
5. Create database in local PostgreSQL instance using [psql](https://www.postgresql.org/docs/current/app-psql.html), [pgAdmin](https://www.pgadmin.org/download/), or whatever your preferred method for interacting with a PostgreSQL database is
6. Adjust DATABASE_URL with your local PostgreSQL instance credentials. Additional info on Prisma connection URL can be [here](https://www.prisma.io/docs/orm/overview/databases/postgresql#connection-url)
7. Run `npx prisma migrate dev` to populate the database with the proper tables
7. Run `npx prisma migrate dev --skip-seed` to populate the database with the proper tables
8. Run `npx prisma generate` in root of repository to create TS client for Prisma
9. In the `prisma/seed.ts` file, adjust the seed data so that there is a user with the stake address that you will be connecting with
10. Run `npm run seed` to seed the database with testing data located in `prisma/seed.ts`
Expand Down
28 changes: 28 additions & 0 deletions __mocks__/downloadPollVotes/errorHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { http, HttpResponse } from 'msw';

export const downloadPollVotesPollIdNotValidHandler = [
http.post('/api/downloadPollVotes', async () => {
return HttpResponse.json(
{ success: false, message: 'Missing pollId' },
{ status: 400 },
);
}),
];

export const downloadPollVotesInternalErrorHandler = [
http.post('/api/downloadPollVotes', async () => {
return HttpResponse.json(
{ success: false, message: 'Failed to create CSV file' },
{ status: 500 },
);
}),
];

export const downloadPollVotesNotConcludedHandler = [
http.post('/api/downloadPollVotes', async () => {
return HttpResponse.json(
{ success: false, message: 'Poll is not concluded' },
{ status: 500 },
);
}),
];
9 changes: 9 additions & 0 deletions __mocks__/downloadPollVotes/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { http, HttpResponse } from 'msw';

export const downloadPollVotesHandlers = [
http.post('/api/downloadPollVotes', () => {
return HttpResponse.json({
status: 200,
});
}),
];
19 changes: 19 additions & 0 deletions __mocks__/downloadUserVotes/errorHandlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { http, HttpResponse } from 'msw';

export const downloadUserVotesPollIdNotValidHandler = [
http.post('/api/downloadUserVotes', async () => {
return HttpResponse.json(
{ success: false, message: 'Missing userId' },
{ status: 400 },
);
}),
];

export const downloadUserVotesInternalErrorHandler = [
http.post('/api/downloadUserVotes', async () => {
return HttpResponse.json(
{ success: false, message: 'Failed to create CSV file' },
{ status: 500 },
);
}),
];
10 changes: 10 additions & 0 deletions __mocks__/downloadUserVotes/handlers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { http, HttpResponse } from 'msw';

// TODO: Figure out how to properly mock the blob response
export const downloadUserVotesHandlers = [
http.post('/api/downloadUserVotes', () => {
return HttpResponse.json({
status: 200,
});
}),
];
12 changes: 12 additions & 0 deletions __mocks__/newPollVote/errorHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ export const newPollVoteNotVotingHandler = [
}),
];

export const pollIsArchivedHandler = [
http.post('/api/newPollVote', async () => {
return HttpResponse.json(
{
success: false,
message: 'Poll is archived',
},
{ status: 400 },
);
}),
];

export const newPollVoteInternalErrorHandler = [
http.post('/api/newPollVote', async () => {
return HttpResponse.json(
Expand Down
4 changes: 4 additions & 0 deletions __mocks__/server.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { deletePollHandlers } from '@/../__mocks__/deletePoll/handlers';
import { downloadPollVotesHandlers } from '@/../__mocks__/downloadPollVotes/handlers';
import { downloadUserVotesHandlers } from '@/../__mocks__/downloadUserVotes/handlers';
import { endVotingHandlers } from '@/../__mocks__/endVoting/handlers';
import { getPollHandlers } from '@/../__mocks__/getPoll/handlers';
import { getPollResultsHandlers } from '@/../__mocks__/getPollResults/handlers';
Expand Down Expand Up @@ -33,4 +35,6 @@ export const server = setupServer(
...updateActiveVoterHandlers,
...updateUserHandlers,
...deletePollHandlers,
...downloadPollVotesHandlers,
...downloadUserVotesHandlers,
);
13 changes: 13 additions & 0 deletions __mocks__/updateActiveVoter/errorHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { http, HttpResponse } from 'msw';

export const updateActiveVoterActiveVoteHandler = [
http.post('/api/updateActiveVoter', async () => {
return HttpResponse.json(
{
userId: '-1',
message:
'You cannot change the active voter while a Poll is actively voting.',
},
{ status: 400 },
);
}),
];

export const updateActiveVoterNoWorkshopIdHandler = [
http.post('/api/updateActiveVoter', async () => {
return HttpResponse.json(
Expand Down
13 changes: 13 additions & 0 deletions __mocks__/updateUser/errorHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { http, HttpResponse } from 'msw';

export const updateUserActiveVoteHandler = [
http.post('/api/updateUser', async () => {
return HttpResponse.json(
{
pollId: BigInt(-1).toString(),
message:
'You cannot update user information while a Poll is actively voting.',
},
{ status: 400 },
);
}),
];

export const updateUserNoNameHandler = [
http.post('/api/updateUser', async () => {
return HttpResponse.json(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { downloadPollVotesInternalErrorHandler } from '@/../__mocks__/downloadPollVotes/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadPollVotesButton } from '@/components/buttons/downloadPollVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadPollVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when internal error', async () => {
server.use(...downloadPollVotesInternalErrorHandler);
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadPollVotesButton pollId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/Failed to create CSV file/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { downloadPollVotesPollIdNotValidHandler } from '@/../__mocks__/downloadPollVotes/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadPollVotesButton } from '@/components/buttons/downloadPollVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadPollVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when no poll ID', async () => {
server.use(...downloadPollVotesPollIdNotValidHandler);
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadPollVotesButton pollId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download Votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/Missing pollId/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { downloadPollVotesNotConcludedHandler } from '@/../__mocks__/downloadPollVotes/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadPollVotesButton } from '@/components/buttons/downloadPollVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadPollVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when poll is not concluded', async () => {
server.use(...downloadPollVotesNotConcludedHandler);
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadPollVotesButton pollId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download Votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/'Poll is not concluded'/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadPollVotesButton } from '@/components/buttons/downloadPollVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadPollVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when successful', async () => {
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadPollVotesButton pollId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/Poll votes downloaded/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { downloadUserVotesInternalErrorHandler } from '@/../__mocks__/downloadUserVotes/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadUserVotesButton } from '@/components/buttons/downloadUserVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadUserVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when internal error', async () => {
server.use(...downloadUserVotesInternalErrorHandler);
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadUserVotesButton userId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/Failed to create CSV file/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { downloadUserVotesPollIdNotValidHandler } from '@/../__mocks__/downloadUserVotes/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadUserVotesButton } from '@/components/buttons/downloadUserVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadUserVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when no poll ID', async () => {
server.use(...downloadUserVotesPollIdNotValidHandler);
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadUserVotesButton userId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download Votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/Missing userId/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { DownloadUserVotesButton } from '@/components/buttons/downloadUserVotesButton';

// This mock environment has difficulty simulating blob responses which is what the downloadUserVotes function returns.
// This test is skipped because it will not work in the current environment. This will need to rely on E2E tests.
test.skip('alerts user when successful', async () => {
const user = userEvent.setup();
render(
<>
<Toaster />
<DownloadUserVotesButton userId="1" />
</>,
);

const button = screen.getByRole('button', {
name: /Download votes/i,
});
expect(button).toBeDefined();
await user.click(button);
const toast = await screen.findByText(/User votes downloaded/i);
expect(toast).toBeDefined();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { pollIsArchivedHandler } from '@/../__mocks__/newPollVote/errorHandlers';
import { server } from '@/../__mocks__/server';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { SessionProvider } from 'next-auth/react';
import { Toaster } from 'react-hot-toast';
import { expect, test } from 'vitest';

import { VoteOnPollButtons } from '@/components/buttons/voteOnPollButtons';

test.skip('alerts user when poll is archived', async () => {
server.use(...pollIsArchivedHandler);
const user = userEvent.setup();
render(
<SessionProvider
session={{
expires: '1',
user: {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
},
}}
>
<Toaster />
<VoteOnPollButtons
pollId="1"
disabled={false}
setDisabled={() => {}}
isActiveVoter={true}
/>
</SessionProvider>,
);

const yesVoteButton = screen.getByRole('button', {
name: /yes/i,
});
expect(yesVoteButton).toBeDefined();
await user.click(yesVoteButton);
const toast = await screen.findByText(/Poll is archived/i);
expect(toast).toBeDefined();
});
Loading

0 comments on commit f6320fc

Please sign in to comment.