Fakultas Ilmu Komputer UI

index.test.tsx 2.51 KB
Newer Older
1
import React from 'react';
2
import { render, fireEvent, waitFor } from 'utils/testing';
3
import * as ROUTES from 'constants/routes';
4
import axios from 'axios';
5
6

import Login from '.';
Wulan Mantiri's avatar
Wulan Mantiri committed
7
import { authResponse, validLoginValues } from 'mocks/auth';
8
9
10
11
12
import { textField } from './schema';

jest.mock('react-native-toast-message');
jest.mock('axios');
const mockAxios = axios as jest.Mocked<typeof axios>;
13
14

describe('Login page', () => {
15
16
17
18
19
20
  it('renders correctly if client has filled questionnaire and cart', async () => {
    const { queryByText } = render(<Login />, ROUTES.login);

    await waitFor(() =>
      expect(queryByText(/Lanjut dengan Google/i)).toBeTruthy(),
    );
21
  });
22
23
24
25
26
27
28
29
30

  it('success when field is valid and submit success', async () => {
    const loginApi = () =>
      Promise.resolve({
        status: 201,
        data: authResponse,
      });
    mockAxios.request.mockImplementationOnce(loginApi);

31
    const { getByPlaceholderText, getByTestId } = render(
32
33
34
35
36
37
38
39
40
41
42
43
44
      <Login />,
      ROUTES.login,
    );

    textField.map(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validLoginValues[name]);
    });

    const loginButton = getByTestId('loginButton');
    await waitFor(() => fireEvent.press(loginButton));
  });

45
  it('fails when field is valid and submit fails', async () => {
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
    const loginApi = () =>
      Promise.reject({
        status: 400,
        response: {
          data: 'error',
        },
      });
    mockAxios.request.mockImplementationOnce(loginApi);

    const { getByPlaceholderText, queryByText, getByTestId } = render(
      <Login />,
      ROUTES.login,
    );

    textField.map(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
62
      fireEvent.changeText(formField, validLoginValues[name]);
63
64
65
66
67
68
69
70
71
72
73
74
    });

    const loginButton = getByTestId('loginButton');
    await waitFor(() => fireEvent.press(loginButton));

    const toastWarning = queryByText(/Profile/i);
    expect(toastWarning).toBeFalsy();
  });

  afterAll(() => {
    jest.clearAllMocks();
  });
75
76
77
78
79
80
81
82
83
84
85
86
87
88

  test('has link button that navigates to Nutritionist/Admin Login Page', async () => {
    const { getByText, queryByText, queryAllByText } = render(
      <Login />,
      ROUTES.login,
    );

    expect(queryByText(/Login sebagai Nutrisionis/i)).toBeTruthy();
    await waitFor(() =>
      fireEvent.press(getByText(/Login sebagai Nutrisionis/i)),
    );

    expect(queryAllByText(/Login Tim Dietela/i)).toBeTruthy();
  });
89
});