admin管理员组文章数量:1400837
I have a county drop down. There is a default value. User can select a country. On change it will redirect user to correct country page. Which is working.
However, I am trying to unit test default value in select box. but I keep getting empty string/value selectEl.nativeElement.value
to be ''
. Any one have any ideas why?
// locale-ponent.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { cultures} from '../../../config/config';
@Component({
selector: 'app-locale',
templateUrl: './localeponent.html',
styleUrls: ['./localeponent.scss']
})
export class LocaleComponent {
cultures = cultures;
constructor() {}
onLocaleChange(locale) {
const str = window.location.href.replace(new RegExp(`/${this.cultures.default}/`), `/${locale}/`);
window.location.assign(str);
}
}
// localeponent.spec.ts
beforeEach(() => {
fixture = TestBed.createComponent(LocaleComponent);
ponent = fixtureponentInstance;
ponent.cultures = {
default: 'en-ca',
supported_cultures: [
{ "name": "United States", "code": "en-us" },
{ "name": "Canada (English)", "code": "en-ca" },
{ "name": "Canada (Français)", "code": "fr-ca" }
]
}
fixture.detectChanges();
});
it('should create', () => {
expect(ponent).toBeTruthy();
});
it('should select the correct flag/locale by default', () => {
fixture.detectChanges();
const selectEl = fixture.debugElement.query(By.css('select'))
console.log(selectEl)
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(selectEl.nativeElement.value).toEqual('en-ca');
});
});
<!-- localeponent.html -->
<label class="locale locale--footer">
<div class="locale__custom-select locale__custom-select__button locale__custom-select__ff-hack">
<select [ngModel]="cultures.default" (ngModelChange)="onLocaleChange($event)">
<option *ngFor="let locale of cultures.supported_cultures" [ngValue]="locale.code">{{locale.name}}</option>
</select>
</div>
</label>
I have a county drop down. There is a default value. User can select a country. On change it will redirect user to correct country page. Which is working.
However, I am trying to unit test default value in select box. but I keep getting empty string/value selectEl.nativeElement.value
to be ''
. Any one have any ideas why?
// locale-ponent.ts
import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { cultures} from '../../../config/config';
@Component({
selector: 'app-locale',
templateUrl: './locale.ponent.html',
styleUrls: ['./locale.ponent.scss']
})
export class LocaleComponent {
cultures = cultures;
constructor() {}
onLocaleChange(locale) {
const str = window.location.href.replace(new RegExp(`/${this.cultures.default}/`), `/${locale}/`);
window.location.assign(str);
}
}
// locale.ponent.spec.ts
beforeEach(() => {
fixture = TestBed.createComponent(LocaleComponent);
ponent = fixture.ponentInstance;
ponent.cultures = {
default: 'en-ca',
supported_cultures: [
{ "name": "United States", "code": "en-us" },
{ "name": "Canada (English)", "code": "en-ca" },
{ "name": "Canada (Français)", "code": "fr-ca" }
]
}
fixture.detectChanges();
});
it('should create', () => {
expect(ponent).toBeTruthy();
});
it('should select the correct flag/locale by default', () => {
fixture.detectChanges();
const selectEl = fixture.debugElement.query(By.css('select'))
console.log(selectEl)
fixture.detectChanges();
fixture.whenStable().then(() => {
expect(selectEl.nativeElement.value).toEqual('en-ca');
});
});
<!-- locale.ponent.html -->
<label class="locale locale--footer">
<div class="locale__custom-select locale__custom-select__button locale__custom-select__ff-hack">
<select [ngModel]="cultures.default" (ngModelChange)="onLocaleChange($event)">
<option *ngFor="let locale of cultures.supported_cultures" [ngValue]="locale.code">{{locale.name}}</option>
</select>
</div>
</label>
Share
Improve this question
asked Jun 5, 2017 at 14:55
MonteCristoMonteCristo
1,5502 gold badges21 silver badges44 bronze badges
1
- 1 How'd you go with this one? – Des Horsley Commented Dec 9, 2017 at 2:05
2 Answers
Reset to default 4"Calling detectChanges() inside whenStable()" will populate the dropdown.
fixture.whenStable().then(() => {
fixture.detectChanges();
expect(selectEl.nativeElement.value).toEqual('en-ca');
});
You try this:
fixture.detectChanges();
const selectEl = fixture.debugElement.query(By.css('select'))
console.log(selectEl);
fixture.detectChanges();
fixture.whenStable();
expect(selectEl.nativeElement.value).toEqual('en-ca');
本文标签: javascriptAngular2 unit test select dropdown valueStack Overflow
版权声明:本文标题:javascript - Angular2 unit test select dropdown value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744246005a2597017.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论