-
Notifications
You must be signed in to change notification settings - Fork 0
/
reload.coffee
99 lines (80 loc) · 2.53 KB
/
reload.coffee
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
fs = require 'fs'
os = require 'os'
rimraf = require 'rimraf'
path = require 'path'
touch = require 'touch'
sinon = require 'sinon'
describe "reload-json", ->
Reload = require '../../lib/reload'
base = path.join os.tmpdir(), 'reload-json-test'
filepath = path.join base, 'test.json'
reload = null
errorcb = null
before (done) ->
fs.mkdir base, done
after (done) ->
rimraf base, done
beforeEach (done) ->
errorcb = sinon.stub()
reload = new Reload
delay: 50
reload.on 'error', errorcb
fs.writeFile filepath, JSON.stringify(file: 'test.json'), (err) ->
done()
afterEach (done) ->
# wait for debounce
setTimeout done, 50
it "returns the same data when reading the same file", (done) ->
readOne = null
readTwo = null
verify = ->
assert.strictEqual readOne, readTwo
assert.notCalled errorcb
done()
reload.load filepath, (err, data) ->
assert.isNull err
readOne = data
verify() if readTwo
reload.load filepath, (err, data) ->
assert.isNull err
readTwo = data
verify() if readOne
it "reads new data immediately after the file is changed", (done) ->
reload.load filepath, (err, readOne) ->
assert.isNull err
touch filepath, (err) ->
assert.isNull err
reload.load filepath, (err, readTwo) ->
assert.isNull err
assert.notStrictEqual readOne, readTwo
assert.notCalled errorcb
done()
it "does not use cached value after file is removed", (done) ->
reload.load filepath, (err, readOne) ->
assert.isNull err
fs.unlink filepath, (err) ->
assert.isNull err
reload.load filepath, (err, readTwo) ->
assert.isNull readTwo
assert.instanceOf err, Error
done()
it "does not trigger reload when file is removed", (done) ->
reload.load filepath, (err, readOne) ->
assert.isNull err
fs.unlink filepath, (err) ->
assert.isNull err
assert.notCalled errorcb
setTimeout (->
assert.notCalled errorcb
done()
), 25
it "reads new data when file is replaced", (done) ->
tmppath = path.join base, 'new.json'
fs.writeFile tmppath, JSON.stringify(file: 'new.json'), (err) ->
reload.load filepath, (err, readOne) ->
assert.isNull err
fs.rename tmppath, filepath, (err) ->
assert.isNull err
reload.load filepath, (err, readTwo) ->
assert.propertyVal readTwo, 'file', 'new.json'
done()