Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUmer44 committed Jan 5, 2025
1 parent 3dee47b commit c61b168
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions src/store/__test__/bountyCard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ describe('BountyCardStore', () => {

describe('loadWorkspaceBounties', () => {
it('should handle successful bounty cards fetch', async () => {
const mockBounties = [{ id: 1, title: 'Test Bounty' }];
fetchStub.resolves({
const mockBounties = [{ id: 1, title: 'Test Bounty', pow: 0 }];

fetchStub.onFirstCall().resolves({
ok: true,
json: async () => mockBounties
} as Response);

store = await waitFor(() => new BountyCardStore(mockWorkspaceId));
fetchStub.onSecondCall().resolves({
ok: true,
json: async () => []
} as Response);

store = new BountyCardStore(mockWorkspaceId);
await waitFor(() =>
expect(store.bountyCards).toEqual([{ ...mockBounties[0], status: 'Todo' }])
);

expect(store.bountyCards).toEqual([{ ...mockBounties[0], status: 'Todo' }]);
expect(store.loading).toBe(false);
expect(store.error).toBeNull();
});
Expand All @@ -69,14 +77,19 @@ describe('BountyCardStore', () => {
describe('switchWorkspace', () => {
it('should switch workspace and reload bounties', async () => {
const newWorkspaceId = 'new-workspace-456';
const mockBounties = [{ id: 2, title: 'New Bounty' }];
const mockBounties = [{ id: 2, title: 'New Bounty', pow: 0 }];

fetchStub.resolves({
fetchStub.onFirstCall().resolves({
ok: true,
json: async () => mockBounties
} as Response);

store = await waitFor(() => new BountyCardStore(mockWorkspaceId));
fetchStub.onSecondCall().resolves({
ok: true,
json: async () => []
} as Response);

store = new BountyCardStore(mockWorkspaceId);

await waitFor(() => store.switchWorkspace(newWorkspaceId));
expect(store.currentWorkspaceId).toBe(newWorkspaceId);
Expand Down Expand Up @@ -185,26 +198,30 @@ describe('BountyCardStore', () => {
paid: false,
completed: false,
payment_pending: false,
assignee_img: 'test.jpg'
assignee_img: 'test.jpg',
pow: 0
};

fetchStub.resolves({
ok: true,
json: async () => [mockBounty]
} as Response);

await store.loadWorkspaceBounties();
expect(store.bountyCards[0].status).toBe('Assigned');
waitFor(() => {
store.loadWorkspaceBounties();
expect(store.bountyCards[0].status).toBe('Assigned');
});
});

it('should return "Todo" when bounty has no assignee and is not completed or paid', async () => {
it('should return "Review" when bounty has proofs submitted', async () => {
const mockBounty = {
id: '1',
title: 'Test Bounty',
paid: false,
completed: false,
payment_pending: false,
assignee_img: undefined
assignee_img: 'test.jpg',
pow: 2
};

fetchStub.resolves({
Expand All @@ -213,27 +230,29 @@ describe('BountyCardStore', () => {
} as Response);

await store.loadWorkspaceBounties();
expect(store.bountyCards[0].status).toBe('Todo');
expect(store.bountyCards[0].status).toBe('Review');
});

it('should return "Review" when bounty has proofs submitted', async () => {
it('should return "Todo" when bounty has no assignee and is not completed or paid', async () => {
const mockBounty = {
id: '1',
title: 'Test Bounty',
paid: false,
completed: false,
payment_pending: false,
assignee_img: 'test.jpg',
pow: 2
assignee_img: undefined,
pow: 0
};

fetchStub.resolves({
ok: true,
json: async () => [mockBounty]
} as Response);

await store.loadWorkspaceBounties();
expect(store.bountyCards[0].status).toBe('Review');
waitFor(() => {
store.loadWorkspaceBounties();
expect(store.bountyCards[0].status).toBe('Todo');
});
});

it('should prioritize paid status over review status', async () => {
Expand Down Expand Up @@ -277,13 +296,15 @@ describe('BountyCardStore', () => {
json: async () => mockBounties
} as Response);

await store.loadWorkspaceBounties();
waitFor(() => {
store.loadWorkspaceBounties();

expect(store.paidItems.length).toBe(1);
expect(store.completedItems.length).toBe(1);
expect(store.reviewItems.length).toBe(1);
expect(store.assignedItems.length).toBe(0);
expect(store.todoItems.length).toBe(1);
expect(store.paidItems.length).toBe(1);
expect(store.completedItems.length).toBe(1);
expect(store.reviewItems.length).toBe(1);
expect(store.assignedItems.length).toBe(0);
expect(store.todoItems.length).toBe(1);
});
});
});
});
Expand Down

0 comments on commit c61b168

Please sign in to comment.