From fb400e909b67cea00f631d49e9a3c0b6c6fcd368 Mon Sep 17 00:00:00 2001 From: Maxime Lafarie Date: Wed, 26 Jun 2019 10:22:24 +0200 Subject: [PATCH 1/3] Add travis CI build, fix tests --- .travis.yml | 33 +++++++++++++++++++ package.json | 9 +++-- .../dynamic-form-question.component.spec.ts | 24 ++++++++++++-- .../dynamic-form-question.component.ts | 10 ++++-- .../dynamic-form.component.spec.ts | 3 +- .../question-control.service.spec.ts | 4 ++- src/app/_services/question.service.spec.ts | 4 ++- src/app/app.component.spec.ts | 9 +++-- 8 files changed, 80 insertions(+), 16 deletions(-) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1e94752 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,33 @@ +sudo: required +dist: trusty +addons: + apt: + update: true + sources: + - google-chrome + packages: + - google-chrome-stable +language: node_js +node_js: + - stable +before_install: + - npm i -g npm + - npm i -g live-server@1 + - npm i -g node-gyp@3 +install: + - npm install +script: + - npm run test +before_script: + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start + - sleep 3 +after_success: + - npm run ghpages +deploy: + provider: pages + skip_cleanup: true + local_dir: dist + github_token: $PUSH_TOKEN + on: + branch: master diff --git a/package.json b/package.json index 5087f32..8d15451 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,16 @@ { - "name": "ngx-smart-form", - "version": "0.0.0", + "name": "angular-dynamic-forms", + "description": "Create on-the-fly forms with Angular", + "version": "1.0.0", + "author": "Maxime LAFARIE ", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "test": "ng test", "lint": "ng lint", - "e2e": "ng e2e" + "e2e": "ng e2e", + "ghpages": "npm i && ng build --prod --aot --no-progress --base-href '/angular-dynamic-forms/'" }, "private": true, "dependencies": { diff --git a/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.spec.ts b/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.spec.ts index 4aa001c..88b4286 100644 --- a/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.spec.ts +++ b/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.spec.ts @@ -1,6 +1,8 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { DynamicFormQuestionComponent } from './dynamic-form-question.component'; +import { DynamicFormQuestionModule } from './dynamic-form-question.module'; +import { FormGroup, FormControl } from '@angular/forms'; describe('DynamicFormQuestionComponent', () => { let component: DynamicFormQuestionComponent; @@ -8,14 +10,32 @@ describe('DynamicFormQuestionComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ DynamicFormQuestionComponent ] + imports: [DynamicFormQuestionModule] }) - .compileComponents(); + .compileComponents(); })); beforeEach(() => { fixture = TestBed.createComponent(DynamicFormQuestionComponent); component = fixture.componentInstance; + + // Mock form + component.form = new FormGroup({ + firstName: new FormControl() + }); + + // Mock question + component.question = { + value: 'Bombasto', + key: 'firstName', + label: 'First name', + required: true, + order: 1, + controlType: 'textbox', + placeholder: '', + iterable: false + }; + fixture.detectChanges(); }); diff --git a/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.ts b/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.ts index 676e97e..bf8fbda 100644 --- a/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.ts +++ b/src/app/_components/common/dynamic-form-question/dynamic-form-question.component.ts @@ -34,15 +34,19 @@ export class DynamicFormQuestionComponent implements OnInit { return this.form.get(this.question.key) as FormArray; } + public get questionIsIterable(): boolean { + return !!this.question && this.question.iterable; + } + public questionControl(index?: number): AbstractControl { - return this.question.iterable ? this.asFormArray(this.form.get(this.question.key)).controls[index] : this.form.get(this.question.key); + return this.questionIsIterable ? this.asFormArray(this.form.get(this.question.key)).controls[index] : this.form.get(this.question.key); } public questionId(index?: number): string { - return this.question.iterable ? `${this.question.key}-${index}` : this.question.key; + return this.questionIsIterable ? `${this.question.key}-${index}` : this.question.key; } public questionLabel(index?: number): string { - return this.question.iterable ? `${this.question.label} n°${index + 1}` : this.question.label; + return this.questionIsIterable ? `${this.question.label} n°${index + 1}` : this.question.label; } } diff --git a/src/app/_components/common/dynamic-form/dynamic-form.component.spec.ts b/src/app/_components/common/dynamic-form/dynamic-form.component.spec.ts index 1d3b4a4..b79fed6 100644 --- a/src/app/_components/common/dynamic-form/dynamic-form.component.spec.ts +++ b/src/app/_components/common/dynamic-form/dynamic-form.component.spec.ts @@ -1,6 +1,7 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing'; import { DynamicFormComponent } from './dynamic-form.component'; +import { DynamicFormModule } from './dynamic-form.module'; describe('DynamicFormComponent', () => { let component: DynamicFormComponent; @@ -8,7 +9,7 @@ describe('DynamicFormComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ - declarations: [ DynamicFormComponent ] + imports: [ DynamicFormModule ] }) .compileComponents(); })); diff --git a/src/app/_services/question-control.service.spec.ts b/src/app/_services/question-control.service.spec.ts index 8edfb9b..c1cfaef 100644 --- a/src/app/_services/question-control.service.spec.ts +++ b/src/app/_services/question-control.service.spec.ts @@ -3,7 +3,9 @@ import { TestBed } from '@angular/core/testing'; import { QuestionControlService } from './question-control.service'; describe('QuestionControlService', () => { - beforeEach(() => TestBed.configureTestingModule({})); + beforeEach(() => TestBed.configureTestingModule({ + providers: [QuestionControlService] + })); it('should be created', () => { const service: QuestionControlService = TestBed.get(QuestionControlService); diff --git a/src/app/_services/question.service.spec.ts b/src/app/_services/question.service.spec.ts index bfa0ca5..87e00aa 100644 --- a/src/app/_services/question.service.spec.ts +++ b/src/app/_services/question.service.spec.ts @@ -3,7 +3,9 @@ import { TestBed } from '@angular/core/testing'; import { QuestionService } from './question.service'; describe('QuestionService', () => { - beforeEach(() => TestBed.configureTestingModule({})); + beforeEach(() => TestBed.configureTestingModule({ + providers: [QuestionService] + })); it('should be created', () => { const service: QuestionService = TestBed.get(QuestionService); diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index caa6302..e19700d 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,16 +1,15 @@ import { TestBed, async } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; +import { AppModule } from './app.module'; describe('AppComponent', () => { beforeEach(async(() => { TestBed.configureTestingModule({ imports: [ - RouterTestingModule - ], - declarations: [ - AppComponent - ], + RouterTestingModule, + AppModule + ] }).compileComponents(); })); From 2ab93c2123b08bc7dddde50bdacefb6bcd2438b3 Mon Sep 17 00:00:00 2001 From: Maxime Lafarie Date: Wed, 26 Jun 2019 10:41:23 +0200 Subject: [PATCH 2/3] Improve travic script --- .travis.yml | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/.travis.yml b/.travis.yml index 1e94752..b93e296 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,19 +11,17 @@ language: node_js node_js: - stable before_install: - - npm i -g npm - - npm i -g live-server@1 - - npm i -g node-gyp@3 + - echo "$TRAVIS_BRANCH" + - echo "$TRAVIS_PULL_REQUEST" + - export DISPLAY=:99.0 + - sh -e /etc/init.d/xvfb start + - export CHROME_BIN=chromium-browser install: - npm install script: + - npm run lint - npm run test -before_script: - - export DISPLAY=:99.0 - - sh -e /etc/init.d/xvfb start - - sleep 3 -after_success: - - npm run ghpages + - if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_PULL_REQUEST" == false ]; then npm run ghpages; fi deploy: provider: pages skip_cleanup: true From 57e479ba94d5745dcbc43cceaaacdc74aa1fe8c8 Mon Sep 17 00:00:00 2001 From: Maxime Lafarie Date: Wed, 26 Jun 2019 10:48:04 +0200 Subject: [PATCH 3/3] Fix karma conf for CI --- karma.conf.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/karma.conf.js b/karma.conf.js index d6816d7..610a1d6 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -26,7 +26,7 @@ module.exports = function (config) { logLevel: config.LOG_INFO, autoWatch: true, browsers: ['Chrome'], - singleRun: false, - restartOnFileChange: true + singleRun: true, + restartOnFileChange: false }); };