如何正确测试使用 jest 调用 axios 的 onClick 函数

时间:2021-05-02 21:02:05

标签: reactjs authentication testing axios jestjs

我刚开始使用 jest 进行测试,所以我通过 package PkgMailerPkg; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; //---------------------------------------------------------------------------------------- // To enable GMail to accept email from a less secure app . . . // Go to www.gmail.com // Select LSWeb0247 account // Left click on image // Select "Manage your Google Account" (centered Under email address) // Click on "Security" (on left side of screen) // Scroll down to "Less secure app access" // Ensure it is set to "ON"3 //---------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------- // Send email class. //---------------------------------------------------------------------------------------- public class MailManager { private final static String HOST = "smtp.gmail.com"; private final String user; private final String password; private Properties properties; private final Session session; //------------------------------------------------------------------------------------- // Constructor: // // user = sender email address // password = sender account password //------------------------------------------------------------------------------------- public MailManager(String user, String password) { this.user = user; this.password = password; //---------------------------------------------------------------------------------- // Get system properties //---------------------------------------------------------------------------------- properties = System.getProperties(); //---------------------------------------------------------------------------------- // Setup mail server //---------------------------------------------------------------------------------- properties.put("mail.smtp.host", HOST); properties.put("mail.smtp.port", "465"); properties.put("mail.smtp.ssl.enable", "true"); properties.put("mail.smtp.auth", "true"); //---------------------------------------------------------------------------------- // Get the Session object using GMail account //---------------------------------------------------------------------------------- session = Session.getInstance(properties, new javax.mail.Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(user, password); } }); } //------------------------------------------------------------------------------------- // Parameters: // subject: EMail subject line // to: ArrayList of primary recipients // cc: ArrayList of CC recipients // bcc: ArrayList of BCC recipients // from: Sender address // body: Email body text (HTML allowed) // attachments: ArrayList of files to attach // // Returns: // "OK" - No problem // Error - Something happened //------------------------------------------------------------------------------------- public String sendMail( String subject, ArrayList<String> to, ArrayList<String> cc, ArrayList<String> bcc, String from, String body, ArrayList<String> attachments ) { try { //------------------------------------------------------------------------------- // Create a default MimeMessage object. //------------------------------------------------------------------------------- MimeMessage message = new MimeMessage(session); //------------------------------------------------------------------------------- // Set From: header field of the header. //------------------------------------------------------------------------------- message.setFrom(new InternetAddress(from)); //------------------------------------------------------------------------------- // Set "TO" recipients //------------------------------------------------------------------------------- for (String sendTo : to) { message.addRecipient(Message.RecipientType.TO, new InternetAddress(sendTo)); } //------------------------------------------------------------------------------- // Set "CC" recipients //------------------------------------------------------------------------------- if (cc != null) { for (String ccTo : cc) { message.addRecipient(Message.RecipientType.CC, new InternetAddress(ccTo)); } } //------------------------------------------------------------------------------- // Set "BCC" recipients //------------------------------------------------------------------------------- if (bcc != null) { for (String bccTo : bcc) { message.addRecipient(Message.RecipientType.BCC, new InternetAddress(bccTo)); } } //------------------------------------------------------------------------------- // Set "SUBJECT" //------------------------------------------------------------------------------- message.setSubject(subject); //------------------------------------------------------------------------------- // Add attachments //------------------------------------------------------------------------------- Multipart multipart = new MimeMultipart(); for (String attachment : attachments) { MimeBodyPart attachmentPart = new MimeBodyPart(); File f = new File(attachment); attachmentPart.attachFile(f); multipart.addBodyPart(attachmentPart); } //------------------------------------------------------------------------------- // Add attachments //------------------------------------------------------------------------------- MimeBodyPart messageBodyPart = new MimeBodyPart(); messageBodyPart.setContent(body, "text/html"); multipart.addBodyPart(messageBodyPart); message.setContent(multipart); //------------------------------------------------------------------------------- // Send message //------------------------------------------------------------------------------- Transport.send(message); } catch (MessagingException | IOException ex) { return "MailManager Error: " + ex.getMessage(); } return "OK"; } } 创建了 react 应用程序,现在我正在努力测试功能组件中的登录功能。

  • 点击“登录”按钮后调用此登录函数。
  • 此登录函数首先将 isLoading 状态设置为 true,然后发送 axios 调用以验证凭据。
  • 如果凭据正确 -> 应用程序会将用户重新路由到应用程序中的另一个页面。
  • 如果凭据不正确 -> 应用程序会将 err msg 设置为 state 并将其显示给用户。

我的问题:

  • 我可以以某种方式检查 isLoading 状态是否设置为 true 吗?
  • 我可以检查是否发送了验证凭据的请求吗?
  • 我可以检查(如果凭据正确)用户是否被重新路由?
  • 我可以检查(如果凭据不正确)是否已将 err msg 设置为 state 以及是否已向用户显示 err msg?

我没有发现任何类似的问题,每个地方的 axios 调用都是单独测试的。而且我还发现现在无法检查功能组件中的状态属性。我对吗? 也许我认为测试如何工作是错误的。我看到人们有不同的架构来测试 onClick 函数(函数作为 props 传递给组件),但我对它现在的样子很满意,所以我不想仅仅因为测试目的而改变它。我需要添加其他插件来实现我的目标吗?

我的登录功能组件:

npx create-react-app my-app

谢谢。

0 个答案:

没有答案
相关问题