-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: unable to bind ngIf in angular 17+ (#9314)
- Loading branch information
Showing
8 changed files
with
229 additions
and
15 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import 'jest-preset-angular'; | ||
import 'jest-preset-angular/setup-jest'; | ||
import { ngMocks } from 'ng-mocks'; | ||
|
||
ngMocks.autoSpy('jest'); |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { NgTemplateOutlet } from '@angular/common'; | ||
import { | ||
Component, | ||
ContentChild, | ||
NgModule, | ||
TemplateRef, | ||
VERSION, | ||
} from '@angular/core'; | ||
|
||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/8884 | ||
// New control flow doesn't import NgIf or CommonModule by default. | ||
// However, it still allows to use conditions, and if `ContentChild` is used, | ||
// it causes errors such as "Can't bind to 'ngIf' since it isn't a known property of 'div'". | ||
// The fix is to remove dependency on NgIf or CommonModule. | ||
describe('issue-8884', () => { | ||
ngMocks.throwOnConsole(); | ||
|
||
if (Number.parseInt(VERSION.major, 10) < 17) { | ||
it('needs a17+', () => { | ||
expect(true).toBeTruthy(); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
describe('standalone component without NgIf', () => { | ||
@Component({ | ||
selector: 'standalone-8884', | ||
['standalone' as never]: true, | ||
['imports' as never]: [NgTemplateOutlet], | ||
template: `<ng-template [ngTemplateOutlet]="content" />`, | ||
}) | ||
class Standalone8884Component { | ||
@ContentChild('content', {} as never) | ||
content?: TemplateRef<any>; | ||
} | ||
|
||
describe('real', () => { | ||
beforeEach(() => MockBuilder(Standalone8884Component)); | ||
|
||
it('renders content', () => { | ||
const fixture = MockRender(` | ||
<standalone-8884> | ||
<ng-template #content>content</ng-template> | ||
</standalone-8884> | ||
`); | ||
|
||
expect(ngMocks.formatText(fixture)).toEqual('content'); | ||
}); | ||
}); | ||
|
||
describe('mock', () => { | ||
beforeEach(() => MockBuilder(null, Standalone8884Component)); | ||
|
||
it('renders content', () => { | ||
const fixture = MockRender(` | ||
<standalone-8884> | ||
<ng-template #content>content</ng-template> | ||
</standalone-8884> | ||
`); | ||
expect(ngMocks.formatText(fixture)).toEqual(''); | ||
|
||
ngMocks.render( | ||
ngMocks.findInstance(Standalone8884Component), | ||
ngMocks.findTemplateRef('content'), | ||
); | ||
expect(ngMocks.formatText(fixture)).toEqual('content'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('classic component without NgIf import in its module', () => { | ||
@Component({ | ||
selector: 'target-8884', | ||
template: `<ng-template [ngTemplateOutlet]="content" />`, | ||
}) | ||
class Target8884Component { | ||
@ContentChild('content', {} as never) | ||
content?: TemplateRef<any>; | ||
} | ||
|
||
@NgModule({ | ||
imports: [NgTemplateOutlet], | ||
declarations: [Target8884Component], | ||
exports: [Target8884Component], | ||
}) | ||
class TargetModule {} | ||
|
||
describe('real', () => { | ||
beforeEach(() => MockBuilder(TargetModule)); | ||
|
||
it('renders content', () => { | ||
const fixture = MockRender(` | ||
<target-8884> | ||
<ng-template #content>content</ng-template> | ||
</target-8884> | ||
`); | ||
|
||
expect(ngMocks.formatText(fixture)).toEqual('content'); | ||
}); | ||
}); | ||
|
||
describe('mock', () => { | ||
beforeEach(() => MockBuilder(null, TargetModule)); | ||
|
||
it('render contents', () => { | ||
const fixture = MockRender(` | ||
<target-8884> | ||
<ng-template #content>content</ng-template> | ||
</target-8884> | ||
`); | ||
|
||
expect(ngMocks.formatText(fixture)).toEqual(''); | ||
|
||
ngMocks.render( | ||
ngMocks.findInstance(Target8884Component), | ||
ngMocks.findTemplateRef('content'), | ||
); | ||
expect(ngMocks.formatText(fixture)).toEqual('content'); | ||
}); | ||
}); | ||
}); | ||
}); |