-
-
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.
Merge pull request #5321 from satanTime/issues/5239
Bug: dependencies of a mock take precedence over root mocks #5239
- Loading branch information
Showing
9 changed files
with
189 additions
and
11 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
4 changes: 2 additions & 2 deletions
4
libs/ng-mocks/src/lib/mock-builder/promise/init-mock-declarations.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
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
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,73 @@ | ||
import { Component } from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { MatExpansionModule } from '@angular/material/expansion'; | ||
import { MatMenuModule } from '@angular/material/menu'; | ||
import { NoopAnimationsModule } from '@angular/platform-browser/animations'; | ||
import { | ||
isMockOf, | ||
MockComponent, | ||
MockRender, | ||
ngMocks, | ||
} from 'ng-mocks'; | ||
|
||
// A simple dependency component we are going to mock that imports the standalone pipe. | ||
@Component({ | ||
selector: 'dependency', | ||
template: 'dependency', | ||
standalone: true, | ||
imports: [MatMenuModule], | ||
}) | ||
class DependencyComponent { | ||
public dependency5239e2e() {} | ||
} | ||
|
||
// A standalone component we are going to test. | ||
@Component({ | ||
selector: 'target', | ||
template: ` | ||
<dependency></dependency> | ||
<mat-expansion-panel [expanded]="true"> | ||
<mat-expansion-panel-header> | ||
This is the expansion title | ||
</mat-expansion-panel-header> | ||
<ng-template matExpansionPanelContent> | ||
Some deferred content | ||
</ng-template> | ||
</mat-expansion-panel> | ||
`, | ||
}) | ||
class TargetComponent { | ||
public target5239e2e() {} | ||
} | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/5239 | ||
describe('issue-5239', () => { | ||
beforeEach(() => { | ||
return TestBed.configureTestingModule({ | ||
declarations: [ | ||
// our component for testing | ||
TargetComponent, | ||
], | ||
imports: [ | ||
NoopAnimationsModule, | ||
|
||
// imports PortalModule, therefore it should be kept. | ||
MatExpansionModule, | ||
|
||
// the dependent component we want to mock, | ||
// but internally uses MatMenuModule > OverlayModule > PortalModule, | ||
// and mocks it, but should not. | ||
MockComponent(DependencyComponent), | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
it('renders dependencies', () => { | ||
const fixture = MockRender(TargetComponent); | ||
const html = ngMocks.formatHtml(fixture); | ||
expect(html).toContain('Some deferred content'); | ||
|
||
const dependency = ngMocks.findInstance(DependencyComponent); | ||
expect(isMockOf(dependency, DependencyComponent)).toEqual(true); | ||
}); | ||
}); |
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,89 @@ | ||
import { | ||
Component, | ||
Pipe, | ||
PipeTransform, | ||
VERSION, | ||
} from '@angular/core'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { | ||
ngMocks, | ||
MockComponent, | ||
MockPipe, | ||
MockRender, | ||
} from 'ng-mocks'; | ||
|
||
// A simple standalone pipe we are going to mock. | ||
@Pipe( | ||
{ | ||
name: 'pipe', | ||
standalone: true, | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
) | ||
class StandalonePipe implements PipeTransform { | ||
transform(value: string | null): string { | ||
return `${value}:${this.constructor.name}`; | ||
} | ||
|
||
public pipe5239() {} | ||
} | ||
|
||
// A simple dependency component we are going to mock that imports the standalone pipe. | ||
@Component( | ||
{ | ||
selector: 'dependency', | ||
template: 'dependency', | ||
standalone: true, | ||
imports: [StandalonePipe], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
) | ||
class DependencyComponent { | ||
public dependency5239() {} | ||
} | ||
|
||
// A standalone component we are going to test. | ||
@Component( | ||
{ | ||
selector: 'standalone', | ||
template: `<dependency></dependency> {{ 'test' | pipe }}`, | ||
standalone: true, | ||
imports: [DependencyComponent, StandalonePipe], | ||
} as never /* TODO: remove after upgrade to a14 */, | ||
) | ||
class StandaloneComponent { | ||
public standalone5239() {} | ||
} | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/5239 | ||
// The problem here was because of mocks of DependencyComponent. | ||
// It has StandalonePipe too, so it mocked it without the custom function and cached, | ||
// whereas the mock with the custom function was ignored due to existing cache of the pipe. | ||
describe('issue-5239', () => { | ||
if (Number.parseInt(VERSION.major, 10) < 14) { | ||
it('needs >=a14', () => { | ||
expect(true).toBeTruthy(); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [ | ||
// our component for testing | ||
StandaloneComponent, | ||
|
||
// the dependent component we want to mock | ||
MockComponent(DependencyComponent), | ||
|
||
// the pipe we want to mock with a custom transform | ||
MockPipe(StandalonePipe, () => 'mock'), | ||
], | ||
}).compileComponents(); | ||
}); | ||
|
||
it('renders dependencies', () => { | ||
const fixture = MockRender(StandaloneComponent); | ||
expect(ngMocks.formatHtml(fixture)).toContain('mock'); | ||
}); | ||
}); |