我正在使用“ aws-amplify”库中的signIn方法。在开玩笑地运行测试用例时,我无法从该库中调用signIn方法。
代码:
import { Auth } from "aws-amplify"; // import statement
//code for function
handleSubmit = async event => {
event.preventDefault();
this.setState({ isLoading: true });
try {
await Auth.signIn(this.state.username, this.state.password);
this.props.history.push("/dashboard");
} catch (e) {
this.setState({ isLoading: false });
}
}
测试文件:
it('calls event handler; "handleSubmit"', async() => {
const componentInstance = Wrapper2.dive().instance();
componentInstance.setState({
isLoading : false,
username : "demo",
password : "demo"
})
const event = {
preventDefault : () => {}
};
await componentInstance.handleSubmit(event);
expect(componentInstance.state.isLoading).toEqual(true);
});
在测试用例之上运行时,它总是进入handleSubmit()函数的catch部分。
如何实现从“ aws-amplify”库调用signIn方法并测试正/负方案?
指导我,谢谢。
答案 0 :(得分:5)
一种方法是模拟signIn函数并使用它。 对于在测试文件中导入Auth
import { Auth } from "aws-amplify";
然后在调用handleSubmit函数模拟登录函数之前
it('calls event handler; "handleSubmit"', async() => {
const componentInstance = Wrapper2.dive().instance();
componentInstance.setState({
isLoading : false,
username : "demo",
password : "demo"
})
const event = {
preventDefault : () => {}
};
Auth.signIn = jest.fn().mockImplementation(
() => {
// return whatever you want to test
});
await componentInstance.handleSubmit(event);
expect(componentInstance.state.isLoading).toEqual(true);
});
答案 1 :(得分:1)
这是我的方法:
import awsAmplify from 'aws-amplify'
jest.mock('aws-amplify')
it('does something', () => {
awsAmplify.Auth.signIn.mockRejectedValue('mock error')
awsAmplify.Auth.currentAuthenticatedUser.mockResolvedValue('mock user')
...
})
答案 2 :(得分:0)
这就是我使用 React Native CLI 和 React 测试库的方法?:
使用 CLI 在我的 React Native 应用程序的根目录上创建 mocks 文件夹,其中包含 aws-amplify.js 文件。
在 mocks/aws-amplify.js 里面我有这个代码:
export const Auth = {
currentSession: jest.fn().mockImplementation(() => {
const session = {
accessToken: {
jwtToken: '123456',
},
idToken: {
payload: {
email: 'demo@test.com',
sub: 'abc123',
},
},
};
return Promise.resolve(session);
}),
signOut: jest.fn(),
signIn: jest.fn().mockImplementation(
(email, pass) =>
new Promise((resolve, reject) => {
const userExists = user[email];
if (userExists && pass === '12345678') {
const signedUser = {
username: 'abcdfg123',
attributes: {email, name: 'John Rambo', phone: '+460777777777'},
signInUserSession: {
accessToken: {jwtToken: '123456'},
},
};
return resolve(signedUser);
}
if (userExists) {
return reject({
code: 'NotAuthorizedException',
name: 'NotAuthorizedException',
message: 'Incorrect username or password.',
});
}
return reject({
code: 'UserNotFoundException',
name: 'UserNotFoundException',
message: 'User does not exist.',
});
}),
),
signUp: jest.fn().mockImplementation(
({username, pass, attributes}) =>
new Promise((resolve, reject) => {
const newUser = {
username: 'abcdfg123',
email: username,
name: attributes.name,
signInUserSession: {
accessToken: {jwtToken: '123456'},
},
};
return resolve(newUser);
}),
),
confirmSignUp: jest.fn().mockImplementation(
(email, code) =>
new Promise((resolve, reject) => {
const confirmedUser = {
userConfirmed: true,
username: 'abcdfg123',
user: {username: email},
};
if (code === '123456') {
return resolve(confirmedUser);
}
return reject({
code: 'CodeMismatchException',
name: 'CodeMismatchException',
message: 'Invalid verification code provided, please try again.',
});
}),
),
currentAuthenticatedUser: jest.fn().mockImplementation(
() =>
new Promise((resolve, reject) => {
const currentUser = {
username: 'abc123',
email: 'demo@test.com',
accessToken: '123cvb123',
name: 'John Rambo',
phone: '+46761022312',
phoneVerified: false,
attributes: {
sub: 'abc123',
},
};
return resolve(currentUser);
}),
),
updateUserAttributes: jest.fn(),
};
现在,在我的测试文件中,我有这个:
import React from 'react';
import {render, fireEvent, act} from '@testing-library/react-native';
import {AuthContext} from '../../context/AuthProvider';
import SignInScreen from '../SignInScreen';
describe('<SignInScreen />', () => {
const navigation = {
setOptions: jest.fn(),
navigate: jest.fn(),
};
const currentSession = {
username: '',
email: '',
accessToken: '',
};
const dispatchAuth = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
});
test('should SignIn user', async () => {
const route = {
params: {
email: 'demo@user.com',
},
};
const password = '12345678';
const {getByPlaceholderText, getByTestId} = render(
<AuthContext.Provider value={{currentSession, dispatchAuth}}>
<SignInScreen navigation={navigation} route={route} />
</AuthContext.Provider>,
);
const passwordInput = getByPlaceholderText('Password');
fireEvent(passwordInput, 'onChangeText', password);
const signInButton = getByTestId('signIn-button');
await act(async () => await fireEvent(signInButton, 'press'));
expect(navigation.navigate).toHaveBeenCalledWith('User');
});
test('should Not SignIn user with not existing username or wrong password', async () => {
const route = {
params: {
email: 'notexisting@user.com',
},
};
const password = '123';
const {getByPlaceholderText, getByTestId, findByText} = render(
<AuthContext.Provider value={{currentSession, dispatchAuth}}>
<SignInScreen navigation={navigation} route={route} />
</AuthContext.Provider>,
);
const passwordInput = getByPlaceholderText('Password');
fireEvent(passwordInput, 'onChangeText', password);
const signInButton = getByTestId('signIn-button');
fireEvent(signInButton, 'press');
expect(navigation.navigate).not.toHaveBeenCalledWith('User');
expect(findByText('User does not exist.')).toBeDefined();
});
});
请记住,我使用的是 React.Context API,这就是您看到 AuthContext.Provider 包装器的原因。