Skip to content

Commit

Permalink
fix log track item splitting on idle, add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Maigo Erit committed Sep 11, 2017
1 parent 46c55a9 commit 15a0a31
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 218 deletions.
220 changes: 129 additions & 91 deletions electron/__tests__/state-manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,144 +2,182 @@ jest.autoMockOff();

import BackgroundUtils from '../app/background-utils';
import { models } from '../app/models';
import { TrackItemAttributes, TrackItemInstance } from '../app/models/interfaces/track-item-interface';
import {
TrackItemAttributes,
TrackItemInstance,
} from '../app/models/interfaces/track-item-interface';
import { settingsService } from '../app/services/settings-service';
import { trackItemService } from '../app/services/track-item-service';
import { stateManager } from '../app/state-manager';
import { State } from '../app/enums/state';
import { TrackItemType } from '../app/enums/track-item-type';
import TrackItemTestData from './track-item-test-data';

describe('isSystemOnline', () => {

afterEach(async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
});

it('returns true if State.Online', async () => {
import * as delay from 'delay';

let rawItem: TrackItemInstance = models.TrackItem.build(TrackItemTestData.getStatusTrackItem({ app: State.Online }));

stateManager.setCurrentTrackItem(rawItem);

let state = stateManager.isSystemOnline();
describe('isSystemOnline', () => {
afterEach(async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
});

expect(state).toEqual(true);
});
it('returns true if State.Online', async () => {
let rawItem: TrackItemInstance = models.TrackItem.build(
TrackItemTestData.getStatusTrackItem({ app: State.Online }),
);

it('returns false if State.Offline', async () => {
stateManager.setCurrentTrackItem(rawItem);

let rawItem: TrackItemInstance = models.TrackItem.build(TrackItemTestData.getStatusTrackItem({ app: State.Offline }));
let state = stateManager.isSystemOnline();

stateManager.setCurrentTrackItem(rawItem);
expect(state).toEqual(true);
});

let state = stateManager.isSystemOnline();
it('returns false if State.Offline', async () => {
let rawItem: TrackItemInstance = models.TrackItem.build(
TrackItemTestData.getStatusTrackItem({ app: State.Offline }),
);

expect(state).toEqual(false);
});
stateManager.setCurrentTrackItem(rawItem);

it('returns false if State.Idle', async () => {
let state = stateManager.isSystemOnline();

let rawItem: TrackItemInstance = models.TrackItem.build(TrackItemTestData.getStatusTrackItem());
expect(state).toEqual(false);
});

stateManager.setCurrentTrackItem(rawItem);
it('returns false if State.Idle', async () => {
let rawItem: TrackItemInstance = models.TrackItem.build(
TrackItemTestData.getStatusTrackItem(),
);

let state = stateManager.isSystemOnline();
stateManager.setCurrentTrackItem(rawItem);

expect(state).toEqual(false);
});
let state = stateManager.isSystemOnline();

it('returns false if StateTrackItem is not defined', async () => {
expect(state).toEqual(false);
});

stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
it('returns false if StateTrackItem is not defined', async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);

let state = stateManager.isSystemOnline();
let state = stateManager.isSystemOnline();

expect(state).toEqual(false);
});
expect(state).toEqual(false);
});
});


describe('endRunningTrackItem', () => {
afterEach(async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
models.TrackItem.$clearQueue();
});

afterEach(async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
models.TrackItem.$clearQueue();
});

it('returns item if has running item to end', async () => {
models.TrackItem.$queueResult([]);

stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
let rawItemRunning = TrackItemTestData.getStatusTrackItem({ app: State.Online });
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);
stateManager.setCurrentTrackItem(itemRunning);

let rawItem: TrackItemAttributes = TrackItemTestData.getStatusTrackItem({ app: State.Online }, 1);
it('returns item if has running item to end', async () => {
models.TrackItem.$queueResult([]);

expect(rawItemRunning.endDate).not.toEqual(rawItem.beginDate);

let updatedItem = await stateManager.endRunningTrackItem(rawItem);
expect(updatedItem).not.toEqual(rawItemRunning);
expect(updatedItem.endDate).toEqual(rawItem.beginDate);
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
let rawItemRunning = TrackItemTestData.getStatusTrackItem({
app: State.Online,
});
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);
stateManager.setCurrentTrackItem(itemRunning);

let rawItem: TrackItemAttributes = TrackItemTestData.getStatusTrackItem(
{ app: State.Online },
1,
);

it('returns null if has no running item to end', async () => {
expect(rawItemRunning.endDate).not.toEqual(rawItem.beginDate);

stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);
let updatedItem = await stateManager.endRunningTrackItem(rawItem);
expect(updatedItem).not.toEqual(rawItemRunning);
expect(updatedItem.endDate).toEqual(rawItem.beginDate);
});

let rawItem: TrackItemAttributes = TrackItemTestData.getStatusTrackItem({ app: State.Online });
it('returns null if has no running item to end', async () => {
stateManager.resetCurrentTrackItem(TrackItemType.StatusTrackItem);

let updatedItem = await stateManager.endRunningTrackItem(rawItem);
expect(updatedItem).toEqual(null);
let rawItem: TrackItemAttributes = TrackItemTestData.getStatusTrackItem({
app: State.Online,
});

let updatedItem = await stateManager.endRunningTrackItem(rawItem);
expect(updatedItem).toEqual(null);
});
});

