Skip to content

Commit 3760da3

Browse files
committed
CCM-10893 Update markdown content to use only test id and resolve test output error
1 parent 767a2b8 commit 3760da3

File tree

7 files changed

+30
-37
lines changed

7 files changed

+30
-37
lines changed

frontend/src/__tests__/components/forms/NhsAppTemplateForm/__snapshots__/NhsAppTemplateForm.test.tsx.snap

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ exports[`Client-side validation triggers 1`] = `
160160
class=""
161161
>
162162
<p
163-
id="character-count-0"
163+
data-testid="character-count-0"
164164
>
165165
16 of 5000 characters
166166
</p>
@@ -860,7 +860,7 @@ exports[`renders page 1`] = `
860860
class=""
861861
>
862862
<p
863-
id="character-count-0"
863+
data-testid="character-count-0"
864864
>
865865
16 of 5000 characters
866866
</p>
@@ -1600,7 +1600,7 @@ exports[`renders page one error 1`] = `
16001600
class=""
16011601
>
16021602
<p
1603-
id="character-count-0"
1603+
data-testid="character-count-0"
16041604
>
16051605
0 of 5000 characters
16061606
</p>
@@ -2354,7 +2354,7 @@ exports[`renders page with multiple errors 1`] = `
23542354
class=""
23552355
>
23562356
<p
2357-
id="character-count-0"
2357+
data-testid="character-count-0"
23582358
>
23592359
0 of 5000 characters
23602360
</p>
@@ -3054,7 +3054,7 @@ exports[`renders page with preloaded field values 1`] = `
30543054
class=""
30553055
>
30563056
<p
3057-
id="character-count-0"
3057+
data-testid="character-count-0"
30583058
>
30593059
16 of 5000 characters
30603060
</p>
@@ -3732,7 +3732,7 @@ exports[`renders page without back link for initial state with id - edit mode 1`
37323732
class=""
37333733
>
37343734
<p
3735-
id="character-count-0"
3735+
data-testid="character-count-0"
37363736
>
37373737
16 of 5000 characters
37383738
</p>

frontend/src/__tests__/components/molecules/MarkdownContent.test.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ describe('MarkdownContent', () => {
2424
expect(screen.getByRole('link')).toHaveTextContent('link');
2525
});
2626

27-
it('passes ID through if content is a string', () => {
28-
render(<MarkdownContent content='This is content' id='content-id' />);
27+
it('passes test ID through if content is a string', () => {
28+
render(<MarkdownContent content='This is content' testId='content-id' />);
2929
expect(screen.getByText('This is content')).toHaveAttribute(
30-
'id',
30+
'data-testid',
3131
'content-id-0'
3232
);
3333
});
@@ -45,19 +45,19 @@ describe('MarkdownContent', () => {
4545
expect(screen.getByRole('link')).toHaveTextContent('link');
4646
});
4747

48-
it('passes indexed IDs to each item if content is an array', () => {
48+
it('passes indexed test IDs to each item if content is an array', () => {
4949
render(
5050
<MarkdownContent
5151
content={['First paragraph', 'Second paragraph']}
52-
id='content-id'
52+
testId='content-id'
5353
/>
5454
);
5555

5656
const first = screen.getByText('First paragraph');
57-
expect(first).toHaveAttribute('id', 'content-id-0');
57+
expect(first).toHaveAttribute('data-testid', 'content-id-0');
5858

5959
const second = screen.getByText('Second paragraph');
60-
expect(second).toHaveAttribute('id', 'content-id-1');
60+
expect(second).toHaveAttribute('data-testid', 'content-id-1');
6161
});
6262

6363
it('adds correct attributes to links', () => {

frontend/src/components/forms/NhsAppTemplateForm/NhsAppTemplateForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export const NhsAppTemplateForm: FC<
127127
/>
128128
<JsEnabled>
129129
<MarkdownContent
130-
id='character-count'
130+
testId='character-count'
131131
content={characterCountText}
132132
variables={{ characters: nhsAppTemplateMessage.length }}
133133
/>

frontend/src/components/molecules/ContentRenderer/ContentRenderer.tsx

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import CodeExample from '@atoms/CodeExample/CodeExample';
22
import { MarkdownContent } from '@molecules/MarkdownContent/MarkdownContent';
33

4-
type StandardBlock = { id?: string; testId?: string };
4+
type StandardBlock = { testId?: string };
55
export type MarkdownTextBlock = StandardBlock & { type: 'text'; text: string };
66
export type CodeBlock = StandardBlock & {
77
type: 'code';
@@ -21,24 +21,21 @@ export function ContentRenderer({ content, variables }: ContentRendererProps) {
2121
return (
2222
<>
2323
{content.map((block, index) => {
24-
const key = block.id ?? index;
24+
const key = block.testId ?? index;
2525

2626
switch (block.type) {
27-
case 'text': {
27+
case 'text':
2828
return (
2929
<MarkdownContent
30-
id={block.id}
3130
testId={block.testId}
3231
key={key}
3332
content={block.text}
3433
variables={variables}
3534
/>
3635
);
37-
}
38-
case 'code': {
36+
case 'code':
3937
return (
4038
<CodeExample
41-
id={block.id}
4239
data-testid={block.testId}
4340
key={key}
4441
ariaText={block.aria?.text}
@@ -47,21 +44,18 @@ export function ContentRenderer({ content, variables }: ContentRendererProps) {
4744
{block.code}
4845
</CodeExample>
4946
);
50-
}
51-
case 'list': {
47+
case 'list':
5248
return (
53-
<ul id={block.id} data-testid={block.testId} key={key}>
49+
<ul data-testid={block.testId} key={key}>
5450
{block.items.map((item, itemId) => (
5551
<li key={itemId}>{item}</li>
5652
))}
5753
</ul>
5854
);
59-
}
60-
default: {
55+
default:
6156
console.error('Unsupported content block:', block);
6257

6358
throw new Error('Unsupported content block type');
64-
}
6559
}
6660
})}
6761
</>

frontend/src/components/molecules/MarkdownContent/MarkdownContent.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,26 @@ import React from 'react';
55
type MarkdownContentProps = {
66
content: string | string[];
77
variables?: Record<string, string | number>;
8-
id?: string;
98
testId?: string;
109
};
1110

1211
export function MarkdownContent({
1312
content,
1413
variables,
15-
id,
1614
testId,
1715
}: MarkdownContentProps) {
1816
const items = Array.isArray(content) ? content : [content];
17+
const rendered = items
18+
.map((item) => interpolate(item, variables))
19+
.filter((s) => s.trim().length > 0);
20+
21+
if (rendered.length === 0) return null;
1922

2023
return (
2124
<>
22-
{items.map((item, index) => (
25+
{rendered.map((item, index) => (
2326
<Markdown
2427
key={index}
25-
id={id ? `${id}-${index}` : undefined}
2628
data-testid={testId ? `${testId}-${index}` : undefined}
2729
options={{
2830
forceBlock: true,
@@ -31,10 +33,7 @@ export function MarkdownContent({
3133
overrides: {
3234
a: {
3335
component: 'a',
34-
props: {
35-
rel: 'noopener noreferrer',
36-
target: '_blank',
37-
},
36+
props: { rel: 'noopener noreferrer', target: '_blank' },
3837
},
3938
},
4039
}}

tests/test-team/pages/nhs-app/template-mgmt-create-nhs-app-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export class TemplateMgmtCreateNhsAppPage extends TemplateMgmtBasePageNonDynamic
3434
this.namingYourTemplate = page.locator(
3535
'[data-testid="how-to-name-your-template-details"]'
3636
);
37-
this.characterCountText = page.locator('[id="character-count-0"]');
37+
this.characterCountText = page.getByTestId('character-message-count-0');
3838
this.goBackLink = page
3939
.locator('.nhsuk-back-link__link')
4040
.and(page.getByText('Back to choose a template type'));

tests/test-team/pages/nhs-app/template-mgmt-edit-nhs-app-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export class TemplateMgmtEditNhsAppPage extends TemplateMgmtBasePageDynamic {
3232
this.namingYourTemplate = page.locator(
3333
'[data-testid="how-to-name-your-template-details"]'
3434
);
35-
this.characterCountText = page.locator('[id="character-count-0"]');
35+
this.characterCountText = page.getByTestId('character-message-count-0');
3636
this.messageFormatting = new TemplateMgmtMessageFormatting(page);
3737

3838
this.saveAndPreviewButton = page.locator(

0 commit comments

Comments
 (0)