inject_test.coffee
3.67 KB
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
inject = require('../index')
sinon = require('sinon')
expect = require('chai').expect
moduleFixture = """
var Dispatcher = require('lib/dispatcher');
var EventEmitter = require('events').EventEmitter;
var handleAction = require('lib/handle_action');
Dispatcher.register(handleAction, 'MyStore');
"""
describe 'inject-loader', ->
before ->
@context = cacheable: -> return
@fn = inject.bind(@context)
beforeEach ->
@contextMock = sinon.mock(@context)
it 'is cacheable', ->
@contextMock.expects('cacheable').once()
@fn('')
@contextMock.verify()
describe 'loader', ->
describe 'injecting', ->
it 'injects all require statements by default', ->
src = "require('lib/thing')"
replacement = "(injections['lib/thing'] || require('lib/thing'))"
expect(@fn(src)).to.have.string replacement
describe 'queries', ->
describe 'empty', ->
beforeEach ->
@context.query = null
it 'injects all modules', ->
injectedSrc = @fn(moduleFixture)
expect(injectedSrc).to.have.string "var Dispatcher = (injections['lib/dispatcher'] || require('lib/dispatcher'));"
expect(injectedSrc).to.have.string "var EventEmitter = (injections['events'] || require('events')).EventEmitter;"
expect(injectedSrc).to.have.string "var handleAction = (injections['lib/handle_action'] || require('lib/handle_action'));"
describe 'single specific injection', ->
beforeEach ->
@context.query = '?lib/dispatcher'
it 'only injects the module from the query', ->
injectedSrc = @fn(moduleFixture)
expect(injectedSrc).to.have.string "var Dispatcher = (injections['lib/dispatcher'] || require('lib/dispatcher'));"
expect(injectedSrc).to.have.string "var EventEmitter = require('events').EventEmitter;"
expect(injectedSrc).to.have.string "var handleAction = require('lib/handle_action');"
describe 'multiple specific injections', ->
beforeEach ->
@context.query = '?lib/dispatcher&events'
it 'injects all modules from the query', ->
injectedSrc = @fn(moduleFixture)
expect(injectedSrc).to.have.string "var Dispatcher = (injections['lib/dispatcher'] || require('lib/dispatcher'));"
expect(injectedSrc).to.have.string "var EventEmitter = (injections['events'] || require('events')).EventEmitter;"
expect(injectedSrc).to.have.string "var handleAction = require('lib/handle_action');"
describe 'exculde single specific injection', ->
describe 'single exclusion flag', ->
beforeEach ->
@context.query = '?-lib/dispatcher'
it 'injects all modules except the one from the query', ->
injectedSrc = @fn(moduleFixture)
expect(injectedSrc).to.have.string "var Dispatcher = require('lib/dispatcher');"
expect(injectedSrc).to.have.string "var EventEmitter = (injections['events'] || require('events')).EventEmitter;"
expect(injectedSrc).to.have.string "var handleAction = (injections['lib/handle_action'] || require('lib/handle_action'));"
describe 'exculde multiple specific injections', ->
beforeEach ->
@context.query = '?-lib/dispatcher&-events'
it 'injects all modules except the ones from the query', ->
injectedSrc = @fn(moduleFixture)
expect(injectedSrc).to.have.string "var Dispatcher = require('lib/dispatcher');"
expect(injectedSrc).to.have.string "var EventEmitter = require('events').EventEmitter;"
expect(injectedSrc).to.have.string "var handleAction = (injections['lib/handle_action'] || require('lib/handle_action'));"