Skip to content
This repository was archived by the owner on Jun 14, 2019. It is now read-only.

Commit d83025e

Browse files
committed
Simplifies test ID generator function
1 parent a9a60ce commit d83025e

File tree

6 files changed

+18
-29
lines changed

6 files changed

+18
-29
lines changed

app/ui-react/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,7 @@ UI components that have user interaction should define a `data-testid` attribute
640640
641641
The `toTestId` utilitity function, which is found in the `testIdGenerator.ts` file in the `utils` folder of the `@syndesis/ui` package, should be used to generate **all** test identifiers. This function ensures the identifier is formatted correctly and only contains valid characters. It also provides a way to separate segments of the identifier if segments are desired.
642642
643-
The `toTestId` function accepts one or more strings and inserts 2 dash (`-`) characters between them. It is recommended to have the first string be the name of the component. Here is an example of how to use the `toTestId` function:
643+
The `toTestId` function accepts one or more strings and inserts a dash (`-`) character between them. It is recommended to have the first string be the name of the component. Here is an example of how to use the `toTestId` function:
644644
645645
```tsx
646646
export class ExtensionListItem extends React.Component<
@@ -657,7 +657,7 @@ export class ExtensionListItem extends React.Component<
657657
</Button>
658658
```
659659
660-
The above code produces this test ID for an extension with the name of "My Extension": `extensionlistitem--my-extension--delete-button`.
660+
The above code produces this test ID for an extension with the name of "My Extension": `extensionlistitem-my-extension-delete-button`.
661661
662662
#### Unit testing
663663

app/ui-react/packages/ui/src/utils/testIdGenerator.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,11 @@
11
/**
2-
* Generates an identifier suitable to use as a `data-testid`. Value arguments are separated by two
3-
* dash characters.
2+
* Generates an identifier suitable to use as a `data-testid`. Value arguments are separated by a
3+
* dash character.
44
* @param values the values used to generate a test identifier
55
* @returns a test identifier
66
*/
77
export function toTestId(...values: string[]): string {
8-
let testId = '';
9-
10-
for (let i = 0; i < values.length; i++) {
11-
testId += generateId(values[i]);
12-
13-
// add separator
14-
if (i < values.length - 1) {
15-
testId += '--';
16-
}
17-
}
18-
19-
return testId;
8+
return values.map(generateId).join('-');
209
}
2110

2211
/**

app/ui-react/packages/ui/tests/AggregatedMetricCard.spec.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,25 @@ export default describe('AggregatedMetricCard', () => {
99

1010
it('Should have the A Title title', function() {
1111
const { getByTestId } = render(testComponent);
12-
expect(getByTestId('aggregatedmetriccard--title')).toHaveTextContent(
12+
expect(getByTestId('aggregatedmetriccard-title')).toHaveTextContent(
1313
'A Title'
1414
);
1515
});
1616
it('Should have 5 errors', function() {
1717
const { getByTestId } = render(testComponent);
18-
expect(getByTestId('aggregatedmetriccard--error-count')).toHaveTextContent(
18+
expect(getByTestId('aggregatedmetriccard-error-count')).toHaveTextContent(
1919
'5'
2020
);
2121
});
2222
it('Should have 10 ok', function() {
2323
const { getByTestId } = render(testComponent);
24-
expect(getByTestId('aggregatedmetriccard--ok-count')).toHaveTextContent(
24+
expect(getByTestId('aggregatedmetriccard-ok-count')).toHaveTextContent(
2525
'10'
2626
);
2727
});
2828
it('Should have 15 total', function() {
2929
const { getByTestId } = render(testComponent);
30-
expect(getByTestId('aggregatedmetriccard--total-count')).toHaveTextContent(
30+
expect(getByTestId('aggregatedmetriccard-total-count')).toHaveTextContent(
3131
'15'
3232
);
3333
});

app/ui-react/packages/ui/tests/ConnectionCard.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export default describe('ConnectionCard', () => {
3232
it('Should have the Sample connection title', () => {
3333
const { getByTestId } = render(testComponent);
3434
expect(
35-
getByTestId('connectioncard--sample-connection--details-link')
35+
getByTestId('connectioncard-sample-connection-details-link')
3636
).toHaveTextContent('Sample connection');
3737
});
3838
});

app/ui-react/packages/ui/tests/IntegrationStatusDetail.spec.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,21 @@ export default describe('IntegrationStatusDetail', () => {
4141
it('Should show the starting state', () => {
4242
const { getByTestId } = render(testComponentPublishing);
4343
expect(
44-
getByTestId('integrationstatusdetail--status-detail')
44+
getByTestId('integrationstatusdetail-status-detail')
4545
).toHaveTextContent('Starting...');
4646
});
4747

4848
it('Should show the detailed status', () => {
4949
const { getByTestId } = render(testComponentPublishingDetailed);
5050
expect(
51-
getByTestId('integrationstatusdetail--status-detail')
51+
getByTestId('integrationstatusdetail-status-detail')
5252
).toHaveTextContent('');
5353
});
5454

5555
it('Should show the stopping state', () => {
5656
const { getByTestId } = render(testComponentUnpublishing);
5757
expect(
58-
getByTestId('integrationstatusdetail--status-detail')
58+
getByTestId('integrationstatusdetail-status-detail')
5959
).toHaveTextContent('Stopping...');
6060
});
6161
});

app/ui-react/packages/ui/tests/ProgressWithLink.spec.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ export default describe('IntegrationProgress', () => {
2626

2727
it('Should show the progress value and steps', () => {
2828
const { getByTestId } = render(testComponent);
29-
expect(getByTestId('progresswithlink--value')).toHaveTextContent(
29+
expect(getByTestId('progresswithlink-value')).toHaveTextContent(
3030
'Deploying ( 2 / 4 )'
3131
);
3232
});
3333
it('Should show the log link when supplied', () => {
3434
const { queryByTestId } = render(testComponent);
35-
expect(queryByTestId('progresswithlink--log-url')).toBeDefined();
36-
expect(queryByTestId('progresswithlink--log-url')).toHaveTextContent(
35+
expect(queryByTestId('progresswithlink-log-url')).toBeDefined();
36+
expect(queryByTestId('progresswithlink-log-url')).toHaveTextContent(
3737
'View Logs'
3838
);
3939
});
4040
it('Should show the progress value and steps', () => {
4141
const { getByTestId } = render(testComponentNoLog);
42-
expect(getByTestId('progresswithlink--value')).toHaveTextContent(
42+
expect(getByTestId('progresswithlink-value')).toHaveTextContent(
4343
'Assembling ( 1 / 4 )'
4444
);
4545
});
4646
it('Should not show the log link if not supplied', () => {
4747
const { queryByTestId } = render(testComponentNoLog);
48-
expect(queryByTestId('progresswithlink--log-url')).toEqual(null);
48+
expect(queryByTestId('progresswithlink-log-url')).toEqual(null);
4949
});
5050
});

0 commit comments

Comments
 (0)