Fakultas Ilmu Komputer UI

LoginForm-test.jsx 3 KB
Newer Older
1
/* eslint-disable no-unused-expressions */
2
import React from 'react';
Syahrul Findi's avatar
Syahrul Findi committed
3
import ReactTestUtils from 'react-dom/test-utils';
4
import LoginForm from '../../components/LoginForm';
5

6
describe('LoginForm', () => {
7
  const fetchMock = require('fetch-mock');
8
  it('created without problem', () => {
9
    const formLogin = new LoginForm({ url: 'tes' });
10
    expect(formLogin).to.be.an.instanceof(LoginForm);
11
12
13
14
  });

  it('renders without problem', () => {
    const formLogin = ReactTestUtils.renderIntoDocument(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
15
16
      <LoginForm url="" />,
    );
17
18
19
    expect(formLogin).to.exist;

    const formDiv = ReactTestUtils.findRenderedDOMComponentWithClass(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
20
21
      formLogin, 'formLogin',
    );
22
23
24
    expect(formDiv).to.exist;

    const form = ReactTestUtils.findRenderedDOMComponentWithTag(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
25
26
      formLogin, 'form',
    );
27
28
29
30
31
    expect(form).to.exist;
  });

  it('handle email input without problem', () => {
    const formLogin = ReactTestUtils.renderIntoDocument(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
32
33
      <LoginForm type="sso-ui" />,
    );
34
    expect(formLogin.state.username).to.equal('');
35
36
37
38
39
40
41
    const emailNode = ReactTestUtils.scryRenderedDOMComponentsWithTag(formLogin, 'Input')[0];
    // const emailNode = ReactDOM.findDOMNode(formLogin.refs.email);
    const email = 'jojon';
    emailNode.value = email;

    ReactTestUtils.Simulate.change(emailNode, { target: { value: email } });
    expect(emailNode.value).to.equal(email);
42
    expect(formLogin.state.username).to.equal(email);
43
44
  });

45
  it('handle password input without problem', () => {
46
    const formLogin = ReactTestUtils.renderIntoDocument(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
47
48
      <LoginForm type="sso-ui" />,
    );
49
50
51
52
53
54
55
56
57
58
59

    const passwordNode = ReactTestUtils.scryRenderedDOMComponentsWithTag(formLogin, 'Input')[1];
    // const passwordNode = ReactDOM.findDOMNode(formLogin.refs.password);
    const password = 'passwd';
    passwordNode.value = password;
    expect(formLogin.state.password).to.equal('');
    ReactTestUtils.Simulate.change(passwordNode, { target: { value: password } });
    expect(passwordNode.value).to.equal(password);
    expect(formLogin.state.password).to.equal(password);
  });

60
  it('submit form without problem', () => {
61
    fetchMock.post('*', { data: 'value' });
62
    const formLogin = ReactTestUtils.renderIntoDocument(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
63
64
      <LoginForm url="" />,
    );
65

Zulia Putri's avatar
Zulia Putri committed
66
67
68

    const submitButton = ReactTestUtils.findRenderedDOMComponentWithClass(formLogin, 'submit');
    console.log(submitButton);
69
70
71
72
    ReactTestUtils.Simulate.click(submitButton);

    const form = ReactTestUtils.findRenderedDOMComponentWithTag(formLogin, 'Form');
    ReactTestUtils.Simulate.submit(form);
73
74
75
76
77
78
    fetchMock.restore();
  });

  it('submit form with problem', () => {
    fetchMock.post('*', 404);
    const formLogin = ReactTestUtils.renderIntoDocument(
Arga Ghulam Ahmad's avatar
Arga Ghulam Ahmad committed
79
80
      <LoginForm url="" />,
    );
81

Zulia Putri's avatar
Zulia Putri committed
82
    const submitButton = ReactTestUtils.findRenderedDOMComponentWithClass(formLogin, 'submit');
83
84
85
86
87
    ReactTestUtils.Simulate.click(submitButton);

    const form = ReactTestUtils.findRenderedDOMComponentWithTag(formLogin, 'Form');
    ReactTestUtils.Simulate.submit(form);
    fetchMock.restore();
88
89
  });
});