UNPKG

1.79 kBtext/coffeescriptView Raw
1fs = require "fs"
2
3should = require "should"
4
5FileLoader = require "../lib/FileLoader"
6
7describe "FileLoader", ->
8
9 assetsMock = null
10 loader = null
11 toEditFilePath = process.cwd() + "/test/assets/js/new.coffee"
12 removeNewFiles = ->
13 if fs.existsSync toEditFilePath
14 fs.unlinkSync toEditFilePath
15
16 beforeEach ->
17 removeNewFiles()
18
19 assetsMock =
20 instance:
21 options:
22 helperContext:
23 js: (path) ->
24 # TODO: Something?
25 src: process.cwd() + "/test/assets"
26
27 loader = new FileLoader assetsMock
28
29 afterEach ->
30 removeNewFiles()
31
32 it "can instantiate", ->
33 should.exist loader
34
35 it "can load files from connect-assets src directory", ->
36 fileNames = ["one", "two", "three", "model/book", "view/shelf", "controller/library"]
37 called = 0
38 loader.assetJS = (path) ->
39 called++
40 (path in fileNames).should.equal true
41
42 loader.loadFiles()
43 called.should.equal fileNames.length
44
45 it "skips files with leading '.'", ->
46 filesLoaded = []
47 loader.assetJS = (path) ->
48 filesLoaded.push path
49
50 loader.loadFiles()
51 (".hidden.swp" in filesLoaded).should.equal false
52 ("test/.hidden.swp" in filesLoaded).should.equal false
53
54 it "can monitor when files change", (done) ->
55 fileNames = ["one", "two", "three", "model/book", "view/shelf", "controller/library"]
56 called = 0
57 loader.assetJS = (path) ->
58 called++
59 fileChangedCallback = (err, changedFilePath) ->
60 throw err if err
61
62 changedFilePath.should.equal toEditFilePath
63
64 called.should.equal (fileNames.length + 1)
65
66 done()
67
68 loader.loadFiles()
69
70 called.should.equal fileNames.length
71
72 loader.watchFiles fileChangedCallback, (err, watcher) ->
73 fs.writeFile toEditFilePath, "blah = 123\nother = 456", (err) ->
74 throw err if err
75 # Should have triggered the fileChangedCallback
76