-
-
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(core): mock of mock will return itself
- Loading branch information
Showing
6 changed files
with
138 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { MockedComponent } from '../mock-component/types'; | ||
import { MockedDirective } from '../mock-directive/types'; | ||
import { MockedModule } from '../mock-module/types'; | ||
import { MockedPipe } from '../mock-pipe/types'; | ||
|
||
import { Type } from './core.types'; | ||
import { isNgDef } from './func.is-ng-def'; | ||
|
||
export function isMockNgDef<T>(component: Type<T>, ngType: 'c'): component is Type<MockedComponent<T>>; | ||
|
||
export function isMockNgDef<T>(directive: Type<T>, ngType: 'd'): directive is Type<MockedDirective<T>>; | ||
|
||
export function isMockNgDef<T>(pipe: Type<T>, ngType: 'p'): pipe is Type<MockedPipe<T>>; | ||
|
||
export function isMockNgDef<T>(module: Type<T>, ngType: 'm'): module is Type<MockedModule<T>>; | ||
|
||
export function isMockNgDef<T>(module: Type<T>): module is Type<T>; | ||
|
||
export function isMockNgDef<TComponent>( | ||
component: Type<TComponent> & { mockOf?: any }, | ||
type?: 'c' | 'd' | 'p' | 'm', | ||
): component is Type<TComponent> { | ||
if (!(component as any).mockOf) { | ||
return false; | ||
} | ||
if (!type) { | ||
return true; | ||
} | ||
|
||
return isNgDef(component.mockOf, type as never); | ||
} |
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,87 @@ | ||
import { | ||
Component, | ||
Directive, | ||
NgModule, | ||
Pipe, | ||
PipeTransform, | ||
} from '@angular/core'; | ||
import { | ||
isMockNgDef, | ||
MockComponent, | ||
MockDirective, | ||
MockModule, | ||
MockPipe, | ||
} from 'ng-mocks'; | ||
|
||
@Component({ | ||
selector: 'target', | ||
template: '', | ||
}) | ||
class TargetComponent {} | ||
|
||
@Directive({ | ||
selector: 'target', | ||
}) | ||
class TargetDirective {} | ||
|
||
@Pipe({ | ||
name: 'target', | ||
}) | ||
class TargetPipe implements PipeTransform { | ||
public transform(value: any): any { | ||
return value; | ||
} | ||
} | ||
|
||
@NgModule({}) | ||
class TargetModule {} | ||
|
||
describe('mock-of-mock', () => { | ||
it('returns the same mock component', () => { | ||
const mock1 = MockComponent(TargetComponent); | ||
const mock2 = MockComponent(mock1); | ||
expect(mock1).not.toBe(TargetComponent); | ||
expect(mock2).toBe(mock1); | ||
expect(isMockNgDef(mock2)).toEqual(true); | ||
expect(isMockNgDef(mock2, 'c')).toEqual(true); | ||
expect(isMockNgDef(mock2, 'd')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'p')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'm')).toEqual(false); | ||
}); | ||
|
||
it('returns the same mock directive', () => { | ||
const mock1 = MockDirective(TargetDirective); | ||
const mock2 = MockDirective(mock1); | ||
expect(mock1).not.toBe(TargetDirective); | ||
expect(mock2).toBe(mock1); | ||
expect(isMockNgDef(mock2)).toEqual(true); | ||
expect(isMockNgDef(mock2, 'c')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'd')).toEqual(true); | ||
expect(isMockNgDef(mock2, 'p')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'm')).toEqual(false); | ||
}); | ||
|
||
it('returns the same mock pipe', () => { | ||
const mock1 = MockPipe(TargetPipe); | ||
const mock2 = MockPipe(mock1); | ||
expect(mock1).not.toBe(TargetPipe); | ||
expect(mock2).toBe(mock1); | ||
expect(isMockNgDef(mock2)).toEqual(true); | ||
expect(isMockNgDef(mock2, 'c')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'd')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'p')).toEqual(true); | ||
expect(isMockNgDef(mock2, 'm')).toEqual(false); | ||
}); | ||
|
||
it('returns the same mock module', () => { | ||
const mock1 = MockModule(TargetModule); | ||
const mock2 = MockModule(mock1); | ||
expect(mock1).not.toBe(TargetModule); | ||
expect(mock2).toBe(mock1); | ||
expect(isMockNgDef(mock2)).toEqual(true); | ||
expect(isMockNgDef(mock2, 'c')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'd')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'p')).toEqual(false); | ||
expect(isMockNgDef(mock2, 'm')).toEqual(true); | ||
}); | ||
}); |