Im mit diesem Setup für meine App:Umgang Versprechen
- ReactJS
- KarmaJS
- Instanbul
An dieser Stelle halte ich erhalten diesen Fehler: Object is not a constructor (evaluating '(0, _whatwgFetch2['default'])')
Während ich das Internet überprüfe, sagt es mir immer, was ich importieren soll eigentlich schon, ich bin ein bisschen verloren, wohin ich gehen soll.
Meine Methode, die den Aufruf ausführt:
import fetch from 'whatwg-fetch';
export function authenticateUser(username, password) {
return function (dispatch, getState) {
dispatch({type: 'LOGIN'});
return fetch(getState().config.components.Authentication.login_endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
credentials: 'same-origin',
body: JSON.stringify({
username: username,
password: password,
})
}).then(resp => {
if (resp.status === 302) {
window.location = resp.url;
} else if (resp.status >= 300 && resp.status < 500) {
return Promise.resolve(resp);
} else {
return Promise.reject(resp.statusText);
}
}).then(resp => resp.json())
.then(resp => {
const error = data.errors[0];
dispatch({ type: 'LOGIN_FAILED', payload: error.detail });
}).catch(error => {
console.error(error); // I know, still needs to be catched
});
};
}
Mein Testfall:
import { expect } from 'chai';
import configureMockStore from 'redux-mock-store';
import thunk from 'redux-thunk';
import sinon from 'sinon';
import { authenticateUser } from '../../src/actions/index';
const initialConfig = {
authentication: {
loginform_loading: false,
loginform_errorMessage: null,
forgotpassword_loading: false,
forgotpassword_errorMessage: null,
forgotpassword_passwordSent: false
},
config: {
components: {
Authentication: {
'login_endpoint': '/api/auth/login',
'forgotpassword_enabled': true,
'forgotpassword_path': '/auth/forgot-password',
'forgotpassword_endpoint': '/auth/forgot-password'
}
}
}
};
const middlewares = [ thunk ];
const mockStore = configureMockStore(middlewares);
let store;
describe('Action/Index/authenticateUser',() => {
const testUsername = 'User';
const testPassword = 'Test123';
let sandbox;
beforeEach(function() {
sandbox = sinon.sandbox.create();
store = mockStore(initialConfig);
});
afterEach(function() {
sandbox.restore();
});
it('LOGIN is dispatched',() => {
var response = mockResponse(400, 'Found', '{"errors": [{ "message": "first" }] }');
sandbox.stub(window, 'fetch')
.resolves(response);
return store.dispatch(authenticateUser(testUsername, testPassword))
.then(() => {
var expectedActions = store.getActions();
expect(expectedActions).to.not.be.empty;
var action = expectedActions[0];
expect(action.type).to.equal('LOGIN');
});
});
});
Hoffnung, dass jemand auf mir helfen kann, was falsch ist. Und vielleicht auch, wie die Fehlerbehandlung in dem Versprechen selbst zu verbessern
Danke, Pim
Versuchen Sie, Ihre Import Linie zu ändern, nur um 'Import‚WHATWG-fetch'' – Panther
Dies ist! Hat sich etwas daran geändert, dass es vorher funktioniert hat? – Dirkos