<commit_before>/* global it expect jest */

import React from 'react';
import { mountWithIntl } from '../../utils/intlEnzymeHelper';

import NewChallenge from '../NewChallenge';

const mockBase = () => ({ post: jest.fn().mockReturnValue(Promise.resolve()) });

it('render a name field', () => {
  const wrapper = mountWithIntl(
    <NewChallenge base={mockBase()} defaultName="Hi" />
  );

  expect(wrapper.find('input[name="name"]').length).toEqual(1);
});

it('triggers onSubmit when submitting', () => {
  const promise = Promise.resolve();
  const base = { post: jest.fn().mockReturnValue(promise) };

  const wrapper = mountWithIntl(
    <NewChallenge base={base} defaultName="Hi" />
  );

  const form = wrapper.find('form');
  const preventDefault = jest.fn();

  form.simulate('submit', { preventDefault });

  expect(preventDefault).toHaveBeenCalled();
  expect(base.post).toHaveBeenCalled();

  return promise.then(() => {
    expect(wrapper.state('challengeId').length).toEqual(16);
  });
});

it('does not submit if the name is blank', () => {
  const base = mockBase();
  const wrapper = mountWithIntl(<NewChallenge base={base} defaultName="  " />);
  const form = wrapper.find('form');
  const preventDefault = jest.fn();

  form.simulate('submit', { preventDefault });

  expect(preventDefault).toHaveBeenCalled();
  expect(base.post).not.toHaveBeenCalled();
});

it('shows an error when the post fails', () => {
  const promise = Promise.reject();
  const base = { post: jest.fn().mockReturnValue(promise) };

  const wrapper = mountWithIntl(
    <NewChallenge base={base} defaultName="Hi" />
  );
<commit_msg>Fix warning in NewChallenge test<commit_after>/* global it expect jest */

import React from 'react';
import { MemoryRouter, Route } from 'react-router-dom';
import { mountWithIntl } from '../../utils/intlEnzymeHelper';

import NewChallenge from '../NewChallenge';

const mockBase = () => ({ post: jest.fn().mockReturnValue(Promise.resolve()) });

it('render a name field', () => {
  const wrapper = mountWithIntl(
    <NewChallenge base={mockBase()} defaultName="Hi" />
  );

  expect(wrapper.find('input[name="name"]').length).toEqual(1);
});

it('triggers onSubmit when submitting', () => {
  const promise = Promise.resolve();
  const base = { post: jest.fn().mockReturnValue(promise) };

  const wrapper = mountWithIntl(
    <MemoryRouter initialEntries={['/new-challenge']}>
      <div>
        <Route
          path="/new-challenge"
          render={() => <NewChallenge base={base} defaultName="Hi" />}
        />
        <Route
          path="/play/:challengeId"
          render={({ match: { params } }) => <em>{params.challengeId}</em>}
        />
      </div>
    </MemoryRouter>
  );

  const form = wrapper.find('form');
  const preventDefault = jest.fn();

  form.simulate('submit', { preventDefault });

  expect(preventDefault).toHaveBeenCalled();
  expect(base.post).toHaveBeenCalled();

  return promise.then(() => {
    expect(wrapper.find('em').text().length).toEqual(16);
  });
});

it('does not submit if the name is blank', () => {
  const base = mockBase();
  const wrapper = mountWithIntl(<NewChallenge base={base} defaultName="  " />);
  const form = wrapper.find('form');
  const preventDefault = jest.fn();

  form.simulate('submit', { preventDefault });

  expect(preventDefault).toHaveBeenCalled();
  expect(base.post).not.toHaveBeenCalled();
});

it('shows an error when the post fails', () => {
  const promise = Promise.reject();
  const base = { post: jest.fn().mockReturnValue(promise) };

  const wrapper = mountWithIntl(
    <NewChallenge base={base} defaultName="Hi" />
  );

{'fraction_non_alphanumeric': 0.18017028288931614, 'fraction_numerical': 0.0016478989288656962, 'mean_word_length': 4.072423398328691, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4883257', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1108, 'n_words': 239}