Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme committed Sep 9, 2020
1 parent 92bf8f2 commit dce3608
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 99 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[![npm badge](https://img.shields.io/npm/v/@guihss/ngx-dynamic-forms?color=green)](https://www.npmjs.com/package/@guihss/ngx-dynamic-forms)
# DynamicForms

A dynamic form generator using class annotations.
A dynamic form generator using Typescript Decorators.

More info in [Dynamic Forms](https://guilherme-fafic.github.io/ngx-dynamic-forms/).

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
"e2e": "ng e2e",
"test-coverage": "ng test --no-watch --no-progress --browsers=ChromeHeadlessCI --code-coverage dynamic-forms"
},
"private": true,
"dependencies": {
Expand Down
6 changes: 6 additions & 0 deletions projects/dynamic-forms-showcase/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = function (config) {
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
singleRun: false,
restartOnFileChange: true
});
Expand Down
31 changes: 0 additions & 31 deletions projects/dynamic-forms-showcase/src/app/app.component.spec.ts

This file was deleted.

3 changes: 3 additions & 0 deletions projects/dynamic-forms-showcase/src/app/app.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ export class AppComponent implements AfterViewInit{

let formResult = this.dynamicForm.getResult();

console.log(formResult);


}


Expand Down

This file was deleted.

4 changes: 3 additions & 1 deletion projects/dynamic-forms/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# DynamicForms

A dynamic form generator using class annotations.
A dynamic form generator using Typescript Decorators.

More info in [Dynamic Forms](https://guilherme-fafic.github.io/ngx-dynamic-forms/).


## How to install
Expand Down
6 changes: 6 additions & 0 deletions projects/dynamic-forms/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ module.exports = function (config) {
logLevel: config.LOG_INFO,
autoWatch: true,
browsers: ['Chrome'],
customLaunchers: {
ChromeHeadlessCI: {
base: 'ChromeHeadless',
flags: ['--no-sandbox']
}
},
singleRun: false,
restartOnFileChange: true
});
Expand Down
3 changes: 2 additions & 1 deletion projects/dynamic-forms/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guihss/ngx-dynamic-forms",
"version": "1.0.1",
"version": "1.0.2",
"peerDependencies": {
"@angular/common": "^9.1.9",
"@angular/core": "^9.1.9"
Expand All @@ -18,6 +18,7 @@
"email": "[email protected]",
"url": "https://github.com/guilherme-fafic"
},
"license": "MIT",
"keywords": [
"dynamic",
"forms",
Expand Down
56 changes: 50 additions & 6 deletions projects/dynamic-forms/src/lib/tests/custom-component-test.spec.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,42 @@
import {DynamicFormsComponent} from "../dynamic-forms.component";
import {async, ComponentFixture, TestBed} from "@angular/core/testing";
import {FormBuilder} from "@angular/forms";
import {FormBuilder, FormControl, ReactiveFormsModule} from "@angular/forms";
import {MatCheckbox} from "@angular/material/checkbox";
import {of} from "rxjs";
import {ConfigurableInput, CustomInput, FormInput} from "dynamic-forms";
import {Component} from "@angular/core";
import {Component, DebugElement} from "@angular/core";


@Component({
selector: '',
template: ''
template: '<h1>{{args}}</h1>'
})
class CustomComponent implements ConfigurableInput {

args;
formControl = new FormControl('');

applyArguments(args: any): any {
this.args = JSON.stringify(args);
if (args.descriptor.args.t3) this.formControl.setValue('test');
}


getFormControl(): any {
return this.formControl;
}

}

class FormObjectMock {

@CustomInput(CustomComponent, {
label: 'Custom Label'
label: 'Custom Label',
args: {t1: 1, t2: 2, t3: true},
required: true,
errorMessage: 'error message'
})
customInput;
customInput = 12;

}

Expand All @@ -35,7 +46,7 @@ describe('Custom Component', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
providers: [FormBuilder, MatCheckbox],
providers: [FormBuilder, ReactiveFormsModule],
declarations: [ DynamicFormsComponent ]
})
.compileComponents();
Expand All @@ -51,4 +62,37 @@ describe('Custom Component', () => {
it('should create', () => {
expect(component).toBeDefined();
});

it('should have correct input', () => {
const rootForm = fixture.debugElement.children[0];
const childElement: HTMLElement = rootForm.children[0].children[0].nativeElement;
expect(rootForm.children.length).toBe(1);
expect(childElement.tagName).toBe("H1");
});

it('should have correct arguments', () => {
const rootForm = fixture.debugElement.children[0];
const childElement: HTMLElement = rootForm.children[0].children[0].nativeElement;

const argsObject = JSON.parse(childElement.innerText);
expect(argsObject).toEqual({
type: 'custom-input',
descriptor: {
label: 'Custom Label',
args: {t1: 1, t2: 2, t3: true},
required: true,
errorMessage: 'error message'
},
defaultValue: 12
});

});

it('should return correct value', () => {
const formResult = component.getResult();

expect(formResult).toEqual({
customInput: 'test'
});
});
});
24 changes: 24 additions & 0 deletions projects/dynamic-forms/src/lib/tests/nested-input.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import {of} from "rxjs";

class NestedInputModel {

@FormInput({label: 'f1'})
field1 = 1;
@FormInput({label: 'f2'})
field2 = 2;

}

class FormObjectMock {
@FormInput({label: 'f1'})
field1 = 1;

@NestedInput('Title', 1)
nestedInput = new NestedInputModel();
Expand Down Expand Up @@ -38,4 +45,21 @@ describe('Nested Input', () => {
it('should create', () => {
expect(component).toBeDefined();
});

it('should have correct input', () => {
const rootForm = fixture.debugElement.children[0];
expect(rootForm.children.length).toBe(4);
});

it('should return correct value', () => {
const formResult = component.getResult();

expect(formResult).toEqual({
field1: 1,
nestedInput: {
field1: 1,
field2: 2
}
});
});
});
10 changes: 0 additions & 10 deletions projects/dynamic-forms/src/lib/utils/utils.spec.ts

This file was deleted.

23 changes: 0 additions & 23 deletions projects/dynamic-forms/src/lib/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,4 @@ export function toPascalCase(text: string) {
}


export function assign(target, src, depth) {
if (depth <= 0) {
return;
}
for (const key of Object.keys(src)) {

if (src[key] && typeof src[key] === 'object') {
if (target[key]) {
assign(target[key], src[key], depth - 1);
}else {
target[key] = src[key];
}

} else {
try {
target[key] = src[key];
}catch (e) {

}

}
}
}

0 comments on commit dce3608

Please sign in to comment.