javascript - Angular2 unit test select dropdown value - Stack Overflow

I have a county drop down. There is a default value. User can select a country. On change it will redir

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
Add a ment  | 

2 Answers 2

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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744246005a4564933.html

相关推荐

  • javascript - Angular2 unit test select dropdown value - Stack Overflow

    I have a county drop down. There is a default value. User can select a country. On change it will redir

    8天前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信