zoukankan      html  css  js  c++  java
  • [React Testing] Use Generated Data in Tests with tests-data-bot to Improve Test Maintainability

    A really important aspect of TDD is the refactor phase. A critical piece to making your tests easier to maintain is using code structure and values to communicate what is important and what is not. We’ll use data generation with test-data-bot to communicate that the specific data values are irrelevant and it’s just what kind of data it is. Let’s refactor our tests to communicate this effectively.

    Install:

    yarn add test-data-bot

    Test:

    import React from 'react'
    import { render, fireEvent, waitFor } from '@testing-library/react'
    import { Redirect as MockRedirect } from 'react-router'
    import { savePost as mockSavePost } from '../extra/api'
    import { Editor } from '../extra/redirect'
    import '@testing-library/jest-dom/extend-expect'
    import {build, fake, sequence} from 'test-data-bot'
    
    // Mock Router redirect
    jest.mock('react-router', () => {
      return {
        Redirect: jest.fn(() => null),
      }
    })
    
    jest.mock('../extra/api')
    
    afterEach(() => {
      jest.clearAllMocks()
    })
    
    const postBuilder = build('Post').fields({
        title: fake(f => f.lorem.words()),
        content: fake(f => f.lorem.paragraphs().replace(/
    /g, '')),
        tags: fake(f => [f.lorem.word(), f.lorem.word(), f.lorem.word()]),
      })
      
      const userBuilder = build('User').fields({
        id: sequence(s => `user-${s}`),
      })
    
    test('renders a form with title, content, tags, and a submit button', async () => {
      mockSavePost.mockResolvedValueOnce()
      const fakeUser = userBuilder()
      const { getByLabelText, getByText } = render(<Editor user={fakeUser} />)
      const fakePost = postBuilder()
      getByLabelText(/title/i).value = fakePost.title
      getByLabelText(/content/i).value = fakePost.content
      getByLabelText(/tags/i).value = fakePost.tags.join(', ')
      const submitButton = getByText(/submit/i)
    
      fireEvent.click(submitButton)
    
      expect(submitButton).toBeDisabled()
    
      expect(mockSavePost).toHaveBeenCalledWith({
        ...fakePost,
        authorId: fakeUser.id,
      })
      expect(mockSavePost).toHaveBeenCalledTimes(1)
    
      await waitFor(() =>
        expect(MockRedirect).toHaveBeenCalledWith({ to: '/' }, {}),
      )
    })
  • 相关阅读:
    centos6.5-搭建LNMP
    mysql组织结构
    mysql主从复制
    centos6.5-搭建mysql5.7.9
    操作系统的历史
    用户&权限&系统
    awk是全局周期
    在vm上面安装Linux系统
    Linux rpm yum 等安装软件
    linux中execve函数的用法
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12819247.html
Copyright © 2011-2022 走看看