score:5

Accepted answer
import React from 'react';

const VALID_LANGS = ['en', 'fr'];

const Component = React.createClass({
  propTypes: {
    lang: React.PropTypes.oneOf(VALID_LANGS)
  },
  getDefaultProps() {
    return {
      lang: 'en'
    };
  },
  render() {
    return (
      <h1>
        lang: {this.getLang()}
      </h1>
    );
  },
  getLang() {
    return VALID_LANGS.indexOf(this.props.lang) !== -1 ? this.props.lang : VALID_LANGS[0];
  }
});

export default Component;

score:0

import assert from 'assert';

import React from 'react';
import {mount} from 'enzyme';

import Component from './Component';

describe('<Component />', () => {
  it('get default prop', () => {
    const wrapper = mount(<Component />);
    assert.equal(wrapper.state('lang'), 'en')
  });
  it('get valid prop', () => {
    const wrapper = mount(<Component lang="fr"/>);
    assert.equal(wrapper.state('lang'), 'fr')
  });
  it('get invalid prop', () => {
    const wrapper = mount(<Component lang="it"/>);
    assert.equal(wrapper.state('lang'), 'en')
  });
});

Related Query

More Query from same tag