-
Notifications
You must be signed in to change notification settings - Fork 6.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(select): emit change event #2458
Merged
tinayuangao
merged 4 commits into
angular:master
from
crisbeto:2248/select-change-event
Jan 12, 2017
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ describe('MdSelect', () => { | |
beforeEach(async(() => { | ||
TestBed.configureTestingModule({ | ||
imports: [MdSelectModule.forRoot(), ReactiveFormsModule, FormsModule], | ||
declarations: [BasicSelect, NgModelSelect, ManySelects, NgIfSelect], | ||
declarations: [BasicSelect, NgModelSelect, ManySelects, NgIfSelect, SelectWithChangeEvent], | ||
providers: [ | ||
{provide: OverlayContainer, useFactory: () => { | ||
overlayContainerElement = document.createElement('div') as HTMLElement; | ||
|
@@ -1140,13 +1140,46 @@ describe('MdSelect', () => { | |
|
||
}); | ||
|
||
describe('change event', () => { | ||
let fixture: ComponentFixture<SelectWithChangeEvent>; | ||
let trigger: HTMLElement; | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(SelectWithChangeEvent); | ||
fixture.detectChanges(); | ||
|
||
trigger = fixture.debugElement.query(By.css('.md-select-trigger')).nativeElement; | ||
}); | ||
|
||
it('should emit an event when the selected option has changed', () => { | ||
trigger.click(); | ||
fixture.detectChanges(); | ||
|
||
(overlayContainerElement.querySelector('md-option') as HTMLElement).click(); | ||
|
||
expect(fixture.componentInstance.changeListener).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should not emit multiple change events for the same option', () => { | ||
trigger.click(); | ||
fixture.detectChanges(); | ||
|
||
let option = overlayContainerElement.querySelector('md-option') as HTMLElement; | ||
|
||
option.click(); | ||
option.click(); | ||
|
||
expect(fixture.componentInstance.changeListener).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
}); | ||
|
||
@Component({ | ||
selector: 'basic-select', | ||
template: ` | ||
<div [style.height.px]="heightAbove"></div> | ||
<md-select placeholder="Food" [formControl]="control" [required]="isRequired"> | ||
<md-select placeholder="Food" [formControl]="control" [required]="isRequired" | ||
(change)="changeListener($event)"> | ||
<md-option *ngFor="let food of foods" [value]="food.value" [disabled]="food.disabled"> | ||
{{ food.viewValue }} | ||
</md-option> | ||
|
@@ -1169,6 +1202,7 @@ class BasicSelect { | |
isRequired: boolean; | ||
heightAbove = 0; | ||
heightBelow = 0; | ||
changeListener = jasmine.createSpy('MdSelect change listener'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you remove this too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, done. |
||
|
||
@ViewChild(MdSelect) select: MdSelect; | ||
@ViewChildren(MdOption) options: QueryList<MdOption>; | ||
|
@@ -1234,7 +1268,28 @@ class NgIfSelect { | |
@ViewChild(MdSelect) select: MdSelect; | ||
} | ||
|
||
@Component({ | ||
selector: 'select-with-change-element', | ||
template: ` | ||
<md-select (change)="changeListener($event)"> | ||
<md-option *ngFor="let food of foods" [value]="food">{{ food }}</md-option> | ||
</md-select> | ||
` | ||
}) | ||
class SelectWithChangeEvent { | ||
foods: string[] = [ | ||
'steak-0', | ||
'pizza-1', | ||
'tacos-2', | ||
'sandwich-3', | ||
'chips-4', | ||
'eggs-5', | ||
'pasta-6', | ||
'sushi-7' | ||
]; | ||
|
||
changeListener = jasmine.createSpy('MdSelect change listener'); | ||
} | ||
|
||
/** | ||
* TODO: Move this to core testing utility until Angular has event faking | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you remove the change event and listener from the basic select component? It should only be in the new test component.