-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
80 lines (63 loc) · 1.96 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import path from 'path';
import test from 'ava';
import tempfile from 'tempfile';
import delay from 'delay';
import CacheConf from './';
const fixture = '🦄';
test('constructor', t => {
const conf = new CacheConf();
const filePath = conf.path.split(path.sep);
t.is(filePath.pop(), 'config.json');
t.is(filePath.pop(), 'cache-conf-nodejs');
});
test('project name', t => {
const conf = new CacheConf({projectName: 'foo'});
const filePath = conf.path.split(path.sep);
t.is(filePath.pop(), 'config.json');
t.is(filePath.pop(), 'foo-nodejs');
});
test('.set()', async t => {
const conf = new CacheConf({cwd: tempfile()});
conf.set('unicorn', fixture, {maxAge: 100});
t.is(conf.get('unicorn'), fixture);
t.false(conf.isExpired('unicorn'));
await delay(110);
t.falsy(conf.get('unicorn'));
});
test('.set() with object', async t => {
const conf = new CacheConf({cwd: tempfile()});
conf.set({
unicorn: fixture,
foo: 'bar'
}, {maxAge: 100});
t.is(conf.get('unicorn'), fixture);
t.is(conf.get('foo'), 'bar');
await delay(110);
t.falsy(conf.get('unicorn'));
t.falsy(conf.get('foo'));
});
test('.get() with `ignoreMaxAge`', async t => {
const conf = new CacheConf({cwd: tempfile()});
conf.set('unicorn', fixture, {maxAge: 100});
await delay(110);
t.is(conf.get('unicorn', {ignoreMaxAge: true}), fixture);
t.true(conf.isExpired('unicorn'));
t.falsy(conf.get('unicorn'));
});
test('version', t => {
const cwd = tempfile();
const conf = new CacheConf({cwd, version: '1.0.0'});
conf.set('foo', 'bar');
t.is(conf.get('foo'), 'bar');
const conf2 = new CacheConf({cwd, version: '1.0.0'});
t.is(conf2.get('foo'), 'bar');
const conf3 = new CacheConf({cwd, version: '1.1.0'});
t.falsy(conf3.get('foo'));
});
test('record expires when set with `maxAge` equals to `0`', async t => {
const conf = new CacheConf({cwd: tempfile()});
conf.set('unicorn', fixture, {maxAge: 0});
await delay(1);
t.true(conf.isExpired('unicorn'));
t.is(conf.get('unicorn'), undefined);
});