Fakultas Ilmu Komputer UI

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

import ChoosePlan from '.';
6
7
8

import * as ROUTES from 'constants/routes';
import CACHE_KEYS from 'constants/cacheKeys';
9
import { mockProgramRecommendations } from '__mocks__/quizResult';
10
import { dietPrograms } from 'constants/dietelaProgram';
11
import { setCache } from 'utils/cache';
12
13

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

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

35
  it('provides default program recommendations if not in cache', async () => {
36
37
38
39
40
41
42
43
    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();
  });
44
45

  it('redirects to cart page when user clicks Bayar button', async () => {
46
47
48
49
    await setCache(
      CACHE_KEYS.programRecommendations,
      JSON.stringify(mockProgramRecommendations),
    );
50
51
52
53
54
55
56
57
58
59
60
61
    const createCartApi = () =>
      Promise.resolve({
        status: 200,
        data: {
          id: 1,
          program: { id: 1 },
          nutritionist: nutritionists[0],
        },
      });

    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

62
    const { getByText, queryAllByText } = render(
63
64
65
      <ChoosePlan />,
      ROUTES.choosePlan,
    );
66
67
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

72
73
74
75
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

76
77
78
79
80
81
    const selectNutritionistButton = getByText(/Pilih Wendy/i);
    expect(selectNutritionistButton).toBeTruthy();
    fireEvent.press(selectNutritionistButton);

    mockAxios.request.mockImplementationOnce(createCartApi);

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

86
87
    const checkoutPage = queryAllByText(/Checkout/i);
    expect(checkoutPage).toBeTruthy();
88
89
  });

90
91
92
93
  it('does not redirect to cart page when submit fails', async () => {
    const createCartApi = () =>
      Promise.reject({
        status: 400,
94
95
96
        response: {
          data: 'error',
        },
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
123
124
      });

    mockAxios.request.mockImplementationOnce(retrieveNutritionistsApi);

    const { getByText, queryByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
    );
    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));

125
126
    const checkoutPage = queryByText(/Checkout/i);
    expect(checkoutPage).toBeFalsy();
127
128
  });

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

132
133
134
135
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
    );
136
137
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

142
143
144
145
    const program = mockProgramRecommendations.priority_1;
    const programDetails = dietPrograms[program].details;

    const programDetailPage = getByText(programDetails.description);
146
    expect(programDetailPage).toBeTruthy();
147
148
149
  });

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

152
153
154
155
156
157
    const { getAllByText, getByText } = render(
      <ChoosePlan />,
      ROUTES.choosePlan,
    );
    await waitFor(() => expect(mockAxios.request).toBeCalled());

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

162
163
164
165
    const nextButton = getByText(/Lanjut/i);
    expect(nextButton).toBeTruthy();
    fireEvent.press(nextButton);

166
167
168
    const nutritionistPage = getByText(/Nutrisionis/i);
    expect(nutritionistPage).toBeTruthy();

169
170
171
172
173
174
175
176
177
178
179
180
    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();
  });
});