Ajax 在Epic中测试Post请求
我正在尝试测试一个具有ajax post请求的epicAjax 在Epic中测试Post请求,ajax,ts-jest,epic,Ajax,Ts Jest,Epic,我正在尝试测试一个具有ajax post请求的epic test('test sucess login', async() => { const response = { username: 'admin', }; nock('http://localhost:8080') .post('/login', Buffer.from(['admin', 'admin']).toString('bas
test('test sucess login', async() => {
const response = {
username: 'admin',
};
nock('http://localhost:8080')
.post('/login', Buffer.from(['admin', 'admin']).toString('base64'))
.reply(200, response);
const credentials = { username: 'admin', password: 'admin' };
const action$ = ActionsObservable.of(login(credentials));
const result$ = await loginEpic(action$);
console.log(result$);
result$.subscribe(actions => {
expect(actions).toEqual([{
type: 'LOGIN_SUCCESS',
payload: response
}]);
});
});
即使我将预期的操作类型更改为“LOGIN\u FAIL”或使对象为空,测试始终通过。我还在epic中放了一个控制台日志,它从未被调用过
export const loginEpic: Epic<LoginActionTypes | CallHistoryMethodAction > = (action$) => action$.pipe(
filter(isOfType(LOGIN)),
mergeMap(action => login(action.payload.credentials ).pipe(
mergeMap(ajaxResponseObject => {
console.log('inside epic',ajaxResponseObject);
return of(
loginSuccess(ajaxResponseObject.response), push('/test')
);
}),
catchError((error: AjaxError) => of(loginFail(error)))
))
);
export const loginEpic:Epic=(action$)=>action$.pipe(
过滤器(类型(登录)),
mergeMap(action=>login(action.payload.credentials).pipe(
合并映射(ajaxResponseObject=>{
console.log('insideepic',ajaxResponseObject);
归还(
登录成功(ajaxResponseObject.response),推送('/test')
);
}),
catchError((错误:AjaxError)=>of(loginFail(错误)))
))
);
这是登录功能:
const login = (credentials: LoginCredentials): Observable<AjaxResponse> => {
const { username, password } = credentials;
return ajax({
url: 'http://localhost:8080/authenticate',
method: 'POST',
headers: {
Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}}`
}
});
};
const login=(凭证:LoginCredentials):可观察=>{
const{username,password}=凭证;
返回ajax({
网址:'http://localhost:8080/authenticate',
方法:“POST”,
标题:{
授权:`Basic${Buffer.from(`${username}:${password}`)。toString(`base64')}`
}
});
};