0
我有一個React組件,我從JS組件遷移過來。我正在遷移並檢查測試,並且我有很多失敗,因爲存根似乎不再工作。這裏是我的組件......使用酶淺的渲染與Sinon存根淺渲染
import DeleteButton from "./delete-button.jsx"
import Dialogs from "../../dialogs";
import React from "react";
import UrlHelper from "../../helpers/url-helper";
export default class ActiveDeleteButton extends React.Component {
/**
* Creates an instance of ActiveDeleteButton.
*
* @param {object} props The react props collection.
*
* @memberOf ActiveDeleteButton
*/
constructor(props) {
super (props);
this.handleConfirmDelete = this.handleConfirmDelete.bind(this);
}
handleConfirmDelete() {
$.ajax({
url: this.props.deleteUri,
type: `DELETE`,
contentType: `application/json; charset=utf-8`,
cache: false,
success: (xhr) => {
let successUri = this.props.successUri;
if (!successUri && xhr && xhr.uri) { successUri = xhr.uri; }
if (successUri) { UrlHelper.redirect(successUri); }
},
error: (xhr, status) => {
this.showFailed();
}
});
}
/**
* Shows failure of deletion.
*
* @memberOf ActiveDeleteButton
*/
showFailed() {
Dialogs.alert(this.props.errorMessage);
}
/**
* Renders the component to the DOM.
*
* @returns the HTML to render.
*
* @memberOf ActiveDeleteButton
*/
render() {
return (
<DeleteButton text = {this.props.text}
title = {this.props.title}
cancelText = {this.props.cancelText}
confirmText = {this.props.confirmText}
message = {this.props.message}
onConfirmDelete = {this.handleConfirmDelete} />
);
}
}
而這裏的測試(冷凝)...
describe("performs a DELETE AJAX request",() => {
it ("for specified URLs", sinon.test(function() {
let wrapper = shallow(<ActiveDeleteButton text = "Click Me" />);
let instance = wrapper.instance();
let ajaxStub = this.stub($, 'ajax');
instance.forceUpdate()
wrapper.update()
instance.handleConfirmDelete();
console.log(ajaxStub.getCall(0));
let options = ajaxStub.getCall(0).args[0];
assert.equal(options.url, objUt.deleteUri);
assert.equal(options.type, "DELETE");
}));
}));
我的問題是, 'ajaxStub.getCall(0)' 返回null。這應該返回Ajax調用,以便我可以檢查參數(以前它在我的舊JS組件中使用過)。雖然它(在我看來)顯然應該是這個存根從未被調用。
我在這裏錯過了什麼嗎?