我是JEST的新手,正在尝试为tsx文件编写单元测试用例。我正在尝试测试一个函数,该函数接受一个参数,并将全局变量(x)的值设置为等于传递的参数。运行测试时,我得到的值是未定义的。这是代码:
.TSX文件
import { Component, Event, EventEmitter, Method, Prop } from '@stencil/core';
import { CountryLanguageSelectorModel } from './country-language-selector.model';
@Component({
tag: 'dxp-country-language-selector',
styleUrls: ['dxp-country-language-selector.css'],
shadow: false
})
export class CountryLanguageSelector {
@Prop({ mutable: true }) contentData: CountryLanguageSelectorModel;
@Prop({ mutable: true }) contentPath = '../assets/mock.json';
@Prop({ mutable: true }) modalView: boolean;
@Prop({ mutable: true }) selectedCountry: string;
@Prop({ mutable: true }) selectedLanguage: string;
@Event() countrySelected: EventEmitter;
@Event() languageSelected: EventEmitter;
componentWillLoad () {
this.getData(this.contentPath)
.then(data => {
this.contentData = data;
this.selectedCountry = this.contentData.countryList[0];
this.selectedLanguage = this.contentData.languageList[0];
});
}
@Method()
getData (contentPath: string): Promise<CountryLanguageSelectorModel> {
if (contentPath) {
return fetch(contentPath)
.then((response: any) => {
if (response.ok) {
return response.json();
} else {
return Promise.resolve(null);
}
});
} else {
return Promise.resolve(this.contentData);
}
}
countrySelectedHandler (selectedCountry) {
this.selectedCountry = selectedCountry.target.value;
this.countrySelected.emit(selectedCountry.target.value);
}
languageSelectedHandler (selectedLanguage) {
this.selectedLanguage = selectedLanguage.target.value;
this.languageSelected.emit(selectedLanguage.target.value);
}
render () {
if (this.contentData) {
return (
<div>
<div id="country-selector" class="country-selector primary-light" onClick={() => { this.modalView = true }}>
<div class="wrapper">
<p class="globe-wrapper"></p>
<a href="#." class="clang-selector" role="button" aria-disabled="false"><span
class="country">{this.selectedCountry}
<span class="down-arrow"></span>
</span><span class="lang small">{this.selectedLanguage}</span></a>
</div>
</div>
{ this.modalView ?
<div class="modal main-section col-12 visible" id="modal">
<div class="modal-dialog col-xl-8 col-lg-8 col-md-10 primary-light">
<div class="modal-header">
<span class="btn-close" id="closeBtn"
onClick={() => this.modalView = false}><span
class="icon icon-close"></span></span>
</div>
<div class="modal-body">
<p><a href="" aria-label="go to">
{/*<span class="mastercard-logo"></span>*/}
<img src={this.contentData.imageUrl}></img>
</a></p>
<h5>{this.contentData.title}</h5>
<p>{this.contentData.subtitle}</p>
<div id="country">
<div>Country</div>
<select title="Select your country" aria-label="Select your country"
onInput={event => this.countrySelectedHandler(event)}>
{this.contentData.countryList.map(country =>
<option>{country}</option>
)}
</select>
</div>
<p></p>
<div id="lang">
<div>Language</div>
<select title="Select your language" aria-label="Select your language"
onInput={event => this.languageSelectedHandler(event)}>
{this.contentData.languageList.map(language =>
<option>{language}</option>
)}
</select>
</div>
</div>
<div class="modal-footer">
<button class="btn btn-cta btn-brick" id="go-back"
onClick={() => this.modalView = false}>{this.contentData.buttonText}</button>
</div>
</div>
</div> : '' }
</div>
)
}
}
}
================单元测试=====================
import { TestWindow } from '@stencil/core/testing'
import { CountryLanguageSelector } from './dxp-country-language-selector'
let obj = new CountryLanguageSelector();
window.fetch = jest.fn().mockImplementation(contentPath => {
if (contentPath === '../assets/mock.json') {
return Promise.resolve({
json: () => {
return {
'title': 'Select Country',
'subtitle': 'Lorem Ipsum',
'logoPath': '../images/icons/logo.svg',
'countryList': ['India', 'USA', 'Australia'],
'languageList': ['English', 'Hindi', 'French'],
'buttonText': 'Go Back To Site'
};
}
});
} else {
return Promise.resolve('error');
}
});
describe('CountryLanguageSelector', () => {
beforeEach(() => {
obj = new CountryLanguageSelector();
});
=======这失败了===========
// it('should set the passed argument as selected country', () => {
// let country = { target : { value: '' } } ;
// country.target.value = 'India';
// obj.languageSelectedHandler(country);
// expect(obj.selectedLanguage).toEqual('India');
// });
});
===================================