Skip to content

Commit 87b2931

Browse files
committed
fix(tabs): Fix an issue in migrations
1 parent c7c6e8f commit 87b2931

File tree

2 files changed

+77
-12
lines changed

2 files changed

+77
-12
lines changed

projects/igniteui-angular/migrations/update-12_0_0/index.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,62 @@ ${noteText}
140140
</igx-tabs>`);
141141
});
142142

143+
it('Should not create igx-[tab|botton-nav]-content if it\'s already present', async () => {
144+
appTree.create(
145+
'/testSrc/appPrefix/component/custom.component.html', `
146+
<igx-tabs #tabs1>
147+
<igx-tab-item>
148+
<igx-tab-header>
149+
<span igxTabHeaderLabel>Home</span>
150+
</igx-tab-header>
151+
<igx-tab-content>Home content.</igx-tab-content>
152+
</igx-tab-item>
153+
</igx-tabs>
154+
<!--BottomNav-->
155+
<igx-bottom-nav>
156+
<igx-bottom-nav-item>
157+
<igx-bottom-nav-header>
158+
<igx-icon igxBottomNavHeaderIcon>library_music</igx-icon>
159+
<span igxBottomNavHeaderLabel>Songs</span>
160+
</igx-bottom-nav-header>
161+
<igx-bottom-nav-content>
162+
<div class="item" *ngFor="let song of songsList">
163+
<span class="item-line1">{{song.title}}</span><br/>
164+
<span class="item-line2">{{song.artist}}</span>
165+
</div>
166+
</igx-bottom-nav-content>
167+
</igx-bottom-nav-item>
168+
</igx-bottom-nav>`);
169+
const tree = await schematicRunner.runSchematicAsync('migration-20', {}, appTree)
170+
.toPromise();
171+
172+
expect(tree.readContent('/testSrc/appPrefix/component/custom.component.html'))
173+
.toEqual(`
174+
<igx-tabs #tabs1>
175+
<igx-tab-item>
176+
<igx-tab-header>
177+
<span igxTabHeaderLabel>Home</span>
178+
</igx-tab-header>
179+
<igx-tab-content>Home content.</igx-tab-content>
180+
</igx-tab-item>
181+
</igx-tabs>
182+
<!--BottomNav-->
183+
<igx-bottom-nav>
184+
<igx-bottom-nav-item>
185+
<igx-bottom-nav-header>
186+
<igx-icon igxBottomNavHeaderIcon>library_music</igx-icon>
187+
<span igxBottomNavHeaderLabel>Songs</span>
188+
</igx-bottom-nav-header>
189+
<igx-bottom-nav-content>
190+
<div class="item" *ngFor="let song of songsList">
191+
<span class="item-line1">{{song.title}}</span><br/>
192+
<span class="item-line2">{{song.artist}}</span>
193+
</div>
194+
</igx-bottom-nav-content>
195+
</igx-bottom-nav-item>
196+
</igx-bottom-nav>`);
197+
});
198+
143199
it('Should insert ng-template content into igx-tab-header', async () => {
144200
appTree.create(
145201
'/testSrc/appPrefix/component/custom.component.html', `

projects/igniteui-angular/migrations/update-12_0_0/index.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export default (): Rule => (host: Tree, context: SchematicContext) => {
3434
const changes = new Map<string, FileChange[]>();
3535
const htmlFiles = update.templateFiles;
3636
const sassFiles = update.sassFiles;
37+
let applyComment = false;
3738

3839
const applyChanges = () => {
3940
for (const [path, change] of changes.entries()) {
@@ -44,6 +45,7 @@ export default (): Rule => (host: Tree, context: SchematicContext) => {
4445
.forEach(c => buffer = c.apply(buffer));
4546

4647
host.overwrite(path, buffer);
48+
applyComment = true;
4749
}
4850
};
4951

@@ -55,7 +57,7 @@ export default (): Rule => (host: Tree, context: SchematicContext) => {
5557
}
5658
};
5759

58-
const isEmptyOrSpaces = (str) => str === null || str === '' || str === '\n' || str.match(/^ *$/) !== null;
60+
const isEmptyOrSpaces = (str) => str === null || str === '' || str === '\n' || str === '\r\n' || str.match(/^[\n\t]* *$/) !== null;
5961

6062
// Replace the tabsType input with tabsAligment
6163
for (const path of htmlFiles) {
@@ -151,7 +153,12 @@ export default (): Rule => (host: Tree, context: SchematicContext) => {
151153

152154
if (tabHeader) {
153155
const content = offset.file.content.substring(tabHeader.sourceSpan.end.offset, offset.endTag.start);
154-
if (!isEmptyOrSpaces(content)) {
156+
// Since igx-tab-item tag is common for old and new igx-tabs
157+
// Check whether igx-tab-content is already present!
158+
const tabContentTag = new RegExp(String.raw`${comp.panelItem}`);
159+
const hasTabContent = content.match(tabContentTag);
160+
161+
if ((!hasTabContent || hasTabContent.length === 0) && !isEmptyOrSpaces(content)) {
155162
const tabPanel = `\n<${comp.panelItem}${classAttrText}>${content}</${comp.panelItem}>\n`;
156163
addChange(offset.file.url, new FileChange(tabHeader.sourceSpan.end.offset, tabPanel, content, 'replace'));
157164
}
@@ -162,17 +169,19 @@ export default (): Rule => (host: Tree, context: SchematicContext) => {
162169
changes.clear();
163170

164171
// Insert a comment indicating the change/replacement
165-
findElementNodes(parseFile(host, path), comp.component).
166-
map(node => getSourceOffset(node as Element)).
167-
forEach(offset => {
168-
const { startTag, file } = offset;
169-
// eslint-disable-next-line max-len
170-
const commentText = `<!--NOTE: This component has been updated by Infragistics migration: v${version}\nPlease check your template whether all bindings/event handlers are correct.-->\n`;
171-
addChange(file.url, new FileChange(startTag.start, commentText));
172-
});
172+
if (applyComment) {
173+
findElementNodes(parseFile(host, path), comp.component).
174+
map(node => getSourceOffset(node as Element)).
175+
forEach(offset => {
176+
const { startTag, file } = offset;
177+
// eslint-disable-next-line max-len
178+
const commentText = `<!--NOTE: This component has been updated by Infragistics migration: v${version}\nPlease check your template whether all bindings/event handlers are correct.-->\n`;
179+
addChange(file.url, new FileChange(startTag.start, commentText));
180+
});
173181

174-
applyChanges();
175-
changes.clear();
182+
applyChanges();
183+
changes.clear();
184+
}
176185
}
177186

178187
for (const sassPath of sassFiles) {

0 commit comments

Comments
 (0)