-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: adding NG_VALUE_ACCESSOR only when necessary
closes #145
- Loading branch information
Showing
8 changed files
with
238 additions
and
22 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
10 changes: 9 additions & 1 deletion
10
lib/mock-component/test-components/empty-component.component.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Component } from '@angular/core'; | ||
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { MockComponent } from 'ng-mocks'; | ||
import { directiveResolver } from 'ng-mocks/dist/lib/common/reflect'; | ||
|
||
@Component({ | ||
selector: 'component', | ||
template: '', | ||
}) | ||
export class ComponentDefault {} | ||
|
||
@Component({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: ComponentValueAccessor, | ||
}, | ||
], | ||
selector: 'component', | ||
template: '', | ||
}) | ||
export class ComponentValueAccessor {} | ||
|
||
@Component({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALIDATORS, | ||
useExisting: ComponentValidator, | ||
}, | ||
], | ||
selector: 'component', | ||
template: '', | ||
}) | ||
export class ComponentValidator {} | ||
|
||
// providers should be added to components only in case if they were specified in the original component. | ||
describe('issue-145', () => { | ||
it('ComponentDefault', () => { | ||
const mock = MockComponent(ComponentDefault); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: ComponentDefault, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
|
||
it('ComponentValueAccessor', () => { | ||
const mock = MockComponent(ComponentValueAccessor); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: ComponentValueAccessor, | ||
useExisting: jasmine.anything(), | ||
}, | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
|
||
it('ComponentValidator', () => { | ||
const mock = MockComponent(ComponentValidator); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: ComponentValidator, | ||
useExisting: jasmine.anything(), | ||
}, | ||
{ | ||
multi: true, | ||
provide: NG_VALIDATORS, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
}); |
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,79 @@ | ||
import { Directive } from '@angular/core'; | ||
import { NG_VALIDATORS, NG_VALUE_ACCESSOR } from '@angular/forms'; | ||
import { MockDirective } from 'ng-mocks'; | ||
import { directiveResolver } from 'ng-mocks/dist/lib/common/reflect'; | ||
|
||
@Directive({ | ||
selector: 'directive', | ||
}) | ||
export class DirectiveDefault {} | ||
|
||
@Directive({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: DirectiveValueAccessor, | ||
}, | ||
], | ||
selector: 'directive', | ||
}) | ||
export class DirectiveValueAccessor {} | ||
|
||
@Directive({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALIDATORS, | ||
useExisting: DirectiveValidator, | ||
}, | ||
], | ||
selector: 'directive', | ||
}) | ||
export class DirectiveValidator {} | ||
|
||
// providers should be added to directives only in case if they were specified in the original directive. | ||
describe('issue-145', () => { | ||
it('DirectiveDefault', () => { | ||
const mock = MockDirective(DirectiveDefault); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: DirectiveDefault, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
|
||
it('DirectiveValueAccessor', () => { | ||
const mock = MockDirective(DirectiveValueAccessor); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: DirectiveValueAccessor, | ||
useExisting: jasmine.anything(), | ||
}, | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
|
||
it('DirectiveValidator', () => { | ||
const mock = MockDirective(DirectiveValidator); | ||
const { providers } = directiveResolver.resolve(mock); | ||
expect(providers).toEqual([ | ||
{ | ||
provide: DirectiveValidator, | ||
useExisting: jasmine.anything(), | ||
}, | ||
{ | ||
multi: true, | ||
provide: NG_VALIDATORS, | ||
useExisting: jasmine.anything(), | ||
}, | ||
]); | ||
}); | ||
}); |
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