-
-
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.
feat(ngMocks): supports custom method names for change and touch #3341
- Loading branch information
Showing
8 changed files
with
265 additions
and
14 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
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 } from '@angular/core'; | ||
import { | ||
ControlValueAccessor, | ||
FormControl, | ||
NG_VALUE_ACCESSOR, | ||
ReactiveFormsModule, | ||
} from '@angular/forms'; | ||
|
||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Directive({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: CvaDirective, | ||
}, | ||
], | ||
selector: 'custom', | ||
}) | ||
class CvaDirective implements ControlValueAccessor { | ||
public registerOnChange = (fn: never) => | ||
(this.customChangeClb = fn); | ||
public registerOnTouched = (fn: never) => | ||
(this.customTouchedClb = fn); | ||
public setDisabledState = () => undefined; | ||
public writeValue = () => undefined; | ||
|
||
public customChangeClb() {} | ||
public customTouchedClb() {} | ||
} | ||
|
||
@Component({ | ||
selector: 'my', | ||
template: ` <custom [formControl]="myControl"></custom> `, | ||
}) | ||
class MyComponent { | ||
public readonly myControl = new FormControl(); | ||
} | ||
|
||
@NgModule({ | ||
declarations: [MyComponent, CvaDirective], | ||
exports: [MyComponent], | ||
imports: [ReactiveFormsModule], | ||
}) | ||
class MyModule {} | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/3341 | ||
describe('ng-mocks-change:3341', () => { | ||
beforeEach(() => MockBuilder(MyModule)); | ||
|
||
it('throws error about the default method', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => ngMocks.change(cvaEl, 123)).toThrowError( | ||
/please ensure it has 'onChange' method/, | ||
); | ||
}); | ||
|
||
it('throws error with suggestions', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => ngMocks.change(cvaEl, 123)).toThrowError( | ||
/customChangeClb, customTouchedClb/, | ||
); | ||
}); | ||
|
||
it('throws error about the wrongly provided method', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => | ||
ngMocks.change(cvaEl, 123, 'triggerChange'), | ||
).toThrowError(/please ensure it has 'triggerChange' method/); | ||
}); | ||
|
||
it('triggers change correctly', () => { | ||
const component = MockRender(MyComponent).point.componentInstance; | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(component.myControl.value).toEqual(null); | ||
ngMocks.change(cvaEl, 123, 'customChangeClb'); | ||
expect(component.myControl.value).toEqual(123); | ||
}); | ||
}); |
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 } from '@angular/core'; | ||
import { | ||
ControlValueAccessor, | ||
FormControl, | ||
NG_VALUE_ACCESSOR, | ||
ReactiveFormsModule, | ||
} from '@angular/forms'; | ||
|
||
import { MockBuilder, MockRender, ngMocks } from 'ng-mocks'; | ||
|
||
@Directive({ | ||
providers: [ | ||
{ | ||
multi: true, | ||
provide: NG_VALUE_ACCESSOR, | ||
useExisting: CvaDirective, | ||
}, | ||
], | ||
selector: 'custom', | ||
}) | ||
class CvaDirective implements ControlValueAccessor { | ||
public registerOnChange = (fn: never) => | ||
(this.customChangeClb = fn); | ||
public registerOnTouched = (fn: never) => | ||
(this.customTouchedClb = fn); | ||
public setDisabledState = () => undefined; | ||
public writeValue = () => undefined; | ||
|
||
public customChangeClb() {} | ||
public customTouchedClb() {} | ||
} | ||
|
||
@Component({ | ||
selector: 'my', | ||
template: ` <custom [formControl]="myControl"></custom> `, | ||
}) | ||
class MyComponent { | ||
public readonly myControl = new FormControl(); | ||
} | ||
|
||
@NgModule({ | ||
declarations: [MyComponent, CvaDirective], | ||
exports: [MyComponent], | ||
imports: [ReactiveFormsModule], | ||
}) | ||
class MyModule {} | ||
|
||
// @see https://github.com/help-me-mom/ng-mocks/issues/3341 | ||
describe('ng-mocks-touch:3341', () => { | ||
beforeEach(() => MockBuilder(MyModule)); | ||
|
||
it('throws error about the default method', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => ngMocks.touch(cvaEl)).toThrowError( | ||
/please ensure it has 'onTouched' method/, | ||
); | ||
}); | ||
|
||
it('throws error with suggestions', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => ngMocks.touch(cvaEl)).toThrowError( | ||
/customChangeClb, customTouchedClb/, | ||
); | ||
}); | ||
|
||
it('throws error about the wrongly provided method', () => { | ||
MockRender(MyComponent); | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(() => ngMocks.touch(cvaEl, 'triggerTouch')).toThrowError( | ||
/please ensure it has 'triggerTouch' method/, | ||
); | ||
}); | ||
|
||
it('triggers touch correctly', () => { | ||
const component = MockRender(MyComponent).point.componentInstance; | ||
const cvaEl = ngMocks.find('custom'); | ||
|
||
expect(component.myControl.touched).toEqual(false); | ||
ngMocks.touch(cvaEl, 'customTouchedClb'); | ||
expect(component.myControl.touched).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