Fakultas Ilmu Komputer UI

CreateVacancy-test.jsx 7.35 KB
Newer Older
1
2
import React from 'react';
import ReactTestUtils from 'react-addons-test-utils';
3
import fetchMock from 'fetch-mock';
4
import moment from 'moment';
5
import CreateVacancy from '../CreateVacancy';
6
7
import Storage from '../lib/Storage';

8
describe('CreateVacancy', () => {
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
  const companySession = {
    url: 'http://localhost:8001/api/users/8/',
    username: 'Tutuplapak',
    email: '',
    is_staff: false,
    company: {
      id: 3,
      user: {
        url: 'http://localhost:8001/api/users/8/',
        username: 'Tutuplapak',
        email: '',
        is_staff: false,
      },
      name: 'Tutuplapak',
      created: '2017-03-28T07:30:10.535000Z',
      updated: '2017-03-28T07:30:10.535000Z',
      description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla aliquet semper neque a fermentum. Duis ac tellus vitae augue iaculis ultrices. Curabitur commodo et neque nec feugiat. Morbi ac diam vel nunc commodo cursus. Phasellus nulla sapien, hendrerit vitae bibendum at, sollicitudin eu ante. Maecenas maximus, ante eu sollicitudin convallis, mauris nunc posuere risus, eu porttitor diam lacus vitae enim. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Suspendisse at lectus a elit sollicitudin tempor. Nullam condimentum, justo nec tincidunt maximus, neque mi vulputate leo, sit amet lacinia massa ex eget sem. Duis ac erat facilisis, fringilla mauris in, consequat neque. In et neque consequat, vehicula magna at, efficitur ante. Mauris ac lacinia nibh.\r\n\r\nProin sagittis, lectus quis maximus varius, libero justo sollicitudin augue, non lacinia risus orci a enim. Curabitur iaculis enim quis ullamcorper commodo. Vivamus id nisi rhoncus, dignissim tellus quis, interdum est. Fusce sollicitudin eu libero ac feugiat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Maecenas semper posuere ex, sed accumsan libero iaculis faucibus. Fusce laoreet ac ligula ut consectetur. Donec tortor mauris, rutrum at sodales et, viverra in dolor. Sed bibendum elit et maximus volutpat. Phasellus justo ipsum, laoreet sit amet faucibus eu, ultricies suscipit mauris. Nullam aliquam libero eu ante ultrices mattis. Donec non justo hendrerit neque volutpat placerat. Ut euismod est nec sem mollis, sit amet porttitor massa rhoncus. Aenean id erat sit amet nunc ultrices scelerisque non in ipsum. Curabitur sollicitudin nulla id mi accumsan venenatis.',
      verified: true,
      logo: 'http://localhost:8001/files/company-logo/8a258a48-3bce-4873-b5d1-538b360d0059.png',
      address: 'Jl. Kebayoran Baru nomor 13, Jakarta Barat',
    },
    supervisor: null,
    student: null,
  };

  const errorSession = {
    url: 'http://localhost:8001/api/users/8/',
    username: 'Tutuplapak',
    email: '',
    is_staff: false,
    company: null,
    supervisor: null,
    student: null,
  };

  it('renders for companies without problem', () => {
45
    fetchMock.get('*', {});
46
    Storage.set('user-data', companySession);
47
    const createVacancy = ReactTestUtils.renderIntoDocument(
48
      <CreateVacancy params={{ id: 1 }} user={{ data: { company: {} } }} />,
49
50
    );
    expect(createVacancy).to.exist;
51
    fetchMock.restore();
52
53
54
55
  });


  it('renders without problem for error case', () => {
56
    fetchMock.get('*', {});
57
    Storage.set('user-data', errorSession);
58
    const createVacancy = ReactTestUtils.renderIntoDocument(
59
      <CreateVacancy params={{ id: 1 }} user={{ data: { company: {} } }} />,
60
61
    );
    expect(createVacancy).to.exist;
62
    fetchMock.restore();
63
64
65
  });

  it('support handle change', () => {
66
    fetchMock.get('*', {});
67
68
    Storage.set('user-data', companySession);
    const createVacancy = ReactTestUtils.renderIntoDocument(
69
      <CreateVacancy params={{ id: 1 }} user={{ data: { company: {} } }} />,
70
71
72
73
74
75
76
    );
    createVacancy.setState({
      name: 'stub',
      description: 'stub',
    });
    createVacancy.handleChange({ target: { name: 'test', value: 'hue' } });
    expect(createVacancy.state.test).to.equal('hue');
77
    fetchMock.restore();
78
79
80
81
  });

  it('submit vacancy properly (loading)', () => {
    fetchMock.post('*', 404);
82
    fetchMock.get('*', {});
83
84
    Storage.set('user-data', companySession);
    const createVacancy = ReactTestUtils.renderIntoDocument(
85
      <CreateVacancy params={{ id: undefined }} user={{ data: { company: {} } }} />,
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
    );
    createVacancy.setState({
      name: 'stub',
      description: 'stub',
    });

    const openField = ReactTestUtils.findRenderedDOMComponentWithClass(createVacancy, 'open-time-field');
    const closeField = ReactTestUtils.findRenderedDOMComponentWithClass(createVacancy, 'close-time-field');
    ReactTestUtils.Simulate.click(openField);
    ReactTestUtils.Simulate.keyDown(openField, { key: 'Enter', keyCode: 13, which: 13 });
    ReactTestUtils.Simulate.click(closeField);
    ReactTestUtils.Simulate.keyDown(closeField, { key: 'Enter', keyCode: 13, which: 13 });

    expect(createVacancy.state.formLoading).to.equal(false);
    createVacancy.handleSubmit(new Event('click'));
    expect(createVacancy.state.formLoading).to.equal(true);
    fetchMock.restore();
  });

  it('submit vacancy properly (success)', () => {
    fetchMock.post('*', { data: 'value' });
107
108
109
    fetchMock.get('*', {});
    Storage.set('user-data', companySession);
    const createVacancy = ReactTestUtils.renderIntoDocument(
110
      <CreateVacancy params={{ id: undefined }} user={{ data: { company: {} } }} />,
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    );
    createVacancy.setState({
      name: 'stub',
      description: 'stub',
    });

    expect(createVacancy.state.formLoading).to.equal(false);
    createVacancy.handleSubmit(new Event('click'));
    expect(createVacancy.state.formLoading).to.equal(true);
    fetchMock.restore();
  });

  it('submit vacancy properly (loading)', () => {
    fetchMock.patch('*', 404);
    fetchMock.get('*', {});
    Storage.set('user-data', companySession);
    const createVacancy = ReactTestUtils.renderIntoDocument(
128
      <CreateVacancy params={{ id: 1 }} user={{ data: { company: {} } }} />,
129
130
131
132
133
134
135
136
137
138
139
140
    );
    createVacancy.setState({
      name: 'stub',
      description: 'stub',
    });

    const openField = ReactTestUtils.findRenderedDOMComponentWithClass(createVacancy, 'open-time-field');
    const closeField = ReactTestUtils.findRenderedDOMComponentWithClass(createVacancy, 'close-time-field');
    ReactTestUtils.Simulate.click(openField);
    ReactTestUtils.Simulate.keyDown(openField, { key: 'Enter', keyCode: 13, which: 13 });
    ReactTestUtils.Simulate.click(closeField);
    ReactTestUtils.Simulate.keyDown(closeField, { key: 'Enter', keyCode: 13, which: 13 });
141
142
    createVacancy.setOpenTime(moment());
    createVacancy.setCloseTime(moment());
143
144
145
146
147
148
149
150
151
152

    expect(createVacancy.state.formLoading).to.equal(false);
    createVacancy.handleSubmit(new Event('click'));
    expect(createVacancy.state.formLoading).to.equal(true);
    fetchMock.restore();
  });

  it('submit vacancy properly (success)', () => {
    fetchMock.patch('*', { data: 'value' });
    fetchMock.get('*', {});
153
154
    Storage.set('user-data', companySession);
    const createVacancy = ReactTestUtils.renderIntoDocument(
155
      <CreateVacancy params={{ id: 1 }} user={{ data: { company: {} } }} />,
156
157
158
159
160
161
162
163
164
165
    );
    createVacancy.setState({
      name: 'stub',
      description: 'stub',
    });

    expect(createVacancy.state.formLoading).to.equal(false);
    createVacancy.handleSubmit(new Event('click'));
    expect(createVacancy.state.formLoading).to.equal(true);
    fetchMock.restore();
166
167
  });
});