UNPKG

3.38 kBtext/coffeescriptView Raw
1Stubble = require 'stubble'
2path = require 'path'
3cs = require 'coffee-script'
4filesys = require 'fs'
5specHelper = require './helper'
6
7Package = undefined
8stub = runTest = returned = configs = minify = opts = pack = undefined
9
10# Runs a (partial) test file as if it was written inline into this given test
11# suite.
12runTest = (file)->
13 filename = path.join __dirname, file+'.coffee'
14 eval cs.compile filesys.readFileSync(filename).toString()
15
16describe 'Package', ->
17
18 beforeEach ->
19 returned =
20 compiled: {}
21 response:
22 on: jasmine.createSpy 'response'
23
24 minify = jasmine.createSpy "minify"
25 configs =
26 first: 'z'
27 third: 'c'
28 test_args: " --forceReset"
29 depends: ['jquery.js',
30 'http://www.example.com/underscore.js',
31 'https://myprivate.server.net/my_lib.latest.js']
32 test_depends: ['jasmine.js', 'jquery-test-suite.js']
33 sources: ['foo.js', 'baz.js', 'bar.js']
34 test_server: 'http://localhost:9876'
35 test_timeout: 90
36
37 opts =
38 root: process.cwd()+'/'
39 path: 'jspackle.json'
40 first: 'a'
41 second: 'b'
42
43 stub = specHelper.generateStub configs, opts, returned
44 Package = stub.require __dirname+'/../lib/package'
45 Package.prototype.complete = jasmine.createSpy "process.exit"
46
47
48 describe 'when loading a coffee-script project', ->
49
50 srcs = undefined
51 beforeEach ->
52 opts.coffee = true
53 opts.test_build_source_file = 'foo.js'
54 opts.sources = ['http://www.example.com/foo.js', 'foo.coffee', 'bar.coffee']
55 pack = new Package opts
56 srcs = pack.sources
57
58 it 'should return compiled and HTTP sources', ->
59 expect(srcs.length).toEqual 2
60 expect(srcs[0]).toEqual opts.sources[0]
61 expect(srcs[1]).toEqual opts.test_build_source_file
62
63 it 'should compile coffee from sources', ->
64 expect(stub.stubs.fs.readFileSync).toHaveBeenCalled()
65 expect(stub.stubs.fs.readFileSync.calls.length).toEqual 3 # +1 for the config file
66 expect(stub.stubs.fs.readFileSync.calls[1].args[0]).toEqual 'src/'+opts.sources[1]
67 expect(stub.stubs.fs.readFileSync.calls[2].args[0]).toEqual 'src/'+opts.sources[2]
68 expect(stub.stubs['coffee-script'].compile).toHaveBeenCalled()
69
70 it 'should write the compiled coffee to the build file', ->
71 expect(stub.stubs.fs.writeFileSync).toHaveBeenCalled()
72 expect(stub.stubs.fs.writeFileSync.calls[0].args[0]).toBe opts.test_build_source_file
73 expect(stub.stubs.fs.writeFileSync.calls[0].args[1]).toBe [returned.compiled, returned.compiled].join "\n"
74
75 describe 'when loading configs fails', ->
76
77 beforeEach ->
78 stub.stubs.fs.readFileSync = jasmine.createSpy('fs.readFileSync').andReturn '{"bad_json" : tru'
79 pack = new Package opts
80
81 it 'should exit with code 1', ->
82 expect(pack.exitCode).toEqual 1
83
84 describe 'successfully loading configs', ->
85
86 cmd = undefined
87 beforeEach ->
88 cmd =
89 second: 'x'
90 pack = new Package opts, cmd
91
92 it 'reads the jspackle.json file', ->
93 expect(stub.stubs.fs.readFileSync).toHaveBeenCalled()
94 expect(stub.stubs.fs.readFileSync.calls[0].args[0]).toEqual process.cwd()+'/jspackle.json'
95
96 runTest 'configs'
97 runTest 'properties'
98 runTest 'test'
99 runTest 'build'
100 runTest 'build_depends'
101 runTest 'minify'
102 runTest 'get'
103