Fakultas Ilmu Komputer UI

ProfilePage-test.jsx 6.53 KB
Newer Older
1
/* eslint-disable no-unused-expressions */
sirinbaisa's avatar
sirinbaisa committed
2
import React from 'react';
Syahrul Findi's avatar
Syahrul Findi committed
3
import ReactTestUtils from 'react-dom/test-utils';
4
import fetchMock from 'fetch-mock';
5
import TopMenu from '../components/TopMenu';
sirinbaisa's avatar
sirinbaisa committed
6
import ProfilePage from '../ProfilePage';
7
8
import Storage from '../lib/Storage';
import { studentSession, studentUser, companyUser, response, response2 } from '../object/Response';
sirinbaisa's avatar
sirinbaisa committed
9

Kristianto's avatar
Kristianto committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const studentUserVerified = {
  role: 'student',
  data: {
    url: 'http://localhost:8000/api/users/9/',
    username: 'muhammad.reza42',
    email: 'muhammad.reza42@ui.ac.id',
    is_staff: false,
    company: null,
    supervisor: null,
    student: {
      id: 3,
      user: {
        url: 'http://localhost:8000/api/users/9/',
        username: 'muhammad.reza42',
        email: 'muhammad.reza42@ui.ac.id',
        is_staff: false,
      },
      name: 'Muhammad R.',
      created: '2017-03-28T13:33:46.147241Z',
      updated: '2017-03-28T13:33:46.148248Z',
      npm: 1406543593,
      resume: null,
      phone_number: null,
Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
33
      sertifikat:'ada',
Kristianto's avatar
Kristianto committed
34
35
36
37
38
39
40
41
42
43
44
      bookmarked_vacancies: [
        3,
      ],
      applied_vacancies: [
        3,
        1,
      ],
    },
  },
};

Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
45
46
47
describe('Sertifikat doesnt exists', () => {
  it('render sertifikat doesnt exists', () => {
    studentUserVerified.data.sertifikat = null;
Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
48
49
50
51
52
53
54
    const sertifikat = ReactTestUtils.renderIntoDocument(
        <p>Sertifikat belum ada</p>,
    );
    expect(sertifikat).to.exist;
  });
});

Yumna Pratista Tastaftian's avatar
Yumna Pratista Tastaftian committed
55
56
57
58
59
60
61
62
63
describe('Sertifikat exists', () => {
  it('render sertifikat exists', () => {
    const sertifikat = ReactTestUtils.renderIntoDocument(
        <p>Sertifikat sudah ada</p>,
    );
    expect(sertifikat).to.exist;
  });
});

sirinbaisa's avatar
sirinbaisa committed
64
65
describe('ProfilePage', () => {
  it('renders without problem', () => {
66
    fetchMock.get('*', response);
67
    const profile = ReactTestUtils.renderIntoDocument(
68
69
70
71
72
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }} params={{}}
      />,
    );
73
74
    profile.getProfile().then(() => expect(profile.state.name).to.equal(response.name));
    profile.getProfile().then(() => expect(profile.state.birth_date).to.equal('24 Februari 1995'));
Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
75
    // profile.updateForm(true);
76
    fetchMock.restore();
sirinbaisa's avatar
sirinbaisa committed
77
  });
78

79
  it('get profile for company without problem', () => {
80
81
    fetchMock.get('*', response);
    const profile = ReactTestUtils.renderIntoDocument(
82
83
84
85
86
87
      <ProfilePage
        route={{ own: false, data: studentSession }}
        user={{ data: companyUser }}
        params={{ id: 3 }}
      />,
    );
88
89
    profile.getProfile().then(()=> expect(profile.state.name).to.equal(response.name));
    fetchMock.restore();
90
91
  });

92
93
94
  it('get profile for company without problem', () => {
    fetchMock.get('*', response2);
    const profile = ReactTestUtils.renderIntoDocument(
95
96
97
98
99
100
      <ProfilePage
        route={{ own: false, data: studentSession }}
        user={{ data: companyUser }}
        params={{ id: 3 }}
      />,
    );
101
102
103
104
    profile.getProfile().then(()=> expect(profile.state.name).to.equal(response2.name));
    fetchMock.restore();
  });

