Fakultas Ilmu Komputer UI

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

import * as ROUTES from 'constants/routes';
import ChoosePlan from '.';
7
import { defaultProgramRecommendations } from 'constants/dietelaProgram';
8
9

jest.mock('axios');
10
const mockAxios = axios as jest.Mocked<typeof axios>;
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

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,
    });

31
32
33
34
35
36
37
38
39
  it('provides default program recommendations if not passed', async () => {
    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

    const { getAllByText } = render(<ChoosePlan />, ROUTES.choosePlan);
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const defaultProgram = getAllByText(/One Week Trial/i);
    expect(defaultProgram).toBeTruthy();
  });
40
41

  it('redirects to cart page when user clicks Bayar button', async () => {
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    const createCartApi = () =>
      Promise.resolve({
        status: 200,
        data: {
          id: 1,
          program: { id: 1 },
          nutritionist: nutritionists[0],
        },
      });

    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

    const { getByText, queryByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
      defaultProgramRecommendations,
    );
59
60
    await waitFor(() => expect(mockAxios.request).toBeCalled());

61
62
63
64
    const selectProgramButton = getByText(/Pilih One Week Trial/i);
    expect(selectProgramButton).toBeTruthy();
    fireEvent.press(selectProgramButton);

65
66
67
68
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

69
70
71
72
73
74
    const selectNutritionistButton = getByText(/Pilih Wendy/i);
    expect(selectNutritionistButton).toBeTruthy();
    fireEvent.press(selectNutritionistButton);

    mockAxios.request.mockImplementationOnce(createCartApi);

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

79
    const cartPage = queryByText(/Coming Soon/i);
80
81
82
    expect(cartPage).toBeTruthy();
  });

83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  it('does not redirect to cart page when submit fails', async () => {
    const createCartApi = () =>
      Promise.reject({
        status: 400,
        data: 'error',
      });

    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

    const { getByText, queryByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
      defaultProgramRecommendations,
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

    const selectProgramButton = getByText(/Pilih One Week Trial/i);
    expect(selectProgramButton).toBeTruthy();
    fireEvent.press(selectProgramButton);

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

    const selectNutritionistButton = getByText(/Pilih Wendy/i);
    expect(selectNutritionistButton).toBeTruthy();
    fireEvent.press(selectNutritionistButton);

    mockAxios.request.mockImplementationOnce(createCartApi);

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

    const cartPage = queryByText(/Coming Soon/i);
    expect(cartPage).toBeFalsy();
  });

121
  it('redirects to program detail page when user clicks Baca selengkapnya button for program', async () => {
122
123
    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

124
125
126
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
127
      defaultProgramRecommendations,
128
    );
129
130
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

135
136
    const programDetailPage = getByText(/Coming Soon/i);
    expect(programDetailPage).toBeTruthy();
137
138
139
  });

  it('redirects to nutritionist detail page when user clicks Baca selengkapnya button for nutritionist', async () => {
140
141
    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

142
143
144
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
145
      defaultProgramRecommendations,
146
147
148
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

149
150
151
152
    const selectProgramButton = getByText(/Pilih One Week Trial/i);
    expect(selectProgramButton).toBeTruthy();
    fireEvent.press(selectProgramButton);

153
154
155
156
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

157
158
159
    const nutritionistPage = getByText(/Nutrisionis/i);
    expect(nutritionistPage).toBeTruthy();

160
161
162
163
164
165
166
167
168
169
170
171
    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();
  });
});