const sinon = require('sinon');
const filesaver = require('file-saver');
const utils = require('../../src/commons/utils');
const showSlides = require('../../src/commons/showSlides');

const event = [{
  _id: '234',
  voName: 'run the swamp',
  voCharityId: '123',
  voCharityName: 'howdy',
  voloppId: 1,
  voNumPeopleNeeded: 1,
  voDescription: '',
  voWorkTypes: [],
  voTalentTypes: [],
  voWorkTypeOther: '',
  voTalentTypeOther: '',
  voStartDate: '2017-01-01T10:14:32.909Z',
  voStartTime: '10:10 am',
  voEndDate: '2018-11-14T10:14:32.909Z',
  voEndTime: '8:15pm',
  voContactName: '',
  voContactEmail: '',
  voContactPhone: ''
}];

describe('the common utils', () => {
  beforeEach((done) => {
    jest.useFakeTimers();
    done();
  });
  it('starts a slide show', (done) => {
    sMock = sinon.mock(showSlides);
    sMock.expects('showSlides').returns(true);
    utils.startSlides(['1', '2', '3']);
    jest.advanceTimersByTime(5400);
    sMock.restore();
    done();
  });
  it('should test util compareTime functions', () => {
    expect(utils.compareTime('11:07 pm', '10:18 pm')).toBeTruthy();
    expect(utils.compareTime('11:07 pm', '10:18 am')).toBeTruthy();
    expect(utils.compareTime('11:07 pm', '')).toBeFalsy();
    expect(utils.compareTime('11:07 pm', '11:18 pm')).toBeFalsy();
    expect(utils.compareTime('11:37 pm', '11:18 pm')).toBeTruthy();
    expect(utils.compareTime('10:37 pm', '11:18 pm')).toBeFalsy();
  });
  it('should format a 12hr time', () => {
    expect(utils.getTime(13, 30)).toBe('1:30 pm');
    expect(utils.getTime(11, 30)).toBe('11:30 am');
    expect(utils.getTime(0, 20)).toBe('12:20 am');
  });
  it('should format a datetime', () => {
    utils.formatDate(new Date());
    utils.fixDates(event);
  });
  it('should mark past dates', () => {
    utils.markPast(event, utils.formatDate);
  });
  it('makes a tab delimted text file', async () => {
    let cb;
    const fMock = sinon.mock(filesaver);
    fMock.expects('saveAs').resolves(true);
    try {
      cb = await utils.makeCSVfile({ fetch() { return Promise.resolve({ json() { return Promise.resolve({}); } }); } }, '', '');
      expect(cb).toBe(true);
    } catch (e) { throw e; }
    fMock.restore();
  });
});

@@ -2210,12 +2210,399 @@
 ;%0A  %7D);%0A
+  it('should filter selected module', () =%3E %7B%0A    utils.filterSelected(%7BselectedFilter: %5B%5D, filters: %5B%7Bvalue: '', filterby: ''%7D%5D%7D);%0A    utils.filterSelected(%7BselectedFilter: %5B'hello', 'sir'%5D, filters: %5B%7Bvalue: '', filterby: 'hello'%7D, %7Bvalue: '', filterby: 'syre'%7D%5D%7D)%0A  %7D);%0A  it('should mark filter dropdown', () =%3E %7B%0A    utils.makeFilterDropdown(%5B%5D, %5B%7Battrib: 'hello'%7D%5D, 'attrib');%0A  %7D)%0A
 %7D);%0A

update coverage for utils functions