Skip to content

Commit

Permalink
chore: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wa0x6e committed Nov 28, 2023
1 parent e354bd8 commit 159f02d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export async function getSubscribers(space) {
return subscriptions.map(subscription => subscription.address);
}

export async function getProposal(id): Promise<Record<string, any> | null> {
let proposal: { [key: string]: any } | null = null;
export async function getProposal(id: string): Promise<Record<string, any> | null> {
const query = {
proposal: {
__args: {
Expand Down Expand Up @@ -62,8 +61,7 @@ export async function getProposal(id): Promise<Record<string, any> | null> {
if (result.errors) {
console.error(`[events] Errors in subgraph request for proposal id: ${id}`);
}
proposal = result.proposal?.flagged ? null : result.proposal || null;
return proposal;
return result.proposal?.flagged ? null : result.proposal || null;
} catch (e: any) {
capture(e, { contexts: { input: { query, id } } });
return null;
Expand Down
41 changes: 38 additions & 3 deletions test/unit/helpers/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,42 @@
import { getProposal } from '../../../src/helpers/utils';

const PROPOSAL = { id: '0x0', flagged: false };

const mockSnapshotUtilsSubgraphRequest = jest.fn((): any => {
return {};
});
jest.mock('@snapshot-labs/snapshot.js', () => {
const originalModule = jest.requireActual('@snapshot-labs/snapshot.js');

return {
...originalModule,
utils: {
...originalModule.utils,
subgraphRequest: () => mockSnapshotUtilsSubgraphRequest()
}
};
});

describe('getProposal()', () => {
it.todo('returns null when the proposal is flagged');
it.todo('returns null when the proposal does not exist');
it.todo('returns the proposal');
it('returns null when the proposal is flagged', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({
proposal: { ...PROPOSAL, flagged: true }
});
expect(getProposal('')).resolves.toBeNull();
});

it('returns null when the proposal does not exist', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({ proposal: null });
expect(getProposal('')).resolves.toBeNull();
});

it('returns null on missing response', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({});
expect(getProposal('')).resolves.toBeNull();
});

it('returns the proposal', () => {
mockSnapshotUtilsSubgraphRequest.mockResolvedValueOnce({ proposal: PROPOSAL });
expect(getProposal('')).resolves.toEqual(PROPOSAL);
});
});

0 comments on commit 159f02d

Please sign in to comment.