/*
 * SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
 *
 * SPDX-License-Identifier: AGPL-3.0-only
 */

import { replaceSelection } from './replace-selection'

describe('replace selection', () => {
  it('inserts a text after the from-cursor if no to-cursor is present', () => {
    const actual = replaceSelection(
      'text1',
      {
        from: 2
      },
      'text2'
    )
    expect(actual).toEqual(['tetext2xt1', { from: 2, to: 7 }])
  })

  it('inserts a text if from-cursor and to-cursor are the same', () => {
    const actual = replaceSelection(
      'text1',
      {
        from: 2,
        to: 2
      },
      'text2'
    )
    expect(actual).toEqual(['tetext2xt1', { from: 2, to: 7 }])
  })

  it('replaces a single line text', () => {
    const actual = replaceSelection(
      'text1\ntext2\ntext3',
      {
        from: 7,
        to: 8
      },
      'text4'
    )
    expect(actual).toEqual(['text1\nttext4xt2\ntext3', { from: 7, to: 12 }])
  })

  it('replaces a multi line text', () => {
    const actual = replaceSelection(
      'text1\ntext2\ntext3',
      {
        from: 2,
        to: 15
      },
      'text4'
    )
    expect(actual).toEqual(['tetext4t3', { from: 2, to: 7 }])
  })
})
