Skip to content

Commit

Permalink
fix: proposal details form test
Browse files Browse the repository at this point in the history
  • Loading branch information
fmorency committed Nov 7, 2024
1 parent 3071081 commit eaf76b3
Showing 1 changed file with 24 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
import { describe, test, afterEach, expect, jest } from 'bun:test';
import { describe, test, afterEach, expect, jest, mock } from 'bun:test';
import React from 'react';
import { screen, fireEvent, cleanup, waitFor } from '@testing-library/react';
import ProposalDetails from '@/components/groups/forms/proposals/ProposalDetailsForm';
import { renderWithChainProvider } from '@/tests/render';
import { mockProposalFormData } from '@/tests/mock';
import matchers from '@testing-library/jest-dom/matchers';

expect.extend(matchers);

// Mock next/router
const m = jest.fn();
mock.module('next/router', () => ({
useRouter: m.mockReturnValue({
query: {},
push: jest.fn(),
}),
}));

const mockProps = {
nextStep: jest.fn(),
Expand All @@ -20,7 +32,7 @@ describe('ProposalDetails Component', () => {

test('renders component with correct details', () => {
renderWithChainProvider(<ProposalDetails {...mockProps} />);
expect(screen.getByText('Proposal')).toBeDefined();
expect(screen.getByText('Proposal details')).toBeDefined();
expect(screen.getByText('Next: Proposal Messages')).toBeDefined();
});

Expand Down Expand Up @@ -57,10 +69,18 @@ describe('ProposalDetails Component', () => {
});
});

test('next button is disabled when form is invalid', () => {
test('next button is enabled when form is valid', () => {
renderWithChainProvider(<ProposalDetails {...mockProps} />);
const nextButton = screen.getByText('Next: Proposal Messages');
expect(nextButton).toBeEnabled();
});

test('next button is disabled when form is invalid', async () => {
renderWithChainProvider(<ProposalDetails {...mockProps} />);
const titleInput = screen.getByLabelText('Proposal Title');
fireEvent.change(titleInput, { target: { value: '' } });
const nextButton = screen.getByText('Next: Proposal Messages');
expect(nextButton).toBeDisabled();
await waitFor(() => expect(nextButton).toBeDisabled());
});

test('next button is enabled when form is valid and dirty', async () => {
Expand All @@ -80,17 +100,4 @@ describe('ProposalDetails Component', () => {
fireEvent.click(nextButton);
expect(mockProps.nextStep).toHaveBeenCalled();
});

test('updates proposers field with address when address button is clicked', async () => {
renderWithChainProvider(<ProposalDetails {...mockProps} />);
const addressButton = screen.getByLabelText('address-btn');
fireEvent.click(addressButton);
await waitFor(() => {
expect(mockProps.dispatch).toHaveBeenCalledWith({
type: 'UPDATE_FIELD',
field: 'proposers',
value: mockProps.address,
});
});
});
});

0 comments on commit eaf76b3

Please sign in to comment.