|
| 1 | +import React from 'react'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { IntlProvider } from 'react-intl'; |
| 4 | +import { vi } from 'vitest'; |
| 5 | + |
| 6 | +import EventForm from '../EventForm'; |
| 7 | + |
| 8 | +vi.mock('../../../store/hooks', () => ({ |
| 9 | + useAppSelector: (selector: unknown) => { |
| 10 | + if (typeof selector === 'function') { |
| 11 | + return selector({ |
| 12 | + geo: { |
| 13 | + addressCoordinates: null, |
| 14 | + selectedAddress: null, |
| 15 | + selectedContractZone: { id: 1 }, |
| 16 | + unavailableDates: [], |
| 17 | + }, |
| 18 | + }); |
| 19 | + } |
| 20 | + |
| 21 | + return undefined; |
| 22 | + }, |
| 23 | +})); |
| 24 | + |
| 25 | +vi.mock('../fields/Input', () => ({ |
| 26 | + default: (props: Record<string, unknown>) => ( |
| 27 | + <div data-testid={String(props.id)} /> |
| 28 | + ), |
| 29 | +})); |
| 30 | + |
| 31 | +vi.mock('../fields/NumericInput', () => ({ |
| 32 | + default: (props: Record<string, unknown>) => ( |
| 33 | + <div data-testid={String(props.id)} /> |
| 34 | + ), |
| 35 | +})); |
| 36 | + |
| 37 | +vi.mock('../partitions/DateRange', () => ({ |
| 38 | + default: () => <div data-testid="date-range" />, |
| 39 | +})); |
| 40 | + |
| 41 | +vi.mock('../partitions/Location', () => ({ |
| 42 | + default: () => <div data-testid="location" />, |
| 43 | +})); |
| 44 | + |
| 45 | +const renderComponent = (additionalInformation: string) => |
| 46 | + render( |
| 47 | + <IntlProvider |
| 48 | + locale="fi" |
| 49 | + messages={{ |
| 50 | + 'form.event.title.name_and_description': 'Tapahtuman perustiedot', |
| 51 | + 'form.event.title.time': 'Aika', |
| 52 | + 'form.event.title.contact_person': 'Yhteyshenkilö', |
| 53 | + 'form.event.title.tools_and_suplies': 'Työkalut ja tarvikkeet', |
| 54 | + 'form.event.subtitle.time': |
| 55 | + 'Tapahtuman alkamispäivä voi olla tästä päivästä viikko eteenpäin.', |
| 56 | + 'form.event.subtitle.contact_person': |
| 57 | + 'Järjestäjän yhteystiedot tarvitaan urakoitsijoita varten.', |
| 58 | + 'form.event.subtitle.tools_and_suplies': |
| 59 | + 'Ilmoita tarvittavat työkalut. Urakoitsijat hoitavat oikean määrän tarvikkeita paikan päälle.', |
| 60 | + }} |
| 61 | + > |
| 62 | + <EventForm |
| 63 | + errors={{} as never} |
| 64 | + handleBlur={vi.fn()} |
| 65 | + handleChange={vi.fn()} |
| 66 | + handleSubmit={vi.fn()} |
| 67 | + touched={{} as never} |
| 68 | + values={ |
| 69 | + { |
| 70 | + name: '', |
| 71 | + description: '', |
| 72 | + estimated_attendee_count: undefined, |
| 73 | + targets: '', |
| 74 | + location: undefined, |
| 75 | + organizer_first_name: '', |
| 76 | + organizer_last_name: '', |
| 77 | + organizer_email: '', |
| 78 | + organizer_phone: '', |
| 79 | + large_trash_bag_count: undefined, |
| 80 | + small_trash_bag_count: undefined, |
| 81 | + trash_picker_count: undefined, |
| 82 | + maintenance_location: '', |
| 83 | + additional_information: additionalInformation, |
| 84 | + start_time: '', |
| 85 | + end_time: '', |
| 86 | + } as never |
| 87 | + } |
| 88 | + /> |
| 89 | + </IntlProvider> |
| 90 | + ); |
| 91 | + |
| 92 | +describe('<EventForm />', () => { |
| 93 | + it('renders additional information as a printable field when populated', () => { |
| 94 | + const { container } = renderComponent('Muita lisätietoja toimitukselle'); |
| 95 | + |
| 96 | + const additionalInformation = container.querySelector( |
| 97 | + '[data-testid="additional_information"]' |
| 98 | + ); |
| 99 | + |
| 100 | + expect(additionalInformation).toBeInTheDocument(); |
| 101 | + expect( |
| 102 | + additionalInformation?.closest('.event-form-print-note') |
| 103 | + ).toBeInTheDocument(); |
| 104 | + expect( |
| 105 | + additionalInformation?.closest('.event-form-print-skip') |
| 106 | + ).not.toBeInTheDocument(); |
| 107 | + expect( |
| 108 | + additionalInformation?.closest('.event-form-print-note--empty') |
| 109 | + ).not.toBeInTheDocument(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('hides empty additional information from the printable layout', () => { |
| 113 | + const { container } = renderComponent(''); |
| 114 | + |
| 115 | + expect( |
| 116 | + container.querySelector('[data-testid="additional_information"]') |
| 117 | + ).toBeInTheDocument(); |
| 118 | + expect( |
| 119 | + container.querySelector('.event-form-print-note--empty') |
| 120 | + ).toBeInTheDocument(); |
| 121 | + }); |
| 122 | +}); |
0 commit comments