2018-09-14 10:05:57 +01:00
const testPackage = require ( '../../helpers/test-package' );
const Dgeni = require ( 'dgeni' );
describe ( 'processCliCommands processor' , () => {
2019-01-22 13:52:04 +01:00
let dgeni , injector , processor , createDocMessage ;
const navigationStub = {
docType : 'navigation-json' ,
data : {
SideNav : [{
children : [{ 'url' : 'cli' }]
}]
}
};
beforeEach (() => {
dgeni = new Dgeni ([ testPackage ( 'cli-docs-package' )]);
injector = dgeni . configureInjector ();
processor = injector . get ( 'processCliCommands' );
createDocMessage = injector . get ( 'createDocMessage' );
});
2018-09-14 10:05:57 +01:00
it ( 'should be available on the injector' , () => {
expect ( processor . $process ). toBeDefined ();
});
it ( 'should run after the correct processor' , () => {
expect ( processor . $runAfter ). toEqual ([ 'extra-docs-added' ]);
});
it ( 'should run before the correct processor' , () => {
expect ( processor . $runBefore ). toEqual ([ 'rendering-docs' ]);
});
it ( 'should collect the names (name + aliases)' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [ 'alias1' , 'alias2' ],
options : [],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . names ). toEqual ([ 'name' , 'alias1' , 'alias2' ]);
});
describe ( 'options' , () => {
it ( 'should remove the hidden options' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [
{ name : 'option1' },
{ name : 'option2' , hidden : true },
{ name : 'option3' },
{ name : 'option4' , hidden : true },
],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'option1' }),
jasmine . objectContaining ({ name : 'option3' }),
]);
});
it ( 'should collect the non-hidden positional and named options' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [
{ name : 'named1' },
{ name : 'positional1' , positional : 0 },
{ name : 'named2' , hidden : true },
{ name : 'positional2' , hidden : true , positional : 1 },
],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . positionalOptions ). toEqual ([
jasmine . objectContaining ({ name : 'positional1' , positional : 0 }),
]);
expect ( doc . namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'named1' }),
]);
});
it ( 'should sort the named options into order by name' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [
{ name : 'c' },
{ name : 'a' },
{ name : 'b' },
],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'a' }),
jasmine . objectContaining ({ name : 'b' }),
jasmine . objectContaining ({ name : 'c' }),
]);
});
});
describe ( 'subcommands' , () => {
it ( 'should convert subcommands hash into a collection' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [{
name : 'supercommand' ,
subcommands : {
subcommand1 : {
name : 'subcommand1' ,
options : [
{ name : 'subcommand1-option1' },
{ name : 'subcommand1-option2' },
],
},
subcommand2 : {
name : 'subcommand2' ,
options : [
{ name : 'subcommand2-option1' },
{ name : 'subcommand2-option2' },
],
}
},
}],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . options [ 0 ]. subcommands ). toEqual ([
jasmine . objectContaining ({ name : 'subcommand1' }),
jasmine . objectContaining ({ name : 'subcommand2' }),
]);
});
it ( 'should remove the hidden subcommand options' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [{
name : 'supercommand' ,
subcommands : {
subcommand1 : {
name : 'subcommand1' ,
options : [
{ name : 'subcommand1-option1' },
{ name : 'subcommand1-option2' , hidden : true },
],
},
subcommand2 : {
name : 'subcommand2' ,
options : [
{ name : 'subcommand2-option1' , hidden : true },
{ name : 'subcommand2-option2' },
],
}
},
}],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . options [ 0 ]. subcommands [ 0 ]. namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'subcommand1-option1' }),
]);
expect ( doc . options [ 0 ]. subcommands [ 1 ]. namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'subcommand2-option2' }),
]);
});
it ( 'should collect the non-hidden positional arguments and named options' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [{
name : 'supercommand' ,
subcommands : {
subcommand1 : {
name : 'subcommand1' ,
options : [
{ name : 'subcommand1-option1' },
{ name : 'subcommand1-option2' , positional : 0 },
],
},
subcommand2 : {
name : 'subcommand2' ,
options : [
{ name : 'subcommand2-option1' , hidden : true },
{ name : 'subcommand2-option2' , hidden : true , positional : 1 },
],
}
},
}],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . options [ 0 ]. subcommands [ 0 ]. positionalOptions ). toEqual ([
jasmine . objectContaining ({ name : 'subcommand1-option2' , positional : 0 }),
]);
expect ( doc . options [ 0 ]. subcommands [ 0 ]. namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'subcommand1-option1' }),
]);
expect ( doc . options [ 0 ]. subcommands [ 1 ]. positionalOptions ). toEqual ([]);
expect ( doc . options [ 0 ]. subcommands [ 1 ]. namedOptions ). toEqual ([]);
});
it ( 'should sort the named subcommand options into order by name' , () => {
const doc = {
docType : 'cli-command' ,
name : 'name' ,
commandAliases : [],
options : [{
name : 'supercommand' ,
subcommands : {
subcommand1 : {
name : 'subcommand1' ,
options : [
{ name : 'c' },
{ name : 'a' },
{ name : 'b' },
]
}
}
}],
};
2019-01-22 13:52:04 +01:00
processor . $process ([ doc , navigationStub ]);
2018-09-14 10:05:57 +01:00
expect ( doc . options [ 0 ]. subcommands [ 0 ]. namedOptions ). toEqual ([
jasmine . objectContaining ({ name : 'a' }),
jasmine . objectContaining ({ name : 'b' }),
jasmine . objectContaining ({ name : 'c' }),
]);
});
});
2019-01-22 13:52:04 +01:00
it ( 'should add the command to the CLI node in the navigation doc if there is a first child node with a `cli` url' , () => {
2018-09-14 10:05:57 +01:00
const command = {
docType : 'cli-command' ,
name : 'command1' ,
commandAliases : [ 'alias1' , 'alias2' ],
options : [],
path : 'cli/command1' ,
};
const navigation = {
docType : 'navigation-json' ,
data : {
SideNav : [
{ url : 'some/page' , title : 'Some Page' },
2019-01-22 13:52:04 +01:00
{
title : 'CLI Commands' ,
tooltip : 'Angular CLI command reference' ,
children : [
{
'title' : 'Overview' ,
'url' : 'cli'
}
]
},
{ url : 'other/page' , title : 'Other Page' }
2018-09-14 10:05:57 +01:00
]
}
};
processor . $process ([ command , navigation ]);
expect ( navigation . data . SideNav [ 1 ]. title ). toEqual ( 'CLI Commands' );
expect ( navigation . data . SideNav [ 1 ]. children ). toEqual ([
2019-01-22 13:52:04 +01:00
{ url : 'cli' , title : 'Overview' },
2018-09-14 10:05:57 +01:00
{ url : 'cli/command1' , title : 'ng command1' },
]);
});
2019-01-22 13:52:04 +01:00
it ( 'should complain if there is no child with `cli` url' , () => {
const command = {
docType : 'cli-command' ,
name : 'command1' ,
commandAliases : [ 'alias1' , 'alias2' ],
options : [],
path : 'cli/command1' ,
};
const navigation = {
docType : 'navigation-json' ,
data : {
SideNav : [
{ url : 'some/page' , title : 'Some Page' },
{
title : 'CLI Commands' ,
tooltip : 'Angular CLI command reference' ,
children : [
{
'title' : 'Overview' ,
'url' : 'client'
}
]
},
{ url : 'other/page' , title : 'Other Page' }
]
}
};
expect (() => processor . $process ([ command , navigation ])). toThrowError ( createDocMessage ( 'Missing `cli` url - CLI Commands must include a first child node with url set at `cli`' , navigation ));
});
2018-09-14 10:05:57 +01:00
});