Skip to content

Commit

Permalink
Adds unit tests for resuming timer for a task
Browse files Browse the repository at this point in the history
Adds dirty-chai plugin to chai. It help fix complains from
standardJS linting. It also makes the code more robust.
standard/standard#690 (comment)

Also adds sinon so we can test console.log outputs.
  • Loading branch information
Nightpanda committed May 4, 2018
1 parent 713074b commit 27f2b71
Show file tree
Hide file tree
Showing 5 changed files with 191 additions and 39 deletions.
1 change: 0 additions & 1 deletion app.js
Expand Up @@ -110,7 +110,6 @@ exports.switchAutosave = (taskList, readInterface) => {
} else {
clearInterval(autosaver.timer)
}

this.clearAndDisplayHelpAndTasks(taskList)
}

Expand Down
127 changes: 117 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions package.json
@@ -1,6 +1,6 @@
{
"name": "task-time-manager",
"version": "1.2.0",
"version": "1.1.5",
"description": "Helper for tracking time between differents tasks.",
"main": "app.js",
"scripts": {
Expand All @@ -26,10 +26,12 @@
},
"homepage": "https://github.com/Nightpanda/task-time-manager#readme",
"devDependencies": {
"chai": "4.1.2",
"chai": "^4.1.2",
"dirty-chai": "^2.0.1",
"husky": "^0.14.3",
"mocha": "5.1.0",
"nyc": "11.6.0",
"sinon": "5.0.1",
"standard": "^11.0.1"
},
"dependencies": {
Expand Down
33 changes: 18 additions & 15 deletions tasks.js
Expand Up @@ -58,9 +58,8 @@ exports.stopAllTasks = (tasks) => {
exports.applyToTaskByIndex = (taskList, index, method, ...args) => {
let task = taskList[index]
if (task) {
const display = app.clearAndDisplayHelpAndTasks
task = method(task, ...args)
display(taskList)
taskList[index] = task
} else {
log(styles.warningStyle(`No task found for index ${index}`))
}
Expand All @@ -81,6 +80,7 @@ exports.setTaskTime = (taskList, readInterface) => {
readInterface.question(styles.questionStyle('Give the index number of the task to set time for: '), taskIndex => {
readInterface.question(styles.questionStyle('Give time for task in seconds: '), time => {
this.applyToTaskByIndex(taskList, taskIndex, this.setTimeFor, parseInt(time))
app.clearAndDisplayHelpAndTasks(taskList)
})
})
}
Expand Down Expand Up @@ -117,21 +117,24 @@ exports.taskTimersActive = (taskList) => {
}
}

exports.resumeTaskFor = (task, taskIndex, taskList) => {
const running = task.timerRunning
const taskName = task.name
if (!running) {
task.timerRunning = !running
task.timer = setInterval(intervalFunc, 1000, taskIndex, taskList)
console.log(styles.successStyle(`Resumed task ${task.name}`))
} else {
console.log(styles.warningStyle(`Timer already running for ${taskName}`))
}
return task
}

exports.resumeTask = (taskList, readInterface) => {
readInterface.question(styles.questionStyle('Give the index number of the task to resume timing: '), taskIndex => {
let task = this.findTaskByIndex(taskIndex, taskList)
if (task) {
const running = task.timerRunning
const taskName = task.name
if (!running) {
task.timerRunning = !running
task.timer = setInterval(intervalFunc, 1000, taskIndex, taskList)
app.clearAndDisplayHelpAndTasks(taskList)
log(styles.successStyle(`Resumed task ${taskName}`))
} else {
log(styles.warningStyle(`Timer already running for ${taskName}`))
}
}
this.applyToTaskByIndex(taskList, taskIndex, (task) => {
return this.resumeTaskFor(task, taskIndex, taskList)
})
})
}

Expand Down
63 changes: 52 additions & 11 deletions test.js
@@ -1,9 +1,15 @@
/* global describe it */
/* global describe it beforeEach afterEach */
'use strict'

const should = require('chai').should()
const dirtyChai = require('dirty-chai')
const chai = require('chai')
const should = chai.should()
chai.use(dirtyChai)

const tasks = require('./tasks.js')
const readInterface = require('./interface.js')
const sinon = require('sinon')
const styles = require('./styles.js')

/*
describe('Test stopRunningTaskTimers', () => {
Expand All @@ -20,6 +26,17 @@ describe('Test stopRunningTaskTimers', () => {
})
})
*/

function timerTask () {
return {timer: {_onTimeout: () => {}}, timerRunning: true}
}

function stoppedTask () {
let task = {timer: setInterval(task => { clearInterval(task.timer) }), timerRunning: true, name: 'Halted One'}
clearInterval(task.timer)
return task
}

describe('Test stopRunningTimerInTask', () => {
it('reverses timerRunning in task', () => {
let task = {timer: setInterval(task => { clearInterval(task.timer) }), timerRunning: true}
Expand Down Expand Up @@ -104,15 +121,39 @@ describe('Test isTaskTimerRunning', () => {
})
})

function timerTask () {
return {timer: {_onTimeout: () => {}}, timerRunning: true}
}

function stoppedTask () {
let task = {timer: setInterval(task => { clearInterval(task.timer) }), timerRunning: true}
clearInterval(task.timer)
return task
}
describe('Test resumeTaskFor', () => {
let sandbox
beforeEach(() => {
sandbox = sinon.createSandbox()
sandbox.stub(console, 'log')
sandbox.stub(console, 'error')
})
afterEach(() => {
sandbox.restore()
})
const taskName = 'TestonosTaskonos'
const timerTask = {timer: {_onTimeout: () => {}}, timerRunning: true, name: taskName}
it('should be a function', () => {
tasks.resumeTaskFor.should.be.a('function')
})
it('should log error when timer already running', () => {
tasks.resumeTaskFor(timerTask)
sinon.assert.calledOnce(console.log)
sinon.assert.calledWithExactly(console.log, styles.warningStyle(`Timer already running for ${taskName}`))
})
it('should log success, change timerRunning true and start a timer, when timer succesfully started', () => {
const stoppedTimer = {timer: setInterval(() => {}), timerRunning: true, name: 'Halted One'}
let taskList = []
clearInterval(stoppedTimer.timer)
stoppedTimer.timerRunning = false
tasks.isTaskTimerRunning(stoppedTimer).should.be.false()
const resumedTask = tasks.resumeTaskFor(stoppedTimer, 0, taskList)
tasks.isTaskTimerRunning(stoppedTimer).should.be.true()
resumedTask.timerRunning.should.be.true()
sinon.assert.calledOnce(console.log)
sinon.assert.calledWithExactly(console.log, styles.successStyle(`Resumed task ${stoppedTimer.name}`))
})
})

describe('Test interface.js', () => {
let newReadInterface = readInterface.createReadInterface()
Expand Down

0 comments on commit 27f2b71

Please sign in to comment.