Skip to content

Instantly share code, notes, and snippets.

@MaxMoskalenko
Created September 20, 2022 10:26
Show Gist options
  • Save MaxMoskalenko/04ce6c1108d2bfc7b7804fb558e1b87e to your computer and use it in GitHub Desktop.
Save MaxMoskalenko/04ce6c1108d2bfc7b7804fb558e1b87e to your computer and use it in GitHub Desktop.
import { NextApiRequest, NextApiResponse } from 'next';
import { Body, createMocks, Headers, RequestMethod } from 'node-mocks-http';
import handler from 'pages/api/v2/private/bounties/claim';
jest.mock(
'jose',
jest.fn(() => ({ jwtVerify: jest.fn() })),
);
jest.mock('helpers/getUserFromHeaders');
jest.mock('helpers/getUserFromDatabase');
jest.mock('helpers/getBountyAllTransactionObject');
const jose = require('jose');
const { getUserFromHeaders } = require('helpers/getUserFromHeaders');
const { getUserFromDatabase } = require('helpers/getUserFromDatabase');
const jwtVerifyMock = jest.fn(() => ({ payload: { sub: 4 } }));
jose.jwtVerify.mockImplementation(jwtVerifyMock);
const getUserFromHeadersMock = jest.fn(() => ({ sub: 'uid-123' }));
getUserFromHeaders.mockImplementation(getUserFromHeadersMock);
const getUserFromDatabaseMock = jest.fn(() => ({ email: 'test@mail.com', wallet: '0xwallet' }));
getUserFromDatabase.mockImplementation(getUserFromDatabaseMock);
jest.mock('ethers');
jest.mock(
'prismicio',
jest.fn(() => ({ createClient: jest.fn() })),
);
jest.mock(
'helpers/withAssociatedWallet',
jest.fn(() => ({
withAssociatedWallet: jest.fn((handler: (req: NextApiRequest, res: NextApiResponse) => any) => handler)
}))
);
// jest.mock('helpers/withBountyValidation');
// jest.mock('helpers/withErrorHandling');
// import { withAssociatedWallet } from 'helpers/withAssociatedWallet';
// import { withBountyValidation } from 'helpers/withBountyValidation';
// import { withErrorHandling } from 'helpers/withErrorHandling';
// (withAssociatedWallet as any).mockImplementation(
// jest.fn((handler: (req: NextApiRequest, res: NextApiResponse) => any) => handler),
// );
// (withBountyValidation as any).mockImplementation((handler: (req: NextApiRequest, res: NextApiResponse) => any) => {
// return async (req: NextApiRequest, res: NextApiResponse) => {
// await handler(req, res);
// };
// });
// (withErrorHandling as any).mockImplementation((handler: (req: NextApiRequest, res: NextApiResponse) => any) => {
// return async (req: NextApiRequest, res: NextApiResponse) => {
// await handler(req, res);
// };
// });
function mockRequestResponse(method: RequestMethod = 'GET', headers: Headers = {}, body: Body = {}) {
const { req, res }: any = createMocks({ method, headers, body });
return { req, res };
}
describe('/api/v2/private/bounties/all API Endpoint', () => {
it('no accociated wallet', async () => {
const { req, res } = mockRequestResponse('GET', { authorization: 'Bearer auth.token' });
await handler(req as any, res as any);
// console.log(res._getJSONData());
// expect('error' in res._getJSONData()).toEqual(true);
// expect(res._getJSONData().error).toEqual('User should have connected MetaMask account');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment