Fakultas Ilmu Komputer UI

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

import AllAccessQuestionnaire from '.';
6
import { allAccessQuestions, textFields } from './schema';
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
49
50
51
52
53
54
55
56
57
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
describe('AllAccessQuestionnaire', () => {
  const validFormValues: { [_: string]: any } = {
    name: 'Dietela',
    email: 'dietela@gmail.com',
    age: '29',
    weight: '82',
    height: '178',
  };

  it('shows biodata form and all required errors after submit with empty form values', async () => {
    const { getByText, queryByText } = render(
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

    allAccessQuestions.forEach(() => {
      const nextButton = getByText(/Lanjut/i);
      expect(nextButton).toBeTruthy();
      fireEvent.press(nextButton);
    });

    const submitButton = getByText('Selesai');
    expect(submitButton).toBeTruthy();
    await waitFor(() => fireEvent.press(submitButton));

    const preQuizHeader = queryByText(/Data Diri/i);
    await waitFor(() => expect(preQuizHeader).toBeTruthy());

    textFields.forEach(({ label }) => {
      const errorMessage = queryByText(`${label} harus diisi`);
      expect(errorMessage).toBeTruthy();
    });

    const genderErrorMessage = queryByText('Pilihan harus diisi');
    expect(genderErrorMessage).toBeTruthy();

    const sampleNextPage = queryByText(/Pertanyaan/i);
    expect(sampleNextPage).toBeFalsy();
  });

  it('shows the foremost page with error after submit', async () => {
    const { getByText, getByPlaceholderText, queryByText } = render(
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

    textFields.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });

    const maleChoice = getByText(/Pria/i);
    fireEvent.press(maleChoice);

    allAccessQuestions.forEach(() => {
      const nextButton = getByText(/Lanjut/i);
      fireEvent.press(nextButton);
    });

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));

    const nextPage = queryByText(/Pertanyaan/i);
    await waitFor(() => expect(nextPage).toBeTruthy());
  });

  it('redirects to initial page if all form values are valid', async () => {
    const { getByText, getByPlaceholderText, queryByText } = render(
      <AllAccessQuestionnaire />,
      ROUTES.allAccessQuestionnaire,
    );

    textFields.forEach(({ name, placeholder }) => {
      const formField = getByPlaceholderText(placeholder as string);
      fireEvent.changeText(formField, validFormValues[name]);
    });

    const maleChoice = getByText(/Pria/i);
    fireEvent.press(maleChoice);

    allAccessQuestions.forEach(({ choiceList }) => {
      const nextButton = getByText(/Lanjut/i);
      fireEvent.press(nextButton);

      const firstChoice = getByText(choiceList[0]);
      fireEvent.press(firstChoice);
    });

    const submitButton = getByText('Selesai');
    await waitFor(() => fireEvent.press(submitButton));

    const initialPage = queryByText(/konsultasi sekarang/i);
    await waitFor(() => expect(initialPage).toBeTruthy());
101
102
  });
});