11import { loadConfig } from "../" ;
22import * as path from "path" ;
33import * as fs from "fs" ;
4+ import {
5+ DefaultClientConfig ,
6+ DefaultServiceConfig ,
7+ DefaultEngineConfig
8+ } from "../config" ;
49
510const makeNestedDir = dir => {
611 if ( fs . existsSync ( dir ) ) return ;
@@ -269,7 +274,7 @@ Object {
269274 } ) ;
270275
271276 describe ( "project type" , ( ) => {
272- it ( "uses passed in type as override " , async ( ) => {
277+ it ( "uses passed in type when config doesnt have client/service " , async ( ) => {
273278 writeFilesToDir ( dir , {
274279 "my.config.js" : `module.exports = { engine: { endpoint: 'http://a.a' } }`
275280 } ) ;
@@ -282,21 +287,138 @@ Object {
282287
283288 expect ( config . isClient ) . toEqual ( true ) ;
284289 } ) ;
285- it ( "infers client projects" , ( ) => { } ) ;
286- it ( "infers service projects" , ( ) => { } ) ;
287- it ( "throws if project type cant be inferred" , ( ) => { } ) ;
290+
291+ it ( "infers client projects from config" , async ( ) => {
292+ writeFilesToDir ( dir , {
293+ "my.config.js" : `module.exports = { client: { service: 'hello' } }`
294+ } ) ;
295+
296+ const config = await loadConfig ( {
297+ configPath : dirPath ,
298+ configFileName : "my.config.js"
299+ } ) ;
300+
301+ expect ( config . isClient ) . toEqual ( true ) ;
302+ } ) ;
303+
304+ it ( "infers service projects from config" , async ( ) => {
305+ writeFilesToDir ( dir , {
306+ "my.config.js" : `module.exports = { service: 'wow' }`
307+ } ) ;
308+
309+ const config = await loadConfig ( {
310+ configPath : dirPath ,
311+ configFileName : "my.config.js"
312+ } ) ;
313+
314+ expect ( config . isService ) . toEqual ( true ) ;
315+ } ) ;
316+
317+ it ( "throws if project type cant be inferred" , done => {
318+ writeFilesToDir ( dir , {
319+ "my.config.js" : `module.exports = { engine: { endpoint: 'http://a.a' } }`
320+ } ) ;
321+
322+ return loadConfig ( {
323+ configPath : dirPath ,
324+ configFileName : "my.config.js"
325+ } ) . catch ( err => {
326+ expect ( err . message ) . toMatch ( / .* U n a b l e t o r e s o l v e p r o j e c t t y p e .* / ) ;
327+ done ( ) ;
328+ } ) ;
329+ } ) ;
288330 } ) ;
289331
290332 describe ( "service name" , ( ) => {
291- it ( "lets config service name take precedence for client project" , ( ) => { } ) ;
292- it ( "lets name passed in take precedence over env var" , ( ) => { } ) ;
293- it ( "uses env var to determine service name when no other options" , ( ) => { } ) ;
333+ it ( "lets config service name take precedence for client project" , async ( ) => {
334+ writeFilesToDir ( dir , {
335+ "my.config.js" : `module.exports = { client: { service: 'hello' } }` ,
336+ ".env" : `ENGINE_API_KEY=service:harambe:54378950jn`
337+ } ) ;
338+
339+ const config = await loadConfig ( {
340+ configPath : dirPath ,
341+ configFileName : "my.config.js" ,
342+ name : "not-it"
343+ } ) ;
344+
345+ expect ( config . client . service ) . toEqual ( "hello" ) ;
346+ } ) ;
347+
348+ it ( "lets name passed in take precedence over env var" , async ( ) => {
349+ writeFilesToDir ( dir , {
350+ "my.config.js" : `module.exports = { client: { } }` ,
351+ ".env" : `ENGINE_API_KEY=service:harambe:54378950jn`
352+ } ) ;
353+
354+ const config = await loadConfig ( {
355+ configPath : dirPath ,
356+ configFileName : "my.config.js" ,
357+ name : "hello"
358+ } ) ;
359+
360+ expect ( config . client . service ) . toEqual ( "hello" ) ;
361+ } ) ;
362+
363+ it ( "uses env var to determine service name when no other options" , async ( ) => {
364+ writeFilesToDir ( dir , {
365+ "my.config.js" : `module.exports = { client: { } }` ,
366+ ".env" : `ENGINE_API_KEY=service:harambe:54378950jn`
367+ } ) ;
368+
369+ const config = await loadConfig ( {
370+ configPath : dirPath ,
371+ configFileName : "my.config.js"
372+ } ) ;
373+
374+ expect ( config . client . service ) . toEqual ( "harambe" ) ;
375+ } ) ;
294376 } ) ;
295377
296378 describe ( "default merging" , ( ) => {
297- it ( "merges service name and default config for client projects" , ( ) => { } ) ;
298- it ( "merges service name and default config for service projects" , ( ) => { } ) ;
299- it ( "merges engine config with projects" , ( ) => { } ) ;
300- it ( "merges defaults in at the end" , ( ) => { } ) ;
379+ it ( "merges service name and default config for client projects" , async ( ) => {
380+ writeFilesToDir ( dir , {
381+ "my.config.js" : `module.exports = { client: { service: 'hello' } }`
382+ } ) ;
383+
384+ const config = await loadConfig ( {
385+ configPath : dirPath ,
386+ configFileName : "my.config.js"
387+ } ) ;
388+
389+ expect ( config . rawConfig . client . includes ) . toEqual (
390+ DefaultClientConfig . includes
391+ ) ;
392+ } ) ;
393+
394+ it ( "merges service name and default config for service projects" , async ( ) => {
395+ writeFilesToDir ( dir , {
396+ "my.config.js" : `module.exports = { service: { name: 'wow' } }`
397+ } ) ;
398+
399+ const config = await loadConfig ( {
400+ configPath : dirPath ,
401+ configFileName : "my.config.js"
402+ } ) ;
403+
404+ expect ( config . rawConfig . service . includes ) . toEqual (
405+ DefaultServiceConfig . includes
406+ ) ;
407+ } ) ;
408+
409+ it ( "merges engine config defaults" , async ( ) => {
410+ writeFilesToDir ( dir , {
411+ "my.config.js" : `module.exports = { client: { service: 'wow' } }`
412+ } ) ;
413+
414+ const config = await loadConfig ( {
415+ configPath : dirPath ,
416+ configFileName : "my.config.js"
417+ } ) ;
418+
419+ expect ( config . rawConfig . engine . endpoint ) . toEqual (
420+ DefaultEngineConfig . endpoint
421+ ) ;
422+ } ) ;
301423 } ) ;
302424} ) ;
0 commit comments