Skip to content
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

fix(toggle): accept initial value for state (#120) #130

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,59 @@ class ToggleReactiveTestComponent implements OnInit {
}
}

@Component({
selector: 'sbb-toggle-test-reactive',
template: `
<form [formGroup]="form" novalidate>
<sbb-toggle aria-labelledby="group_label_2" formControlName="test">
<sbb-toggle-option
#options
*ngFor="let option of toggleOptions | async; let i = index"
[infoText]="i === 0 ? 'info text' : undefined"
[label]="option.label"
[value]="option.value"
>
<ng-container *ngIf="i === 0">
<sbb-icon-arrow-right *sbbToggleOptionIcon></sbb-icon-arrow-right>
</ng-container>
<ng-container *ngIf="i === 1">
<sbb-icon-arrows-right-left *sbbToggleOptionIcon></sbb-icon-arrows-right-left>
</ng-container>
<sbb-field mode="long" *ngIf="i === 1">
<sbb-label for="name1">Select date</sbb-label>
<sbb-datepicker>
<input sbbDateInput type="text" />
</sbb-datepicker>
</sbb-field>
</sbb-toggle-option>
</sbb-toggle>
</form>
`
})
class ToggleReactiveDefaultValueTestComponent implements OnInit {
modelReactive = 'Option_2';
@ContentChildren('options') options: QueryList<ToggleOptionComponent>;

form = new FormGroup({
test: new FormControl('Option_2')
});

toggleOptions: Observable<any> = of([
{
label: 'Einfache Fahrt',
value: 'Option_1'
},
{
label: 'Hin- und Rückfahrt',
value: 'Option_2'
}
]);

ngOnInit() {
this.form.get('test').valueChanges.subscribe(val => (this.modelReactive = val));
}
}

@Component({
selector: 'sbb-toggle-test-template-driven',
template: `
Expand Down Expand Up @@ -135,8 +188,6 @@ class ToggleSimpleCaseTestComponent {
modelReactive = 'Option_2';
@ContentChildren('options') options: QueryList<ToggleOptionComponent>;

constructor() {}

toggleOptions: Observable<any> = of([
{
label: 'Einfache Fahrt',
Expand Down Expand Up @@ -248,6 +299,49 @@ describe('ToggleComponent case reactive using mock component', () => {
});
});

describe('ToggleComponent case reactive with default value using mock component', () => {
let componentTest: ToggleReactiveDefaultValueTestComponent;
let fixtureTest: ComponentFixture<ToggleReactiveDefaultValueTestComponent>;

configureTestSuite(() => {
TestBed.configureTestingModule({
imports: [
ToggleModule,
CommonModule,
IconCollectionModule,
DatepickerModule,
FieldModule,
ReactiveFormsModule
],
declarations: [ToggleReactiveDefaultValueTestComponent]
});
});

beforeEach(() => {
fixtureTest = TestBed.createComponent(ToggleReactiveDefaultValueTestComponent);
componentTest = fixtureTest.componentInstance;
fixtureTest.detectChanges();
});

it('component test is created', () => {
expect(componentTest).toBeTruthy();
});

it('it verifies that second toggle button is checked', () => {
fixtureTest.detectChanges();

const toggleOptionReferenceValue = fixtureTest.debugElement.queryAll(
By.css('.sbb-toggle-option-button-inner > input')
);

const toggleOption2ValueElement = toggleOptionReferenceValue[1].nativeElement;
console.log(toggleOption2ValueElement);

expect(toggleOption2ValueElement.attributes.getNamedItem('aria-checked').value).toBe('true');
expect(toggleOption2ValueElement.value).toBe('Option_2');
});
});

describe('ToggleComponent case template driven using mock component', () => {
let componentTest: ToggleTemplateDrivenTestComponent;
let fixtureTest: ComponentFixture<ToggleTemplateDrivenTestComponent>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ export class ToggleComponent extends RadioButton
this._zone.onStable.pipe(first()).subscribe(() =>
this._zone.run(() => {
this._checkNumOfOptions();
const defaultOption = this.toggleOptions.toArray()[0];
// Before assigning the first tab to defaultOption, I check whether a different value has been specified
const defaultOption =
this.toggleOptions.toArray().find(toggle => toggle.value === this.value) ||
this.toggleOptions.toArray()[0];
defaultOption.setToggleChecked(true);
})
);
Expand Down