UNPKG

3.15 kBtext/coffeescriptView Raw
1path = require 'path'
2should = require 'should'
3_ = require 'lodash'
4logger = require 'torch'
5
6core = require '../lib/core'
7
8proj1Dir = path.join __dirname, '../sample/project1'
9
10describe 'core.init', ->
11 before ->
12 @retriever =
13 root: proj1Dir
14
15 beforeEach (done) ->
16 core.reset(done)
17
18 it 'should dynamically load a module based on name', (done) ->
19 data = {greeting: 'hello!'}
20 config =
21 extensions:
22 sample: '*'
23 core.init config, @retriever
24
25 core.request 'sample.echo', data, (err, result) ->
26 should.not.exist err
27 should.exist result
28 result.should.eql data
29 done()
30
31 it "should load the project export as a config", (done) ->
32 internal = require '../lib/core/internal'
33 projectConfig = require proj1Dir
34 should.exist projectConfig
35 core.init {}, @retriever
36 internal.config.should.include projectConfig
37 done()
38
39 it "should load a config override from the config folder", (done) ->
40 core.init {extensions: {sample: '*'}}, @retriever
41
42 sampleExtension = require path.join(proj1Dir, 'node_modules/axiom-sample')
43
44 # Given an extension with a service and corresponding config entry
45 defaultSampleConfig = sampleExtension.config
46 should.exist defaultSampleConfig
47
48 # And a config override in the local project
49 overrideConfig = require path.join(proj1Dir, 'config/sample')
50 should.exist overrideConfig
51
52 expectedConfig = _.merge {}, defaultSampleConfig, overrideConfig
53
54 # When the service is called
55 core.request 'sample.whatsMyContext', {}, (err, config) ->
56 should.not.exist err
57
58 # Then the resulting @config should be the default merged with the override
59 should.exist config
60 config.should.eql expectedConfig
61
62 done()
63
64 it "should expose an injected 'retriever'", (done) ->
65 defaultRetriever = @retriever
66
67 # Given a mock test 'retriever'
68 mockRetriever =
69 root: ''
70 retrieve: (name...) -> {}
71 retrieveExtension: (name...) -> {}
72
73 # And a test service
74 server =
75 services:
76 "run/prepare": (args, fin) ->
77
78 # Then the mock retriever should be included in the context
79 @root.should.eql 'domain/server'
80 (typeof @rel).should.eql 'function'
81 (typeof @retrieve).should.eql 'function'
82
83 fin()
84
85 # When core is initialized with the mock retriever
86 core.init {}, mockRetriever
87
88 # And the service is loaded
89 core.load "server", server
90
91 # And the service is called
92 core.request "server.run/prepare", {}, (err, result) ->
93
94 # It should return without its assertions failing
95 should.not.exist err
96 done()
97
98 it "should link two channels", (done) ->
99
100 config =
101 extensions:
102
103 greeting:
104 services:
105 hello: (args, done) ->
106 done null, {message: 'Hello, world!'}
107
108 routes: [
109 ['link', 'outside.hello', 'greeting.hello']
110 ]
111
112 core.init config, @retriever
113
114 core.request 'outside.hello', {}, (err, result) ->
115 should.not.exist err
116 should.exist result
117 result.should.eql {message: 'Hello, world!'}
118 done()