Skip to content

Commit

Permalink
BDD test
Browse files Browse the repository at this point in the history
  • Loading branch information
dex4er committed Feb 19, 2017
1 parent 1ef7ce1 commit accbad6
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 88 deletions.
3 changes: 3 additions & 0 deletions .taprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodeArgs": ["--harmony"],
}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## v0.0.2 2017-02-19

* ES6
* BDD tests
* Relicensed to MIT

## v0.0.1 2016-07-31
Expand Down
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
## timers-obj

[![Build Status](https://secure.travis-ci.org/dex4er/js-timers-obj.svg)](http://travis-ci.org/dex4er/js-timers-obj) [![Coverage Status](https://coveralls.io/repos/github/dex4er/js-timers-obj/badge.svg)](https://coveralls.io/github/dex4er/js-timers-obj) [![npm](https://img.shields.io/npm/v/timers-obj.svg?maxAge=2592000)](https://www.npmjs.com/package/timers-obj)
[![Build Status](https://secure.travis-ci.org/dex4er/js-timers-obj.svg)](http://travis-ci.org/dex4er/js-timers-obj) [![Coverage Status](https://coveralls.io/repos/github/dex4er/js-timers-obj/badge.svg)](https://coveralls.io/github/dex4er/js-timers-obj) [![npm](https://img.shields.io/npm/v/timers-obj.svg)](https://www.npmjs.com/package/timers-obj)

This module provides the wrappers for standard `timers` module so all timers
(immediate, interval and timeout) can be used as objects.
This module provides the wrappers for standard [timers](https://nodejs.org/api/timers.html)
module so all timers (immediate, interval and timeout) can be used as objects.

For constructors, callback argument is after delay argument so it provides more
convenient syntax for [CoffeeScript](http://coffeescript.org/) and
Expand All @@ -14,6 +14,11 @@ timer = timers.interval 1000, ->
console.log 'Time is ticking'
```

### Requirements

This module requires ES6 with Node >= 4. For Node < 6 `--harmony` flag is
required.

### Installation

```shell
Expand All @@ -22,8 +27,6 @@ npm install timers-obj

### Usage

Require in your script

```js
const timers = require('timers-obj')
```
Expand Down
17 changes: 12 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,28 @@
"timers"
],
"author": "Piotr Roszatycki",
"license": "Artistic-2.0",
"license": "MIT",
"bugs": {
"url": "https://github.com/dex4er/js-timers-obj/issues"
},
"homepage": "http://github.com/dex4er/js-timers-obj",
"engines": {
"node": ">=4.0.0"
},
"dependencies": {},
"devDependencies": {
"standard": "^7.1.2",
"tap": "^6.3.0"
"chai": "^3.5.0",
"standard": "^8.6.0",
"tap": "^10.2.0",
"tap-given": "^0.2.0"
},
"scripts": {
"test": "tap --timeout 20 test/*.js",
"test-coverage": "tap --coverage --timeout 120 test/*.js",
"test-coverage": "nyc tap --timeout 120 test/*.js",
"test-standard": "standard",
"test-standard-coverage": "npm run test-standard && npm run test-coverage"
},
"bin": {}
"nyc": {
"exclude": []
}
}
194 changes: 116 additions & 78 deletions test/timers-obj.js
Original file line number Diff line number Diff line change
@@ -1,89 +1,127 @@
'use strict'

var t = require('tap')
var timersObj = require('../lib/timers-obj')

var TIMEOUT = 10000

t.plan(6)

t.test('Create and call immediate timer', {timeout: TIMEOUT}, function (t) {
t.plan(4)

var timer = timersObj.immediate(function (a, b, c) {
t.pass('immediate timer is called')
t.same([a, b, c], [1, 2, 3], 'immediate timer is called with correct arguments')
timer.remove()
t.ok(timer, 'immediate timer is removed')
t.end()
}, 1, 2, 3)

t.ok(timer, 'immediate timer is created')
})

t.test('Create and remove immediate timer', {timeout: TIMEOUT}, function (t) {
t.plan(2)

var timer = timersObj.immediate(function () {
t.fail('immediate timer is called')
const timersObj = require('../lib/timers-obj')

/* global Feature, Scenario, Given, When, Then */
const t = require('tap')
require('tap-given')(t)
require('chai').should()

Feature('Test timers-obj module', () => {
Scenario('Create and call immediate timer', function () {
Given('callback with arguments for timer', () => {
this.callback = (done, a, b, c) => {
this.arguments = [a, b, c]
done()
}
})

When('I create immediate timer', done => {
this.timer = timersObj.immediate(this.callback, done, 1, 2, 3)
})

Then('timer called callback with correct arguments', () => {
this.arguments.should.deep.equal([1, 2, 3])
})

Then('timer can be removed', () => {
this.timer.remove()
})
})

t.ok(timer, 'immediate timer is created')
timer.remove()
t.ok(timer, 'immediate timer is removed')
t.end()
})

t.test('Create and call interval timer', {timeout: TIMEOUT}, function (t) {
t.plan(4)

var timer = timersObj.interval(1, function (a, b, c) {
t.pass('interval timer is called')
t.same([a, b, c], [1, 2, 3], 'immediate timer is called with correct arguments')
timer.remove()
t.ok(timer, 'interval timer is removed')
t.end()
}, 1, 2, 3)

t.ok(timer, 'interval timer is created')
})

t.test('Create and remove interval timer', {timeout: TIMEOUT}, function (t) {
t.plan(2)

var timer = timersObj.interval(1, function () {
t.fail('interval timer is called')
Scenario('Create and remove immediate timer', () => {
Given('callback for timer', () => {
this.called = false
this.callback = () => {
this.called = true
}
})

When('I create immediate timer which is immediately removed', () => {
this.timer = timersObj.immediate(this.callback)
this.timer.remove()
})

Then('callback is not called', () => {
this.called.should.be.false
})
})

t.ok(timer, 'interval timer is created')
timer.remove()
t.ok(timer, 'interval timer is removed')
t.end()
})

t.test('Create and call timeout timer', {timeout: TIMEOUT}, function (t) {
t.plan(4)

var timer = timersObj.timeout(1, function (a, b, c) {
t.pass('timeout timer is called')
t.same([a, b, c], [1, 2, 3], 'immediate timer is called with correct arguments')
timer.remove()
t.ok(timer, 'timeout timer is removed')
t.end()
}, 1, 2, 3)

t.ok(timer, 'timeout timer is created')
})
Scenario('Create and call interval timer', function () {
Given('callback with arguments for timer', () => {
this.callback = (done, a, b, c) => {
this.arguments = [a, b, c]
done()
}
})

When('I create interval timer', done => {
this.timer = timersObj.interval(0, this.callback, done, 1, 2, 3)
})

Then('timer called callback with correct arguments', () => {
this.arguments.should.deep.equal([1, 2, 3])
})

Then('timer can be removed', () => {
this.timer.remove()
})
})

t.test('Create and remove timeout timer', {timeout: TIMEOUT}, function (t) {
t.plan(2)
Scenario('Create and remove interval timer', () => {
Given('callback for timer', () => {
this.called = false
this.callback = () => {
this.called = true
}
})

When('I create interval timer which is immediately removed', () => {
this.timer = timersObj.interval(0, this.callback)
this.timer.remove()
})

Then('callback is not called', () => {
this.called.should.be.false
})
})

var timer = timersObj.timeout(1, function () {
t.fail('timeout timer is called')
Scenario('Create and call timeout timer', function () {
Given('callback with arguments for timer', () => {
this.callback = (done, a, b, c) => {
this.arguments = [a, b, c]
done()
}
})

When('I create timeout timer', done => {
this.timer = timersObj.timeout(0, this.callback, done, 1, 2, 3)
})

Then('timer called callback with correct arguments', () => {
this.arguments.should.deep.equal([1, 2, 3])
})

Then('timer can be removed', () => {
this.timer.remove()
})
})

t.ok(timer, 'timeout timer is created')
timer.remove()
t.ok(timer, 'timeout timer is removed')
t.end()
Scenario('Create and remove timeout timer', () => {
Given('callback for timer', () => {
this.called = false
this.callback = () => {
this.called = true
}
})

When('I create timeout timer which is immediately removed', () => {
this.timer = timersObj.timeout(0, this.callback)
this.timer.remove()
})

Then('callback is not called', () => {
this.called.should.be.false
})
})
})

0 comments on commit accbad6

Please sign in to comment.