-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add migration for active zone parent (#8873)
Co-authored-by: taiga-family-bot <[email protected]>
- Loading branch information
1 parent
1949c22
commit 66e7c7c
Showing
4 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
projects/cdk/schematics/ng-update/v4/steps/templates/migrate-active-zone.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type {UpdateRecorder} from '@angular-devkit/schematics'; | ||
import type {DevkitFileSystem} from 'ng-morph'; | ||
|
||
import { | ||
findElementsByFn, | ||
findElementsByTagName, | ||
hasElementAttribute, | ||
} from '../../../../utils/templates/elements'; | ||
import {findAttr} from '../../../../utils/templates/inputs'; | ||
import { | ||
getTemplateFromTemplateResource, | ||
getTemplateOffset, | ||
} from '../../../../utils/templates/template-resource'; | ||
import type {TemplateResource} from '../../../interfaces'; | ||
import {removeAttrs} from '../utils/remove-attrs'; | ||
|
||
export function migrateActiveZoneParent({ | ||
resource, | ||
recorder, | ||
fileSystem, | ||
}: { | ||
fileSystem: DevkitFileSystem; | ||
recorder: UpdateRecorder; | ||
resource: TemplateResource; | ||
}): void { | ||
const template = getTemplateFromTemplateResource(resource, fileSystem); | ||
const templateOffset = getTemplateOffset(resource); | ||
|
||
const elements = findElementsByTagName(template, 'ng-template'); | ||
|
||
elements.forEach(({attrs, sourceCodeLocation, childNodes}) => { | ||
const zoneAttr = findAttr(attrs, 'let-activeZone'); | ||
|
||
if (!sourceCodeLocation) { | ||
return; | ||
} | ||
|
||
if (zoneAttr) { | ||
removeAttrs([zoneAttr], sourceCodeLocation, recorder, templateOffset); | ||
} | ||
|
||
const children = findElementsByFn(childNodes, (el) => | ||
hasElementAttribute(el, 'tuiActiveZoneParent'), | ||
); | ||
|
||
children.forEach(({attrs, sourceCodeLocation}) => { | ||
const parentAttr = findAttr(attrs, 'tuiActiveZoneParent'); | ||
|
||
if (!parentAttr || !sourceCodeLocation) { | ||
return; | ||
} | ||
|
||
if (parentAttr) { | ||
removeAttrs([parentAttr], sourceCodeLocation, recorder, templateOffset); | ||
} | ||
}); | ||
}); | ||
} |
132 changes: 132 additions & 0 deletions
132
projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-active-zone.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import {join} from 'node:path'; | ||
|
||
import {HostTree} from '@angular-devkit/schematics'; | ||
import {SchematicTestRunner, UnitTestTree} from '@angular-devkit/schematics/testing'; | ||
import type {TuiSchema} from '@taiga-ui/cdk/schematics/ng-add/schema'; | ||
import { | ||
createProject, | ||
createSourceFile, | ||
resetActiveProject, | ||
saveActiveProject, | ||
setActiveProject, | ||
} from 'ng-morph'; | ||
|
||
import {createAngularJson} from '../../../utils/create-angular-json'; | ||
|
||
const collectionPath = join(__dirname, '../../../migration.json'); | ||
|
||
const COMPONENT_BEFORE = ` | ||
@Component({ | ||
standalone: true, | ||
templateUrl: './test.template.html', | ||
}) | ||
export class Test { | ||
}`; | ||
|
||
const TEMPLATE_BEFORE = ` | ||
<tui-hosted-dropdown | ||
tuiDropdownLimitWidth="fixed" | ||
[content]="dropdown" | ||
[(open)]="open" | ||
> | ||
<button | ||
size="l" | ||
title="Menu" | ||
tuiIconButton | ||
type="button" | ||
class="tui-group__auto-width-item" | ||
[iconRight]="icon" | ||
></button> | ||
</tui-hosted-dropdown> | ||
<ng-template | ||
#dropdown | ||
let-activeZone | ||
> | ||
<tui-select | ||
class="margin" | ||
[tuiActiveZoneParent]="activeZone" | ||
[(ngModel)]="selected" | ||
> | ||
Nested Select | ||
<tui-data-list-wrapper | ||
*tuiDataList | ||
[items]="selectItems" | ||
></tui-data-list-wrapper> | ||
</tui-select> | ||
</ng-template> | ||
`; | ||
|
||
const TEMPLATE_AFTER = ` | ||
<div ${''} | ||
tuiDropdownLimitWidth="fixed" | ||
[tuiDropdown]="dropdown" | ||
[(tuiDropdownOpen)]="open" | ||
> | ||
<button | ||
size="l" | ||
title="Menu" | ||
tuiIconButton | ||
type="button" | ||
class="tui-group__auto-width-item" | ||
[iconEnd]="icon" | ||
></button> | ||
</div> | ||
<ng-template | ||
#dropdown | ||
${''} | ||
> | ||
<tui-select | ||
class="margin" | ||
${''} | ||
[(ngModel)]="selected" | ||
> | ||
Nested Select | ||
<tui-data-list-wrapper | ||
*tuiDataList | ||
[items]="selectItems" | ||
></tui-data-list-wrapper> | ||
</tui-select> | ||
</ng-template> | ||
`; | ||
|
||
describe('ng-update', () => { | ||
let host: UnitTestTree; | ||
let runner: SchematicTestRunner; | ||
|
||
beforeEach(() => { | ||
host = new UnitTestTree(new HostTree()); | ||
runner = new SchematicTestRunner('schematics', collectionPath); | ||
|
||
setActiveProject(createProject(host)); | ||
|
||
createMainFiles(); | ||
|
||
saveActiveProject(); | ||
}); | ||
|
||
it('should migrate active zone parent in template', async () => { | ||
const tree = await runner.runSchematic( | ||
'updateToV4', | ||
{'skip-logs': process.env['TUI_CI'] === 'true'} as Partial<TuiSchema>, | ||
host, | ||
); | ||
|
||
expect(tree.readContent('test/app/test.template.html')).toEqual(TEMPLATE_AFTER); | ||
}); | ||
|
||
afterEach(() => { | ||
resetActiveProject(); | ||
}); | ||
}); | ||
|
||
function createMainFiles(): void { | ||
createSourceFile('test/app/test.component.ts', COMPONENT_BEFORE); | ||
|
||
createSourceFile('test/app/test.template.html', TEMPLATE_BEFORE); | ||
|
||
createAngularJson(); | ||
createSourceFile( | ||
'package.json', | ||
'{"dependencies": {"@angular/core": "~13.0.0", "@taiga-ui/addon-commerce": "~3.42.0"}}', | ||
); | ||
} |