import { matchSort } from '..'

it('does not sort items when query is empty', () => {
  const items = [
    { name: 'app' },
    { name: 'apple' },
  ]
  const query = ''
  const result = matchSort(
    items,
    'name',
    query,
  )
  expect(result).toEqual(items)
})

it('non matching words omitted', () => {
  const items = [
    { name: 'banana' },
    { name: 'app' },
    { name: 'apple' },
  ]
  const query = 'app'
  const result = matchSort(
    items,
    'name',
    query,
  )
  expect(result).toEqual([
    items[1],
    items[2],
  ])
})

it('matching words go first', () => {
  const items = [
    { name: 'apple' },
    { name: 'app' },
  ]
  const query = 'app'
  const result = matchSort(
    items,
    'name',
    query,
  )
  expect(result).toEqual([
    items[1],
    items[0],
  ])
})

it('words starting with query word go second', () => {
  const items = [
    { name: 'apple' },
    { name: 'app' },
    { name: 'buggy app' },
  ]
  const query = 'app'
  const result = matchSort(
    items,
    'name',
    query,
  )
  expect(result).toEqual([
    items[1],
    items[0],
    items[2],
  ])
})