describe('setLogTrackItemMarkedAsRunning', () => {
afterEach(async () => {});

afterEach(async () => {

it('saves running log item id', async () => {
let rawItemRunning = TrackItemTestData.getStatusTrackItem({
app: State.Online,
});
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);

it('saves running log item id', async () => {

let rawItemRunning = TrackItemTestData.getStatusTrackItem({ app: State.Online });
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);

let saveRunningLogItemReferenceMock = jest.fn();
settingsService.saveRunningLogItemReference = saveRunningLogItemReferenceMock;
stateManager.setLogTrackItemMarkedAsRunning(itemRunning);

expect(saveRunningLogItemReferenceMock.mock.calls.length).toBe(1);
let saveRunningLogItemReferenceMockCalledWith = saveRunningLogItemReferenceMock.mock.calls[0][0];
expect(saveRunningLogItemReferenceMockCalledWith).toBe(itemRunning.id);
});
let saveRunningLogItemReferenceMock = jest.fn();
settingsService.saveRunningLogItemReference = saveRunningLogItemReferenceMock;
stateManager.setLogTrackItemMarkedAsRunning(itemRunning);

expect(saveRunningLogItemReferenceMock.mock.calls.length).toBe(1);
let saveRunningLogItemReferenceMockCalledWith =
saveRunningLogItemReferenceMock.mock.calls[0][0];
expect(saveRunningLogItemReferenceMockCalledWith).toBe(itemRunning.id);
});
});


describe('restoreState', () => {
afterEach(async () => {});

afterEach(async () => {
it('saves running log item id', async () => {
let rawItemRunning = TrackItemTestData.getLogTrackItem();
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);

});

it('saves running log item id', async () => {

let rawItemRunning = TrackItemTestData.getLogTrackItem();
let itemRunning: TrackItemInstance = models.TrackItem.build(rawItemRunning);

let findRunningLogItemMock = jest.fn();
trackItemService.findRunningLogItem = findRunningLogItemMock;
findRunningLogItemMock.mockReturnValueOnce(itemRunning);
let findRunningLogItemMock = jest.fn();
trackItemService.findRunningLogItem = findRunningLogItemMock;
findRunningLogItemMock.mockReturnValueOnce(itemRunning);

let restoredItem = await stateManager.restoreState();
let restoredItem = await stateManager.restoreState();

expect(findRunningLogItemMock.mock.calls.length).toBe(1);
expect(stateManager.getCurrentTrackItem(TrackItemType.LogTrackItem)).toBe(itemRunning);
expect(stateManager.getLogTrackItemMarkedAsRunning()).toBe(itemRunning);
expect(restoredItem).toBe(itemRunning);
});
expect(findRunningLogItemMock.mock.calls.length).toBe(1);
expect(stateManager.getCurrentTrackItem(TrackItemType.LogTrackItem)).toBe(
itemRunning,
);
expect(stateManager.getLogTrackItemMarkedAsRunning()).toBe(itemRunning);
expect(restoredItem).toBe(itemRunning);
});
});

});
describe('createNewRunningTrackItem', () => {
afterEach(async () => {});

it('ends current and creates new item. And current item is new one.', async () => {
let rawOnlineItemRunning = TrackItemTestData.getStatusOnlineTrackItem();
let itemOnlineRunning: TrackItemInstance = models.TrackItem.build(
rawOnlineItemRunning,
);

let rawIdleItemRunning = TrackItemTestData.getStatusOnlineTrackItem();
let itemIdleRunning: TrackItemInstance = models.TrackItem.build(
rawIdleItemRunning,
);
stateManager.setCurrentTrackItem(itemIdleRunning);

let updateItemMock = jest.fn(() => delay(200).then(() => true));
let createTrackItemMock = jest.fn();
trackItemService.createTrackItem = createTrackItemMock;
trackItemService.updateItem = updateItemMock;
createTrackItemMock.mockReturnValueOnce(
delay(200).then(() => itemOnlineRunning),
);

let restoredItem = await stateManager.createNewRunningTrackItem(
rawOnlineItemRunning,
);

expect(updateItemMock.mock.calls.length).toBe(1);
expect(createTrackItemMock.mock.calls.length).toBe(1);
expect(
stateManager.getCurrentTrackItem(TrackItemType.StatusTrackItem),
).toBe(itemOnlineRunning);
});
});
114 changes: 78 additions & 36 deletions electron/__tests__/track-item-test-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,87 @@ import { TrackItemType } from '../app/enums/track-item-type';
import * as moment from 'moment';

