|
| 1 | +import React from 'react'; |
| 2 | +import { render, screen } from '@testing-library/react'; |
| 3 | +import { MessagePlanBlock } from '@molecules/MessagePlanBlock/MessagePlanBlock'; |
| 4 | +import type { CascadeItem, Channel } from 'nhs-notify-backend-client'; |
| 5 | +import type { TemplateDto } from 'nhs-notify-backend-client'; |
| 6 | +import { EMAIL_TEMPLATE } from '@testhelpers'; |
| 7 | + |
| 8 | +function buildCascadeItem(channel: Channel): CascadeItem { |
| 9 | + return { |
| 10 | + cascadeGroups: [], |
| 11 | + channel, |
| 12 | + channelType: 'primary', |
| 13 | + defaultTemplateId: '', |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +const mockTemplate: TemplateDto = { |
| 18 | + ...EMAIL_TEMPLATE, |
| 19 | + name: 'Test email template', |
| 20 | +}; |
| 21 | + |
| 22 | +describe('MessagePlanBlock', () => { |
| 23 | + it('should render the step number and the heading for the first cascade item', () => { |
| 24 | + const channelItem = buildCascadeItem('EMAIL'); |
| 25 | + |
| 26 | + const { container } = render( |
| 27 | + <MessagePlanBlock index={0} channelItem={channelItem} /> |
| 28 | + ); |
| 29 | + |
| 30 | + const stepNumber = container.querySelector('.message-plan-block-number'); |
| 31 | + expect(stepNumber).toHaveTextContent('1'); |
| 32 | + |
| 33 | + expect( |
| 34 | + screen.getByRole('heading', { level: 2, name: 'First message' }) |
| 35 | + ).toBeInTheDocument(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should render the step number and the interpolated heading for a third item', () => { |
| 39 | + const channelItem = buildCascadeItem('NHSAPP'); |
| 40 | + |
| 41 | + const { container } = render( |
| 42 | + <MessagePlanBlock index={2} channelItem={channelItem} /> |
| 43 | + ); |
| 44 | + |
| 45 | + const stepNumber = container.querySelector('.message-plan-block-number'); |
| 46 | + expect(stepNumber).toHaveTextContent('3'); |
| 47 | + expect( |
| 48 | + screen.getByRole('heading', { level: 2, name: 'Third message' }) |
| 49 | + ).toBeInTheDocument(); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should render the channel template section with the correct channel subheading', () => { |
| 53 | + const channelItem = buildCascadeItem('EMAIL'); |
| 54 | + |
| 55 | + render(<MessagePlanBlock index={0} channelItem={channelItem} />); |
| 56 | + |
| 57 | + expect( |
| 58 | + screen.getByRole('heading', { level: 3, name: 'Email' }) |
| 59 | + ).toBeInTheDocument(); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('when the channel has a template', () => { |
| 63 | + it('should display the template name', () => { |
| 64 | + const channelItem = buildCascadeItem('EMAIL'); |
| 65 | + |
| 66 | + render( |
| 67 | + <MessagePlanBlock |
| 68 | + index={0} |
| 69 | + channelItem={channelItem} |
| 70 | + template={mockTemplate} |
| 71 | + /> |
| 72 | + ); |
| 73 | + expect(screen.getByText('Test email template')).toBeInTheDocument(); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should show Change/Remove links (and no Choose link)', () => { |
| 77 | + const channelItem = buildCascadeItem('EMAIL'); |
| 78 | + |
| 79 | + render( |
| 80 | + <MessagePlanBlock |
| 81 | + index={0} |
| 82 | + channelItem={channelItem} |
| 83 | + template={mockTemplate} |
| 84 | + /> |
| 85 | + ); |
| 86 | + |
| 87 | + expect( |
| 88 | + screen.getByRole('link', { name: 'Change template' }) |
| 89 | + ).toBeInTheDocument(); |
| 90 | + expect( |
| 91 | + screen.getByRole('link', { name: 'Remove template' }) |
| 92 | + ).toBeInTheDocument(); |
| 93 | + expect( |
| 94 | + screen.queryByRole('link', { name: 'Choose template' }) |
| 95 | + ).not.toBeInTheDocument(); |
| 96 | + }); |
| 97 | + }); |
| 98 | + |
| 99 | + describe('when the channel does not have a template', () => { |
| 100 | + it('should show Choose link (and no Change/Remove links)', () => { |
| 101 | + const channelItem = buildCascadeItem('SMS'); |
| 102 | + |
| 103 | + render(<MessagePlanBlock index={0} channelItem={channelItem} />); |
| 104 | + |
| 105 | + expect( |
| 106 | + screen.getByRole('link', { name: 'Choose template' }) |
| 107 | + ).toBeInTheDocument(); |
| 108 | + expect( |
| 109 | + screen.queryByRole('link', { name: 'Change template' }) |
| 110 | + ).not.toBeInTheDocument(); |
| 111 | + expect( |
| 112 | + screen.queryByRole('link', { name: 'Remove template' }) |
| 113 | + ).not.toBeInTheDocument(); |
| 114 | + }); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should render children inside a nested list when provided', () => { |
| 118 | + const channelItem = buildCascadeItem('LETTER'); |
| 119 | + |
| 120 | + const { container } = render( |
| 121 | + <MessagePlanBlock index={1} channelItem={channelItem}> |
| 122 | + <li>Child one</li> |
| 123 | + <li>Child two</li> |
| 124 | + </MessagePlanBlock> |
| 125 | + ); |
| 126 | + |
| 127 | + const nestedList = container.querySelector('li ul'); |
| 128 | + expect(nestedList).toBeInTheDocument(); |
| 129 | + |
| 130 | + const nestedListItems = nestedList!.querySelectorAll('li'); |
| 131 | + expect(nestedListItems.length).toBe(2); |
| 132 | + expect(nestedListItems[0]).toHaveTextContent('Child one'); |
| 133 | + expect(nestedListItems[1]).toHaveTextContent('Child two'); |
| 134 | + }); |
| 135 | + |
| 136 | + describe.each(['NHSAPP', 'EMAIL', 'SMS', 'LETTER'] as const)( |
| 137 | + 'for channel %s with template', |
| 138 | + (channel) => { |
| 139 | + it('should match snapshot', async () => { |
| 140 | + const channelItem = buildCascadeItem(channel); |
| 141 | + const { asFragment } = render( |
| 142 | + <MessagePlanBlock |
| 143 | + index={0} |
| 144 | + channelItem={channelItem} |
| 145 | + template={mockTemplate} |
| 146 | + /> |
| 147 | + ); |
| 148 | + expect(asFragment()).toMatchSnapshot(); |
| 149 | + }); |
| 150 | + } |
| 151 | + ); |
| 152 | + |
| 153 | + describe.each(['NHSAPP', 'EMAIL', 'SMS', 'LETTER'] as const)( |
| 154 | + 'for channel %s with no template', |
| 155 | + (channel) => { |
| 156 | + it('should match snapshot', async () => { |
| 157 | + const channelItem = buildCascadeItem(channel); |
| 158 | + const { asFragment } = render( |
| 159 | + <MessagePlanBlock index={0} channelItem={channelItem} /> |
| 160 | + ); |
| 161 | + expect(asFragment()).toMatchSnapshot(); |
| 162 | + }); |
| 163 | + } |
| 164 | + ); |
| 165 | +}); |
0 commit comments