Skip to content

Commit 2879ae0

Browse files
committed
Fix Text component migration tests and add replacement of identifiers
1 parent b03edaa commit 2879ae0

File tree

9 files changed

+92
-5
lines changed

9 files changed

+92
-5
lines changed

.changeset/fresh-rats-pay.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@shopify/polaris-migrator': patch
3+
---
4+
5+
Add support to replace Identifiers along with JSXIdentifiers for Text migration

polaris-migrator/src/migrations/react-replace-text-components/steps/replace-display-text.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,11 @@ export function replaceDisplayText<NodeType = ASTNode>(
5757
insertJSXAttribute(j, element, 'as', 'p');
5858
}
5959
});
60+
61+
source
62+
.find(j.Identifier)
63+
.filter((path) => path.node.name === localElementName)
64+
.forEach((path) => {
65+
path.node.name = 'Text';
66+
});
6067
}

polaris-migrator/src/migrations/react-replace-text-components/steps/replace-other.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,12 @@ export function replaceOther<NodeType = ASTNode>(
7676
insertJSXAttribute(j, element, 'visuallyHidden');
7777
}
7878
});
79+
80+
source
81+
.find(j.Identifier)
82+
.filter((path) => path.node.name === localElementName)
83+
.forEach((path) => {
84+
path.node.name = 'Text';
85+
});
7986
});
8087
}

polaris-migrator/src/migrations/react-replace-text-components/steps/replace-text-style.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,11 @@ export function replaceTextStyle<NodeType = ASTNode>(
113113
insertJSXAttribute(j, element, 'as', 'span');
114114
}
115115
});
116+
117+
source
118+
.find(j.Identifier)
119+
.filter((path) => path.node.name === localElementName)
120+
.forEach((path) => {
121+
path.node.name = 'Text';
122+
});
116123
}

polaris-migrator/src/migrations/react-replace-text-components/tests/react-replace-text-components.input.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ import {
88
VisuallyHidden,
99
} from '@shopify/polaris';
1010

11+
const noop = (..._: any) => {};
12+
1113
export function App() {
14+
const textStyle = TextStyle;
15+
noop(textStyle);
16+
1217
return (
1318
<>
1419
<DisplayText size="extraLarge">Display text</DisplayText>

polaris-migrator/src/migrations/react-replace-text-components/tests/react-replace-text-components.output.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import React from 'react';
22
import {Text, InlineCode} from '@shopify/polaris';
33

4+
const noop = (..._: any) => {};
5+
46
export function App() {
7+
const textStyle = Text;
8+
noop(textStyle);
9+
510
return (
611
<>
712
<Text variant="heading4xl" as="p">
@@ -16,16 +21,16 @@ export function App() {
1621
<Text variant="headingLg" as="p">
1722
Display text
1823
</Text>
19-
<Text as="h1" variant="headingLg">
24+
<Text as="h1" variant="headingMd">
2025
Heading
2126
</Text>
22-
<Text variant="headingLg" as="h2">
27+
<Text variant="headingMd" as="h2">
2328
Heading
2429
</Text>
25-
<Text as="h2" variant="headingSm">
30+
<Text as="h2" variant="headingXs">
2631
Subheading
2732
</Text>
28-
<Text variant="headingSm" as="h3">
33+
<Text variant="headingXs" as="h3">
2934
Subheading
3035
</Text>
3136
<Text variant="bodySm" as="p">

polaris-migrator/src/migrations/react-replace-text-components/tests/react-replace-text-components.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import {check} from '../../../utilities/testUtils';
22

33
const migration = 'react-replace-text-components';
4-
const fixtures = ['with-relative'];
4+
const fixtures = [
5+
'react-replace-text-components',
6+
'with-identifier',
7+
'with-relative',
8+
];
59

610
for (const fixture of fixtures) {
711
check(__dirname, {
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import '@shopify/react-testing/matchers';
2+
3+
import React from 'react';
4+
import {
5+
DisplayText,
6+
Heading,
7+
Subheading,
8+
Caption,
9+
TextStyle,
10+
VisuallyHidden,
11+
} from '@shopify/polaris';
12+
13+
const mount = (..._: any) => {};
14+
const MyComponent = () => <div />;
15+
16+
describe('MyComponent', () => {
17+
it('renders', () => {
18+
const container = mount(<MyComponent />);
19+
20+
expect(container).not.toContainReactComponent(DisplayText);
21+
expect(container).not.toContainReactComponent(Heading);
22+
expect(container).not.toContainReactComponent(Subheading);
23+
expect(container).not.toContainReactComponent(Caption);
24+
expect(container).not.toContainReactComponent(TextStyle);
25+
expect(container).not.toContainReactComponent(VisuallyHidden);
26+
});
27+
});
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import '@shopify/react-testing/matchers';
2+
3+
import React from 'react';
4+
import {Text} from '@shopify/polaris';
5+
6+
const mount = (..._: any) => {};
7+
const MyComponent = () => <div />;
8+
9+
describe('MyComponent', () => {
10+
it('renders', () => {
11+
const container = mount(<MyComponent />);
12+
13+
expect(container).not.toContainReactComponent(Text);
14+
expect(container).not.toContainReactComponent(Text);
15+
expect(container).not.toContainReactComponent(Text);
16+
expect(container).not.toContainReactComponent(Text);
17+
expect(container).not.toContainReactComponent(Text);
18+
expect(container).not.toContainReactComponent(Text);
19+
});
20+
});

0 commit comments

Comments
 (0)