Skip to content

Commit

Permalink
Fixed test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
patw0929 committed Jun 22, 2016
1 parent 23ad632 commit 2c9ece7
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 0 deletions.
23 changes: 23 additions & 0 deletions tests/FlagDropDown-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ describe('FlagDropDown', () => {
window.localStorage.clear();
});

it('separateDialCode', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
separateDialCode
/>
);

assert(findDOMNode(parent).className.indexOf('separate-dial-code') > -1);
});

it('Set flag className by country', () => {
assert(findDOMNode(flagComponent).querySelector('.iti-flag').className === 'iti-flag tw');
});
Expand Down Expand Up @@ -113,6 +125,17 @@ describe('FlagDropDown', () => {
assert.deepEqual(parent.refs.flagDropDown.props.countries, result);
});

it('Set excludeCountries', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
excludeCountries={['us', 'kr']}
/>
);
assert(parent.refs.flagDropDown.props.countries.length === 241);
});

it('Set defaultCountry as "auto"', () => {
const lookup = (callback) => {
callback('jp');
Expand Down
42 changes: 42 additions & 0 deletions tests/RootModal-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* eslint-disable no-eval */
import React from 'react';
import ReactDOM, { findDOMNode } from 'react-dom';
import ReactTestUtils from 'react-addons-test-utils';
import RootModal from '../src/components/RootModal';
import { assert } from 'chai';

describe('RootModal', () => {
let renderedComponent;

beforeEach('Render element', () => {
renderedComponent = ReactTestUtils.renderIntoDocument(
<RootModal>
<div>foo</div>
</RootModal>
);
});

afterEach(() => {
if (renderedComponent) {
ReactDOM.unmountComponentAtNode(
ReactDOM.findDOMNode(renderedComponent).parentNode);
}
});

it('is a noscript tag', () => {
assert(findDOMNode(renderedComponent).tagName.toLowerCase() === 'noscript');
});

it('has parent element which has specific className"', () => {
assert(renderedComponent.modalTarget.classList[0] === 'intl-tel-input');
assert(renderedComponent.modalTarget.classList[1] === 'iti-container');
});

it('has a modalTarget in body', () => {
renderedComponent.setState({
foo: 'foo',
});
assert(renderedComponent.modalTarget.classList[0] === 'intl-tel-input');
assert(renderedComponent.modalTarget.classList[1] === 'iti-container');
});
});
106 changes: 106 additions & 0 deletions tests/TelInput-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,22 @@ describe('TelInput', () => {
assert(parent.state.countryCode === 'af');
});

it('getNumber without utilsScript', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
/>
);

assert(parent.getNumber(1) === '');
});

it('setNumber', () => {
renderedComponent.setNumber('+810258310015');
assert(renderedComponent.state.countryCode === 'jp');
});

it('handleKeyUp', () => {
requests[0].respond(200,
{ 'Content-Type': 'text/javascript' },
Expand Down Expand Up @@ -276,4 +292,94 @@ describe('TelInput', () => {
ReactTestUtils.Simulate.change(findDOMNode(input), { target: { value: '+886911222333' } });
assert(expected === 'true,+886911222333,tw,+886 911 222 333,null');
});

it('Blur and cleaning the empty dialcode', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
utilsScript={'../example/assets/libphonenumber.js'}
/>
);

const input = ReactTestUtils.findRenderedComponentWithType(
parent,
TelInput
);

ReactTestUtils.Simulate.change(findDOMNode(input), { target: { value: '+886' } });
parent.handleOnBlur();
assert(parent.state.value === '');
});

it('has empty value and not nationalMode, not autoHideDialCode and not separateDialCode', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
nationalMode={false}
autoHideDialCode={false}
separateDialCode={false}
utilsScript={'../example/assets/libphonenumber.js'}
/>
);

assert(parent.state.value === '+886');
});

it('updateFlagFromNumber', () => {
const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'us'}
utilsScript={'../example/assets/libphonenumber.js'}
nationalMode
/>
);

const input = ReactTestUtils.findRenderedComponentWithType(
parent,
TelInput
);

ReactTestUtils.Simulate.change(findDOMNode(input), { target: { value: '9183319436' } });
assert(parent.state.countryCode === 'us');

ReactTestUtils.Simulate.change(findDOMNode(input), { target: { value: '+' } });
assert(parent.state.countryCode === 'us');
});

it('isValidNumber', () => {
requests[0].respond(200,
{ 'Content-Type': 'text/javascript' },
libphonenumberUtils);
window.eval(getScript().text);

assert(renderedComponent.isValidNumber('0910123456') === true);
assert(renderedComponent.isValidNumber('091012345') === false);
});

it('getFullNumber', () => {
requests[0].respond(200,
{ 'Content-Type': 'text/javascript' },
libphonenumberUtils);
window.eval(getScript().text);

const parent = ReactTestUtils.renderIntoDocument(
<IntlTelInput css={['intl-tel-input', 'form-control phoneNumber']}
fieldName={'telephone'}
defaultCountry={'tw'}
utilsScript={'../example/assets/libphonenumber.js'}
separateDialCode
/>
);

const input = ReactTestUtils.findRenderedComponentWithType(
parent,
TelInput
);

ReactTestUtils.Simulate.change(findDOMNode(input), { target: { value: '910123456' } });
assert(parent.getFullNumber() === '+886910123456');
});
});
14 changes: 14 additions & 0 deletions tests/utils-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe('utils', () => {
b = null;
assert(utils.arraysEqual(a, b) === false);

a = [1, 2, 3];
b = [2, 1 ,4 ,5];
assert(utils.arraysEqual(a, b) === false);

a = ['1', '2', '3'];
b = ['3', '1', '2'];
assert(utils.arraysEqual(a, b) === false);
Expand All @@ -33,6 +37,16 @@ describe('utils', () => {
};
assert(utils.shallowEquals(a, b) === true);

a = {
x: ['1', '2', '3'],
y: ['1', '3', '4'],
};
b = {
x: ['1', '2', '3'],
y: ['4', '2'],
};
assert(utils.shallowEquals(a, b) === false);

a = {
a: 1,
b: 2,
Expand Down

0 comments on commit 2c9ece7

Please sign in to comment.