Files
growstuff/app/javascript/components/AddJobModal.test.jsx
T
Brenda WallaceandDaniel O'Connor 75595350bc Easier recording of harvests from the front page (#5077)
* Implement AddSeedsPicker component and enhance planting selection UI; update styles and serializers for better seed management

* Enhance PlantingPicker component: add hints and titles for harvest and seed actions; update test to reflect changes in seed saving behavior

* Enhance harvest recording: allow harvesting from crops without plantings; update forms and modals for better user experience

* Refactor code structure for improved readability and maintainability

* Remove AddSeedsPicker component and its associated tests

* Add planting picker functionality for harvesting and seed saving

- Introduced PlantingPickersController to handle JSON responses for member's plantings.
- Updated HomeHelper to include crop ID in planting picker properties.
- Refactored PlantPicker and PlantSomethingModal components to support multiple gardens.
- Created PlantingFlow component to manage planting actions (harvest and save seeds).
- Implemented RecordDialog component to handle server-rendered links for harvesting and seed saving.
- Updated various views to include data attributes for triggering the record dialog.
- Added routes for planting picker and corresponding request specs for testing.
- Adjusted styles in _plantings.scss for better layout and spacing.

* Add job management features including job creation and wishlist functionality

* Remove WishlistJobsController and associated views and tests

* Add suggestion links rendering and fix routes formatting

* Refactor AddJobModal and JobPicker components to simplify props and enhance user experience

* Update seed addition test to check dialog presence and current path

* Apply suggestion from @CloCkWeRX

---------

Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>
2026-10-26 21:38:12 +10:30

78 lines
2.9 KiB
React

import {render, screen} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import {afterEach, describe, expect, it, vi} from 'vitest';
import {daysBefore, localToday} from '../dates';
import AddJobModal from './AddJobModal';
function stubFetch(response) {
const fetch = vi.fn(async () => ({ok: response.ok, status: response.status, json: async () => response.data || {}}));
vi.stubGlobal('fetch', fetch);
return fetch;
}
function setup(props = {}) {
const onSaved = vi.fn();
render(<AddJobModal onClose={vi.fn()} onSaved={onSaved} {...props} />);
return onSaved;
}
const body = (fetch) => JSON.parse(fetch.mock.calls[0][1].body).job;
describe('AddJobModal', () => {
afterEach(() => vi.unstubAllGlobals());
it('saves the job for today', async () => {
const fetch = stubFetch({ok: true, data: {}});
setup();
await userEvent.type(screen.getByLabelText('What needs doing?'), 'Build a trellis');
await userEvent.click(screen.getByRole('button', {name: 'Add job'}));
expect(body(fetch)).toMatchObject({name: 'Build a trellis', due_date: localToday()});
});
it('saves the job for tomorrow', async () => {
const fetch = stubFetch({ok: true, data: {}});
setup();
await userEvent.type(screen.getByLabelText('What needs doing?'), 'Water');
await userEvent.click(screen.getByLabelText('Tomorrow'));
await userEvent.click(screen.getByRole('button', {name: 'Add job'}));
expect(body(fetch).due_date).toBe(daysBefore(localToday(), -1));
});
it('hands the saved job on', async () => {
stubFetch({ok: true, data: {id: 7}});
const onSaved = setup();
await userEvent.type(screen.getByLabelText('What needs doing?'), 'Water');
await userEvent.click(screen.getByRole('button', {name: 'Add job'}));
expect(onSaved).toHaveBeenCalledWith({id: 7});
});
it('shows what the server rejected', async () => {
stubFetch({ok: false, status: 422, data: {errors: {name: ["can't be blank"]}}});
setup();
await userEvent.type(screen.getByLabelText('What needs doing?'), ' ');
await userEvent.click(screen.getByRole('button', {name: 'Add job'}));
expect(await screen.findByText("Name can't be blank")).toBeInTheDocument();
});
it('hides the notes until asked', () => {
setup();
expect(screen.queryByLabelText('Notes')).not.toBeInTheDocument();
});
it('saves a note', async () => {
const fetch = stubFetch({ok: true, data: {}});
setup();
await userEvent.type(screen.getByLabelText('What needs doing?'), 'Water');
await userEvent.click(screen.getByRole('button', {name: 'Add a note'}));
await userEvent.type(screen.getByLabelText('Notes'), 'Before noon');
await userEvent.click(screen.getByRole('button', {name: 'Add job'}));
expect(body(fetch).description).toBe('Before noon');
});
it('says jobs are public', () => {
setup();
expect(screen.getByText('Jobs are public')).toBeInTheDocument();
});
});