Fakultas Ilmu Komputer UI

index.test.tsx 5.49 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
8
import { mockProgramRecommendations } from 'mocks/quizResult';
import { dietPrograms } from 'constants/dietelaProgram';
9
import { defaultProgramRecommendations } from 'constants/dietelaProgram';
10
11

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

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

33
34
35
36
37
38
39
40
41
  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();
  });
42
43

  it('redirects to cart page when user clicks Bayar button', async () => {
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
    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,
    );
61
62
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

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

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

    mockAxios.request.mockImplementationOnce(createCartApi);

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

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

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
121
122
  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();
  });

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

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

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

137
138
139
140
    const program = mockProgramRecommendations.priority_1;
    const programDetails = dietPrograms[program].details;

    const programDetailPage = getByText(programDetails.description);
141
    expect(programDetailPage).toBeTruthy();
142
143
144
  });

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

147
148
149
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
150
      defaultProgramRecommendations,
151
152
153
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

158
159
160
161
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

162
163
164
    const nutritionistPage = getByText(/Nutrisionis/i);
    expect(nutritionistPage).toBeTruthy();

165
166
167
168
169
170
171
172
173
174
175
176
    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();
  });
});