Skip to content

Commit d172529

Browse files
committed
auto-generate tests for components jadjoubran/laravel5-angular-material-starter#221
1 parent 597fe82 commit d172529

File tree

5 files changed

+56
-3
lines changed

5 files changed

+56
-3
lines changed

config/generators.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,15 @@
1919
'filter' => '.filter.js',
2020
'pageView' => '.page.html',
2121
],
22+
'tests' => [
23+
'enable' => [
24+
'components' => true,
25+
'services' => true,
26+
],
27+
'source' => [
28+
'root' => 'tests/angular/'
29+
'components' => 'app/components',
30+
'services' => 'services',
31+
],
32+
]
2233
];

src/Console/Commands/AngularComponent.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AngularComponent extends Command
1212
*
1313
* @var string
1414
*/
15-
protected $signature = 'ng:component {name}';
15+
protected $signature = 'ng:component {name} {--no-spec}';
1616

1717
/**
1818
* The console command description.
@@ -40,21 +40,27 @@ public function handle()
4040
{
4141
$name = $this->argument('name');
4242
$studly_name = studly_case($name);
43+
$ng_component = str_replace('-', '-', $name);
4344

4445
$html = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.html.stub');
4546
$js = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.js.stub');
4647
$less = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.less.stub');
48+
$spec = file_get_contents(__DIR__.'/Stubs/AngularComponent/component.spec.js.stub');
4749

4850
$js = str_replace('{{StudlyName}}', $studly_name, $js);
4951
$js = str_replace('{{name}}', $name, $js);
5052

53+
$spec = str_replace('{{ng-component}}', $ng_component, $spec);
54+
5155
$folder = base_path(config('generators.source.root')).'/'.config('generators.source.components').'/'.$name;
5256
if (is_dir($folder)) {
5357
$this->info('Folder already exists');
5458

5559
return false;
5660
}
5761

62+
$spec_folder = base_path(config('generators.tests.source.root')).'/'.config('generators.tests.source.components');
63+
5864
//create folder
5965
File::makeDirectory($folder, 0775, true);
6066

@@ -67,6 +73,11 @@ public function handle()
6773
//create less file (.less)
6874
File::put($folder.'/'.$name.'.less', $less);
6975

76+
if ( !$this->argument('no-spec') && config('generators.tests.enable.components') ){
77+
//create spec file (.component.spec.js)
78+
File::put($spec_folder.'/'.$name.'.component.spec.js', $spec);
79+
}
80+
7081
$this->info('Component created successfully.');
7182
}
7283
}

src/Console/Commands/AngularService.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class AngularService extends Command
1212
*
1313
* @var string
1414
*/
15-
protected $signature = 'ng:service {name}';
15+
protected $signature = 'ng:service {name} {--no-spec}';
1616

1717
/**
1818
* The console command description.
@@ -42,14 +42,23 @@ public function handle()
4242
$studly_name = studly_case($name);
4343

4444
$js = file_get_contents(__DIR__.'/Stubs/AngularService/service.js.stub');
45+
$spec = file_get_contents(__DIR__.'/Stubs/AngularService/service.spec.js.stub');
4546

4647
$js = str_replace('{{StudlyName}}', $studly_name, $js);
48+
$spec = str_replace('{{StudlyName}}', $studly_name, $spec);
4749

4850
$folder = base_path(config('generators.source.root')).'/'.config('generators.source.services');
4951

50-
//create service (.js)
52+
$spec_folder = base_path(config('generators.tests.source.root')).'/'.config('generators.tests.source.services');
53+
54+
//create service (.service.js)
5155
File::put($folder.'/'.$name.config('generators.prefix.service'), $js);
5256

57+
if ( !$this->argument('no-spec') && config('generators.tests.enable.services') ){
58+
//create spec (.service.spec.js)
59+
File::put($spec_folder.'/'.$name.'.service.spec.js', $spec);
60+
}
61+
5362
$this->info('Service created successfully.');
5463
}
5564
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ngDescribe({
2+
name: 'Test {{ng-component}} component',
3+
modules: 'app',
4+
element: '<{{ng-component}}></{{ng-component}}>',
5+
tests: function (deps) {
6+
7+
it('basic test', () => {
8+
//
9+
});
10+
}
11+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ngDescribe({
2+
name: 'Test {{StudlyName}}Service',
3+
modules: 'app',
4+
inject: '{{StudlyName}}Service',
5+
tests: function (deps) {
6+
7+
it('basic test', () => {
8+
//
9+
});
10+
}
11+
});

0 commit comments

Comments
 (0)