export default class TrackItemTestData {
static getAppTrackItem(data = {}, addHours = 0): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'Chrome',
title: 'google.com',
taskName: TrackItemType.AppTrackItem,
color: '#123456',
beginDate: moment()
.startOf('day')
.add(addHours, 'hours')
.toDate(),
endDate: moment()
.startOf('day')
.add(addHours + 1, 'hours')
.toDate(),
};

static getAppTrackItem(data = {}, addHours = 0): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'Chrome',
title: 'google.com',
taskName: TrackItemType.AppTrackItem,
color: '#123456',
beginDate: moment().startOf('day').add(addHours, 'hours').toDate(),
endDate: moment().startOf('day').add(addHours + 1, 'hours').toDate()
};
return Object.assign({}, rawItem, data);
}

return Object.assign({}, rawItem, data);
}
static getLogTrackItem(data = {}, addDays = 1): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'WORK',
title: 'Some work.',
taskName: TrackItemType.LogTrackItem,
color: '#123456',
beginDate: moment()
.startOf('day')
.add(addDays, 'hours')
.toDate(),
endDate: moment()
.startOf('day')
.add(addDays + 1, 'hours')
.toDate(),
};

static getLogTrackItem(data = {}, addDays = 1): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'WORK',
title: 'Some work.',
taskName: TrackItemType.LogTrackItem,
color: '#123456',
beginDate: moment().startOf('day').add(addDays, 'hours').toDate(),
endDate: moment().startOf('day').add(addDays + 1, 'hours').toDate()
};
return Object.assign({}, rawItem, data);
}

return Object.assign({}, rawItem, data);
}

static getStatusTrackItem(data: TrackItemAttributes = {}, addDays = 1): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'IDLE',
taskName: TrackItemType.StatusTrackItem,
color: '#123456',
beginDate: moment().startOf('day').add(addDays, 'hours').toDate(),
endDate: moment().startOf('day').add(addDays + 1, 'hours').toDate()
};
rawItem.title = (data.app) ? data.app.toString().toLowerCase() : rawItem.app.toString().toLowerCase();
return Object.assign({}, rawItem, data);
}
static getStatusTrackItem(
data: TrackItemAttributes = {},
addDays = 1,
): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'IDLE',
taskName: TrackItemType.StatusTrackItem,
color: '#123456',
beginDate: moment()
.startOf('day')
.add(addDays, 'hours')
.toDate(),
endDate: moment()
.startOf('day')
.add(addDays + 1, 'hours')
.toDate(),
};
rawItem.title = data.app
? data.app.toString().toLowerCase()
: rawItem.app.toString().toLowerCase();
return Object.assign({}, rawItem, data);
}

static getStatusOnlineTrackItem(
data: TrackItemAttributes = {},
addDays = 1,
): TrackItemAttributes {
const rawItem: TrackItemAttributes = {
app: 'ONLINE',
taskName: TrackItemType.StatusTrackItem,
color: '#666777',
beginDate: moment()
.startOf('day')
.add(addDays, 'hours')
.toDate(),
endDate: moment()
.startOf('day')
.add(addDays + 1, 'hours')
.toDate(),
};
rawItem.title = data.app
? data.app.toString().toLowerCase()
: rawItem.app.toString().toLowerCase();
return Object.assign({}, rawItem, data);
}
}


Loading

0 comments on commit 15a0a31

Please sign in to comment.