Fakultas Ilmu Komputer UI

index.test.tsx 2.5 KB
Newer Older
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
import React from 'react';
import { render, fireEvent, waitFor } from 'utils/testing';
import axios from 'axios';

import * as ROUTES from 'constants/routes';
import ChoosePlan from '.';

jest.mock('axios');

describe('ChoosePlan', () => {
  const nutritionists = [
    {
      id: 1,
      full_name_and_degree: 'Wendy',
      registration_certificate_no: '123',
      university: 'UI',
      mastered_nutritional_problems: 'diet',
      handled_age_group: '18',
      another_practice_place: '-',
      languages: 'English',
    },
  ];
  const retrieveNutritionistsApi = () =>
    Promise.resolve({
      status: 200,
      data: nutritionists,
    });

  const mockAxios = axios as jest.Mocked<typeof axios>;
  mockAxios.request.mockImplementation(retrieveNutritionistsApi);

  it('redirects to cart page when user clicks Bayar button', async () => {
    const { getByText } = render(<ChoosePlan />, ROUTES.choosePlan);
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

    const payButton = getByText(/Bayar/i);
    expect(payButton).toBeTruthy();
    await waitFor(() => fireEvent.press(payButton));

    const cartPage = getByText(/Coming Soon/i);
    expect(cartPage).toBeTruthy();
  });

  it('redirects to program detail page when user clicks Baca selengkapnya button for program', async () => {
49
    const { queryAllByText } = render(<ChoosePlan />, ROUTES.choosePlan);
50
51
    await waitFor(() => expect(mockAxios.request).toBeCalled());

52
53
54
    const readMoreButton = queryAllByText(/Baca selengkapnya/i)[0];
    expect(readMoreButton).toBeFalsy();
    // fireEvent.press(readMoreButton);
55

56
57
    // const programDetailPage = queryByText(/Coming Soon/i);
    // expect(programDetailPage).toBeFalsy();
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
  });

  it('redirects to nutritionist detail page when user clicks Baca selengkapnya button for nutritionist', async () => {
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

    const readMoreButton = getAllByText(/Baca selengkapnya/i)[0];
    expect(readMoreButton).toBeTruthy();
    fireEvent.press(readMoreButton);

    const nutritionistDetailPage = getByText(/Coming Soon/i);
    expect(nutritionistDetailPage).toBeTruthy();
  });

  afterAll(() => {
    jest.clearAllMocks();
  });
});