import { filterByKey, sortStringOn, sortIntOn, sortDateOn } from './search';
import loadSource from './search-source';

describe('Scroll test', () => {
  test('loadSource()', () => {
    const data = loadSource('id', ['title']);
    expect(data).toBeInstanceOf(Promise);
  });

  test('filterByKey()', () => {
    const arr = [
      { type: 'game', content: 'game 1' },
      { type: 'game', content: 'game 2' },
      { type: 'operator', content: 'operator 1' },
    ];
    const games = [
      { type: 'game', content: 'game 1' },
      { type: 'game', content: 'game 2' },
    ];
    expect(arr.filter(filterByKey('type', 'game'))).toEqual(games);
  });

  test('sortStringOn()', () => {
    const arr = [
      { id: 'b', content: 'game 1' },
      { id: 'c', content: 'game 2' },
      { id: 'a', content: 'operator 1' },
    ];
    const sorted = [
      { id: 'a', content: 'operator 1' },
      { id: 'b', content: 'game 1' },
      { id: 'c', content: 'game 2' },
    ];
    expect(arr.sort(sortStringOn('id'))).toEqual(sorted);
  });

  test('sortIntOn()', () => {
    const arr = [
      { id: 1005, content: 'game 1' },
      { id: 62, content: 'game 2' },
      { id: 438, content: 'operator 1' },
    ];
    const sorted = [
      { id: 62, content: 'game 2' },
      { id: 438, content: 'operator 1' },
      { id: 1005, content: 'game 1' },
    ];
    expect(arr.sort(sortIntOn('id'))).toEqual(sorted);
  });

  test('sortDateOn()', () => {
    const arr = [
      { created_at: '2020-04-23 11:45:10', content: 'game 1' },
      { created_at: '2020-06-23 12:45:00', content: 'game 2' },
      { created_at: '2020-04-31 11:45:10', content: 'operator 1' },
    ];
    const sorted = [
      { created_at: '2020-06-23 12:45:00', content: 'game 2' },
      { created_at: '2020-04-31 11:45:10', content: 'operator 1' },
      { created_at: '2020-04-23 11:45:10', content: 'game 1' },
    ];
    expect(arr.sort(sortDateOn('created_at'))).toEqual(sorted);
  });
});
