<commit_before>/* @flow */
const {describe, it} = global;
import expect from 'expect';
import selectionContainsEntity from '../selectionContainsEntity';
import {EditorState, Entity, Modifier, SelectionState} from 'draft-js';

const DUMMY_ENTITY = 'DUMMY_ENTITY';

const findEntity = (character) => {
  const entityKey = character.getEntity();
  return (
    entityKey !== null &&
    Entity.get(entityKey).getType() === 'DUMMY_ENTITY'
  );
};

const strategy = (contentBlock, callback) => {
  contentBlock.findEntityRanges(findEntity, callback);
};

const insertDummyText = (editorState, text, withEntity) => (
  EditorState.push(
    editorState,
    Modifier.insertText(
      editorState.getCurrentContent(),
      editorState.getSelection(),
      text ? text : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
      undefined,
      withEntity ? (
        Entity.create(
          DUMMY_ENTITY,
          'MUTABLE',
        )
      ) : undefined
    ),
    'insert-characters',
  )
);

let editorState = insertDummyText(EditorState.createEmpty());
editorState = EditorState.push(
  editorState,
  Modifier.splitBlock(
    editorState.getCurrentContent(),
    editorState.getSelection(),
  ),
  'split-block',
);
editorState = insertDummyText(editorState);
editorState = insertDummyText(editorState, 'this contains an entity', true);

const blockMap = editorState.getCurrentContent().getBlockMap();

const first = blockMap.first();
const last = blockMap.last();

editorState = EditorState.forceSelection(
  editorState,
  new SelectionState({
    anchorKey: first.getKey(),
    anchoeOffset: 0,
    focusKey: last.getKey(),
    focusOffset: last.getLength(),
  }),
);

describe('selectionContainsEntity', () => {
  it('should call return a new function when passing in a strategy', () => {
    expect(selectionContainsEntity(strategy)).toBeA('function');
  });

  it('should return true, if an entity which matches the strategy has been found', () => {
    expect(selectionContainsEntity(strategy)(editorState)).toEqual(true);
  });

  it('should return false, if no entity which matches the strategy has been found', () => {
    editorState = EditorState.forceSelection(
      editorState,
      new SelectionState({
        anchorKey: first.getKey(),
        anchoeOffset: 0,
        focusKey: first.getKey(),
        focusOffset: first.getLength(),
      }),
    );

    expect(selectionContainsEntity(strategy)(editorState)).toEqual(false);
  });

  it('should allow passing in a custom selection', () => {
    const lastBlockWithEntity = new SelectionState({
      anchorKey: last.getKey(),
      anchoeOffset: 0,
      focusKey: last.getKey(),
      focusOffset: last.getLength(),
    });

    expect(selectionContainsEntity(strategy)(editorState, lastBlockWithEntity)).toEqual(true);

    const lastBlockWithoutEntity = new SelectionState({
      anchorKey: last.getKey(),
      anchoeOffset: 0,
      focusKey: last.getKey(),
      focusOffset: 10,
    });

    expect(selectionContainsEntity(strategy)(editorState, lastBlockWithoutEntity)).toEqual(false);

    const firstBlockWithoutEntity = new SelectionState({
      anchorKey: first.getKey(),
      anchoeOffset: 0,
      focusKey: first.getKey(),
      focusOffset: first.getLength(),
    });

    expect(selectionContainsEntity(strategy)(editorState, firstBlockWithoutEntity)).toEqual(false);
  });
});<commit_msg>Fixed incorrect prop name<commit_after>/* @flow */
const {describe, it} = global;
import expect from 'expect';
import selectionContainsEntity from '../selectionContainsEntity';
import {EditorState, Entity, Modifier, SelectionState} from 'draft-js';

const DUMMY_ENTITY = 'DUMMY_ENTITY';

const findEntity = (character) => {
  const entityKey = character.getEntity();
  return (
    entityKey !== null &&
    Entity.get(entityKey).getType() === 'DUMMY_ENTITY'
  );
};

const strategy = (contentBlock, callback) => {
  contentBlock.findEntityRanges(findEntity, callback);
};

const insertDummyText = (editorState, text, withEntity) => (
  EditorState.push(
    editorState,
    Modifier.insertText(
      editorState.getCurrentContent(),
      editorState.getSelection(),
      text ? text : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit',
      undefined,
      withEntity ? (
        Entity.create(
          DUMMY_ENTITY,
          'MUTABLE',
        )
      ) : undefined
    ),
    'insert-characters',
  )
);

let editorState = insertDummyText(EditorState.createEmpty());
editorState = EditorState.push(
  editorState,
  Modifier.splitBlock(
    editorState.getCurrentContent(),
    editorState.getSelection(),
  ),
  'split-block',
);
editorState = insertDummyText(editorState);
editorState = insertDummyText(editorState, 'this contains an entity', true);

const blockMap = editorState.getCurrentContent().getBlockMap();

const first = blockMap.first();
const last = blockMap.last();

editorState = EditorState.forceSelection(
  editorState,
  new SelectionState({
    anchorKey: first.getKey(),
    anchorOffset: 0,
    focusKey: last.getKey(),
    focusOffset: last.getLength(),
  }),
);

describe('selectionContainsEntity', () => {
  it('should call return a new function when passing in a strategy', () => {
    expect(selectionContainsEntity(strategy)).toBeA('function');
  });

  it('should return true, if an entity which matches the strategy has been found', () => {
    expect(selectionContainsEntity(strategy)(editorState)).toEqual(true);
  });

  it('should return false, if no entity which matches the strategy has been found', () => {
    editorState = EditorState.forceSelection(
      editorState,
      new SelectionState({
        anchorKey: first.getKey(),
        anchorOffset: 0,
        focusKey: first.getKey(),
        focusOffset: first.getLength(),
      }),
    );

    expect(selectionContainsEntity(strategy)(editorState)).toEqual(false);
  });

  it('should allow passing in a custom selection', () => {
    const lastBlockWithEntity = new SelectionState({
      anchorKey: last.getKey(),
      anchorOffset: 0,
      focusKey: last.getKey(),
      focusOffset: last.getLength(),
    });

    expect(selectionContainsEntity(strategy)(editorState, lastBlockWithEntity)).toEqual(true);

    const lastBlockWithoutEntity = new SelectionState({
      anchorKey: last.getKey(),
      anchorOffset: 0,
      focusKey: last.getKey(),
      focusOffset: 10,
    });

    expect(selectionContainsEntity(strategy)(editorState, lastBlockWithoutEntity)).toEqual(false);

    const firstBlockWithoutEntity = new SelectionState({
      anchorKey: first.getKey(),
      anchorOffset: 0,
      focusKey: first.getKey(),
      focusOffset: first.getLength(),
    });

    expect(selectionContainsEntity(strategy)(editorState, firstBlockWithoutEntity)).toEqual(false);
  });
});
{'fraction_non_alphanumeric': 0.12794117647058822, 'fraction_numerical': 0.002058823529411765, 'mean_word_length': 3.9282608695652175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208022', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1914, 'n_words': 427}