Kristianto's avatar
Kristianto committed
105
106
107
  it('get student profile without problem', () => {
    fetchMock.get('*', response);
    const profile = ReactTestUtils.renderIntoDocument(
108
109
110
111
112
113
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }}
        params={{}}
      />,
    );
Kristianto's avatar
Kristianto committed
114
115
116
117
118
119
    profile.getProfile().then(()=> {
      expect(profile.state.name).to.equal(studentUserVerified.data.student.name);
      expect(profile.state.npm).to.equal(studentUserVerified.data.student.npm);
      expect(profile.state.birth_date).to.equal(studentUserVerified.data.student.birth_date);
      expect(profile.state.major).to.equal(studentUserVerified.data.student.major);
      expect(profile.state.batch).to.equal(studentUserVerified.data.student.batch);
120
121
122
123
      expect(profile.state.bookmarked_vacancies).to.equal(
        studentUserVerified.data.student.bookmarked_vacancies);
      expect(profile.state.applied_vacancies).to.equal(
        studentUserVerified.data.student.applied_vacancies);
Kristianto's avatar
Kristianto committed
124
125
126
    });
  })

Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
127
128
129
  it('renders edit profile default value', () => {
    fetchMock.get('*', response);
    const profile = ReactTestUtils.renderIntoDocument(
130
131
132
133
134
135
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }}
        params={{}}
      />,
    );
Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
136
    const phoneNumberNode = ReactTestUtils.scryRenderedDOMComponentsWithTag(profile, 'Input')[2];
137
138
    profile.getProfile().then(()=> expect(
      profile.state.phone_number).to.equal(phoneNumberNode.value));
Nixi Sendya Putri's avatar
Nixi Sendya Putri committed
139
    // profile.updateForm(true);
Hema Mitta Kalyani's avatar
Hema Mitta Kalyani committed
140
141
142
    fetchMock.restore();
  });

143
144
145
  it('renders without problem when error getting data', () => {
    fetchMock.get('*', 400);
    const profile = ReactTestUtils.renderIntoDocument(
146
147
148
149
150
151
      <ProfilePage
        route={{ own: false, data: studentSession }}
        user={{ data: studentSession }}
        params={{ id: 3 }}
      />,
    );
152
    profile.getProfile().then(()=> expect(profile.state.name).to.equal('Gagal mendapatkan informasi'));
153
  });
154

155
156
157
158
  it('handle file input without problem', () => {
    fetchMock.patch('*', { data: 'value' });
    fetchMock.get('*', response);
    const profile = ReactTestUtils.renderIntoDocument(
159
160
161
162
163
164
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }}
        params={{ id: 3 }}
      />,
    );
165

166
    ReactTestUtils.scryRenderedDOMComponentsWithTag(profile, 'Input')[0];
167
168
169
170
    // const passwordNode = ReactDOM.findDOMNode(formLogin.refs.password);
    profile.handleFile({ target: { name: 'photo', files: ['abc', 'def'] } });
    expect(profile.state.form.photo).to.equal('abc');
  });
171

172
173
174
  it('renders count application without problem', () => {
    fetchMock.get('*', response);
    const profile = ReactTestUtils.renderIntoDocument(
175
176
177
178
179
180
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }}
        params={{}}
      />,
    );
181
182
183
184
    profile.getProfile().then(()=> expect(profile.state.acceptedNo).to.not.equal(''));
    profile.getProfile().then(()=> expect(profile.state.readNo).to.not.equal(''));
    fetchMock.restore();
  });
185

186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
  it('sets top menu active item according to page', () => {
    fetchMock.get('*', response);
    ReactTestUtils.renderIntoDocument(
      <ProfilePage
        route={{ own: true, data: studentSession }}
        user={{ data: studentSession }}
        params={{}}
      />,
    );
    const topmenu = ReactTestUtils.renderIntoDocument(
      <TopMenu user={studentUser}>
        <div> test </div>
      </TopMenu>,
    );
    expect(topmenu.state.activeItem).to.equal('Profil');
    fetchMock.restore();
  });
203
});