{"size": 1720, "ext": "js", "max_stars_repo_path": "src/infra/service/util.test.js", "max_stars_repo_name": "antoniotrento/kanca", "max_stars_repo_stars_event_min_datetime": "2017-04-18T00:58:47.000Z", "max_stars_repo_stars_event_max_datetime": "2021-10-02T10:25:37.000Z", "max_issues_repo_path": "src/infra/service/util.test.js", "max_issues_repo_name": "SimpleDataSci/kanca", "max_issues_repo_issues_event_min_datetime": "2017-04-15T11:10:38.000Z", "max_issues_repo_issues_event_max_datetime": "2020-09-26T07:05:03.000Z", "max_forks_repo_path": "src/infra/service/util.test.js", "max_forks_repo_name": "SimpleDataSci/kanca", "max_forks_repo_forks_event_min_datetime": "2017-04-20T13:27:07.000Z", "max_forks_repo_forks_event_max_datetime": "2019-04-21T15:37:04.000Z"}
{"max_stars_count": 29, "max_issues_count": 231, "max_forks_count": 12, "avg_line_length": 30.1754385965, "max_line_length": 99, "alphanum_fraction": 0.5825581395}
// @flow
import { syncToPromise, wordCounter } from './util';
import _ from 'lodash';

describe('sync to promise', () => {

  it('able to generate promise from sync operation', () =>
    syncToPromise(() => {
      return 1 + 1;
    })
      .then(res => expect(res).toEqual(2))
  );

  it('able to generate promise from promise operation', () =>
    syncToPromise(() => Promise.resolve(1 + 1))
      .then(res => expect(res).toEqual(2))
  );

  it('able to catch error from sync operation', () =>
    syncToPromise(() => {
      throw new Error('ERROR');
    })
      .catch(err => expect(err.message).toEqual('ERROR'))
  );

  it('able to catch error from promise operation', () =>
    syncToPromise(() => Promise.reject(new Error('ERROR')))
      .catch(err => expect(err.message).toEqual('ERROR'))
  );

});

describe('word counting', () => {

  type WordCount = { word: string, count: number }
  const pick = (count: WordCount[], word: string) => (_.find(count, co => co.word === word) || {});

  it('able to count words', () => {
    const string = 'aku kamu kami react native react redux , (*&( &*yu 8989';

    return wordCounter(string)
      .then(count => {
        // check should skipped words
        expect(pick(count, 'aku').count).toEqual(undefined);
        expect(pick(count, ',').count).toEqual(undefined);
        expect(pick(count, '(*&(').count).toEqual(undefined);
        expect(pick(count, '&*yu').count).toEqual(undefined);

        // test valid words
        expect(pick(count, '8989').count).toEqual(1);
        expect(pick(count, 'react').count).toEqual(2);
        expect(pick(count, 'redux').count).toEqual(1);
        expect(pick(count, 'native').count).toEqual(1);
      });
  });

});