#
luxiaotao1123
2024-02-06 e9293e07cd386b1cdca525f7c80683f146caa96d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { render, fireEvent, act } from '@testing-library/react';
import React from 'react';
import { TestBrowser } from '@@/testBrowser';
 
// @ts-ignore
import { startMock } from '@@/requestRecordMock';
 
const waitTime = (time: number = 100) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(true);
    }, time);
  });
};
 
let server: {
  close: () => void;
};
 
describe('Login Page', () => {
  beforeAll(async () => {
    server = await startMock({
      port: 8000,
      scene: 'login',
    });
  });
 
  afterAll(() => {
    server?.close();
  });
 
  it('should show login form', async () => {
    const historyRef = React.createRef<any>();
    const rootContainer = render(
      <TestBrowser
        historyRef={historyRef}
        location={{
          pathname: '/user/login',
        }}
      />,
    );
 
    await rootContainer.findAllByText('Ant Design');
 
    act(() => {
      historyRef.current?.push('/user/login');
    });
 
    expect(rootContainer.baseElement?.querySelector('.ant-pro-form-login-desc')?.textContent).toBe(
      'Ant Design is the most influential web design specification in Xihu district',
    );
 
    expect(rootContainer.asFragment()).toMatchSnapshot();
 
    rootContainer.unmount();
  });
 
  it('should login success', async () => {
    const historyRef = React.createRef<any>();
    const rootContainer = render(
      <TestBrowser
        historyRef={historyRef}
        location={{
          pathname: '/user/login',
        }}
      />,
    );
 
    await rootContainer.findAllByText('Ant Design');
 
    const userNameInput = await rootContainer.findByPlaceholderText('Username: admin or user');
 
    act(() => {
      fireEvent.change(userNameInput, { target: { value: 'admin' } });
    });
 
    const passwordInput = await rootContainer.findByPlaceholderText('Password: ant.design');
 
    act(() => {
      fireEvent.change(passwordInput, { target: { value: 'ant.design' } });
    });
 
    await (await rootContainer.findByText('Login')).click();
 
    // 等待接口返回结果
    await waitTime(5000);
 
    await rootContainer.findAllByText('Ant Design Pro');
 
    expect(rootContainer.asFragment()).toMatchSnapshot();
 
    await waitTime(2000);
 
    rootContainer.unmount();
  });
});