Skip to content
This repository has been archived by the owner on Nov 6, 2024. It is now read-only.

Commit

Permalink
Fix async optgroup with mz-select (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
jfcere authored and scote committed Dec 7, 2017
1 parent 6c8f61e commit 5b64051
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/app/select/select.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ export class MzSelectDirective extends HandlePropChanges implements OnInit, OnDe
listenOptionChanges() {
const mutationObserverConfiguration: MutationObserverInit = {
childList: true,
subtree: true,
};

this.mutationObserver = new MutationObserver((mutations: MutationRecord[]) => {
Expand Down
49 changes: 49 additions & 0 deletions src/app/select/select.directive.view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,55 @@ describe('MzSelectDirective:view', () => {
});
});
}));

it('Add option 2 in optgroup', async(() => {

const selectAsyncOptions = ['Option 1'];

buildComponent<any>(`
<mz-select-container>
<select mz-select
id="async-options-select"
[label]="'Label'"
[placeholder]="'Placeholder'"
>
<optgroup label="Group 1">
<option *ngFor="let option of selectAsyncOptions">{{ option }}</option>
</optgroup>
</select>
</mz-select-container>
`, {
selectAsyncOptions,
}).then((fixture) => {

nativeElement = fixture.nativeElement;

fixture.detectChanges();

const optionLength = () => fixture.nativeElement.querySelectorAll('option:not([disabled])').length;
const optionGroupLength = () => fixture.nativeElement.querySelectorAll('li.optgroup').length;
const optionListLength = () => fixture.nativeElement.querySelectorAll('li.optgroup-option').length;

expect(optionLength()).toBe(1);
expect(optionGroupLength()).toBe(1);
expect(optionListLength()).toBe(0);

fixture.componentInstance.selectAsyncOptions = ['Option 1', 'Option 2'];
fixture.detectChanges();

// Wait for the MutationObserver to detect the native option elements change and for the native select to emit the change event.
setTimeout(() => {
// The MutationObserver from the select directive detected the native option elements change
// and notified the native select component. The native select then calls material_select() to update
// the materialize select option list.
fixture.detectChanges();

expect(optionLength()).toBe(2);
expect(optionGroupLength()).toBe(1);
expect(optionListLength()).toBe(2);
});
});
}));
});
});

Expand Down

0 comments on commit 5b64051

Please sign in to comment.