Skip to content

Commit fa494b0

Browse files
committed
refactor tests
1 parent 800bf67 commit fa494b0

3 files changed

Lines changed: 57 additions & 11 deletions

File tree

test/api.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import path from 'path';
33
import wait from 'wait-p';
44
import pify from 'pify';
55
import test from 'ava';
6+
import tempfile from 'tempfile';
7+
import rimraf from 'rimraf';
68
import Timecard from '../dist/index.js';
79

810
const fixtures = path.resolve('test/fixtures');
11+
const temppath = tempfile('.json');
912

1013
test('expose a constructor', t => {
1114
t.is(typeof Timecard, 'function');
@@ -63,7 +66,11 @@ test('allow print before clockout', async t => {
6366
});
6467

6568
test('records total seconds', async t => {
66-
const timecard = new Timecard();
69+
// we have to create a timecard even though we we aren't using the physical
70+
// file in these tests because the clockin and clockout commands require a
71+
// timecard file before executing.
72+
const timecard = new Timecard({prompt: false, filepath: temppath});
73+
await timecard.create();
6774

6875
const fixture = path.join(fixtures, 'timecard.json');
6976
const data = await pify(fs.readFile)(fixture, 'utf8');
@@ -75,3 +82,7 @@ test('records total seconds', async t => {
7582

7683
t.true(timecard.totalSeconds >= 3600 + 60 + 1 + 3);
7784
});
85+
86+
test.after.always('cleanup tempfile', () => {
87+
rimraf.sync(temppath);
88+
});

test/clockin.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,38 @@ import path from 'path';
33
import pify from 'pify';
44
import test from 'ava';
55
import tempfile from 'tempfile';
6+
import rimraf from 'rimraf';
67
import Timecard from '../dist/';
78

89
const fixtures = path.resolve('test/fixtures');
910

11+
const tempfiles = [];
12+
13+
test.beforeEach('cleanup tempfile', async t => {
14+
// Create a blank timecard on the disk so that the operations
15+
// that require a timecard file can find one.
16+
const temppath = tempfile('.json');
17+
tempfiles.push(temppath);
18+
t.context.timecard = new Timecard({prompt: false, filepath: temppath});
19+
await t.context.timecard.create();
20+
});
21+
1022
test('clockin', async t => {
11-
const timecard = new Timecard();
23+
const timecard = t.context.timecard;
1224
timecard.clockin().then(result => {
1325
t.true(result.search('clocked in') > -1);
1426
});
1527
});
1628

1729
test('clockin with message', async t => {
18-
const timecard = new Timecard();
30+
const timecard = t.context.timecard;
1931
await timecard.clockin('yo dude');
2032

2133
t.is(timecard.shifts['1'].messages[0], 'yo dude');
2234
});
2335

2436
test('prevent clockin if clockout pending', async t => {
25-
const timecard = new Timecard();
37+
const timecard = t.context.timecard;
2638

2739
const fixture = path.join(fixtures, 'pendingClockout.json');
2840
const data = await pify(fs.readFile)(fixture, 'utf8');
@@ -49,3 +61,10 @@ test('cant run clockin operations on illegal locations', async t => {
4961
t.true(err.search('You must create a timecard before clocking in') > -1);
5062
});
5163
});
64+
65+
test.after.always('cleanup tempfiles', () => {
66+
tempfiles.forEach(path => {
67+
console.log('deleting temppath:', path);
68+
rimraf.sync(path);
69+
});
70+
});

test/clockout.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,24 @@ import path from 'path';
33
import pify from 'pify';
44
import test from 'ava';
55
import tempfile from 'tempfile';
6+
import rimraf from 'rimraf';
67
import Timecard from '../dist/';
78

89
const fixtures = path.resolve('test/fixtures');
910

10-
test('clockout', async t => {
11-
const timecard = new Timecard();
11+
const tempfiles = [];
12+
13+
test.beforeEach('cleanup tempfile', async t => {
14+
// Create a blank timecard on the disk so that the operations
15+
// that require a timecard file can find one.
16+
const temppath = tempfile('.json');
17+
tempfiles.push(temppath);
18+
t.context.timecard = new Timecard({prompt: false, filepath: temppath});
19+
await t.context.timecard.create();
20+
});
1221

22+
test('clockout', async t => {
23+
const timecard = t.context.timecard;
1324
const fixture = path.join(fixtures, 'pendingClockout.json');
1425
const data = await pify(fs.readFile)(fixture, 'utf8');
1526
await timecard.load(data);
@@ -19,7 +30,7 @@ test('clockout', async t => {
1930
});
2031

2132
test('clockout with message', async t => {
22-
const timecard = new Timecard();
33+
const timecard = t.context.timecard;
2334
await timecard.clockin('foo');
2435
await timecard.clockout('bar');
2536

@@ -28,8 +39,7 @@ test('clockout with message', async t => {
2839
});
2940

3041
test('prevent clockout before clockin', async t => {
31-
const timecard = new Timecard();
32-
42+
const timecard = t.context.timecard;
3343
const fixture = path.join(fixtures, 'blank.json');
3444
const data = await pify(fs.readFile)(fixture, 'utf8');
3545
await timecard.load(data);
@@ -48,8 +58,7 @@ test('report error if attempt to clockout with no timecard', t => {
4858
});
4959

5060
test('prevent clockout if clockin pending', async t => {
51-
const timecard = new Timecard();
52-
61+
const timecard = t.context.timecard;
5362
const fixture = path.join(fixtures, 'blank.json');
5463
const data = await pify(fs.readFile)(fixture, 'utf8');
5564
await timecard.load(data);
@@ -67,3 +76,10 @@ test('cant run clockout operations on illegal locations', async t => {
6776
t.true(err.search('You must create a timecard before clocking out') > -1);
6877
});
6978
});
79+
80+
test.after.always('cleanup tempfiles', () => {
81+
tempfiles.forEach(path => {
82+
console.log('deleting temppath:', path);
83+
rimraf.sync(path);
84+
});
85+
});

0 commit comments

Comments
 (0)