|
| 1 | +import { ComponentFixture, TestBed } from '@angular/core/testing'; |
| 2 | +import { Component } from '@angular/core'; |
| 3 | +import { ReactiveFormsModule, FormControl } from '@angular/forms'; |
| 4 | + |
| 5 | +import { NgxMaskModule } from '../lib/ngx-mask.module'; |
| 6 | +import { equal } from './utils/test-functions.component'; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'mask-test-no-validation-attr', |
| 10 | + template: `<input |
| 11 | + id="maska" |
| 12 | + mask="0000" |
| 13 | + [formControl]="form" />`, |
| 14 | +}) |
| 15 | +export class TestMaskNoValidationAttributeComponent { |
| 16 | + public form: FormControl = new FormControl(''); |
| 17 | +} |
| 18 | + |
| 19 | +@Component({ |
| 20 | + selector: 'mask-test-validation-attr', |
| 21 | + template: `<input |
| 22 | + id="maska" |
| 23 | + mask="0000" |
| 24 | + [validation]="validate" |
| 25 | + [formControl]="form" />`, |
| 26 | +}) |
| 27 | +export class TestMaskValidationAttributeComponent { |
| 28 | + public form: FormControl = new FormControl(''); |
| 29 | + public validate: boolean = true; |
| 30 | +} |
| 31 | + |
| 32 | +describe('Directive: Mask (Validation)', () => { |
| 33 | + |
| 34 | + describe('Global validation true, validation attribute on input not specified', () => { |
| 35 | + let fixture: ComponentFixture<TestMaskNoValidationAttributeComponent>; |
| 36 | + let component: TestMaskNoValidationAttributeComponent; |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + TestBed.configureTestingModule({ |
| 40 | + declarations: [TestMaskNoValidationAttributeComponent], |
| 41 | + imports: [ReactiveFormsModule, NgxMaskModule.forRoot({ |
| 42 | + validation: true, |
| 43 | + })], |
| 44 | + }); |
| 45 | + fixture = TestBed.createComponent(TestMaskNoValidationAttributeComponent); |
| 46 | + component = fixture.componentInstance; |
| 47 | + fixture.detectChanges(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('should be marked as not valid if not valid', () => { |
| 51 | + equal('12', '12', fixture); |
| 52 | + expect(component.form.valid).toBeFalse(); |
| 53 | + expect(component.form.hasError('Mask error')).toBeTrue(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should be marked as valid if valid', () => { |
| 57 | + equal('1234', '1234', fixture); |
| 58 | + expect(component.form.valid).toBeTrue(); |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe('Global validation true, validation attribute on input specified', () => { |
| 63 | + let fixture: ComponentFixture<TestMaskValidationAttributeComponent>; |
| 64 | + let component: TestMaskValidationAttributeComponent; |
| 65 | + |
| 66 | + beforeEach(() => { |
| 67 | + TestBed.configureTestingModule({ |
| 68 | + declarations: [TestMaskValidationAttributeComponent], |
| 69 | + imports: [ReactiveFormsModule, NgxMaskModule.forRoot({ |
| 70 | + validation: true, |
| 71 | + })], |
| 72 | + }); |
| 73 | + fixture = TestBed.createComponent(TestMaskValidationAttributeComponent); |
| 74 | + component = fixture.componentInstance; |
| 75 | + fixture.detectChanges(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should be marked as not valid if not valid and validation attribute true', () => { |
| 79 | + equal('12', '12', fixture); |
| 80 | + expect(component.form.valid).toBeFalse(); |
| 81 | + expect(component.form.hasError('Mask error')).toBeTrue(); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should be marked as valid if valid and validation attribute true', () => { |
| 85 | + equal('1234', '1234', fixture); |
| 86 | + expect(component.form.valid).toBeTrue(); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should be marked as valid if not valid and validation attribute false', () => { |
| 90 | + component.validate = false; |
| 91 | + equal('12', '12', fixture); |
| 92 | + expect(component.form.valid).toBeTrue(); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe('Global validation false, validation attribute on input not specified', () => { |
| 97 | + let fixture: ComponentFixture<TestMaskNoValidationAttributeComponent>; |
| 98 | + let component: TestMaskNoValidationAttributeComponent; |
| 99 | + |
| 100 | + beforeEach(() => { |
| 101 | + TestBed.configureTestingModule({ |
| 102 | + declarations: [TestMaskNoValidationAttributeComponent], |
| 103 | + imports: [ReactiveFormsModule, NgxMaskModule.forRoot({ |
| 104 | + validation: false, |
| 105 | + })], |
| 106 | + }); |
| 107 | + fixture = TestBed.createComponent(TestMaskNoValidationAttributeComponent); |
| 108 | + component = fixture.componentInstance; |
| 109 | + fixture.detectChanges(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should be marked as valid if not valid', () => { |
| 113 | + equal('12', '12', fixture); |
| 114 | + expect(component.form.valid).toBeTrue(); |
| 115 | + }); |
| 116 | + |
| 117 | + }); |
| 118 | + |
| 119 | + describe('Global validation false, validation attribute on input specified', () => { |
| 120 | + let fixture: ComponentFixture<TestMaskValidationAttributeComponent>; |
| 121 | + let component: TestMaskValidationAttributeComponent; |
| 122 | + |
| 123 | + beforeEach(() => { |
| 124 | + TestBed.configureTestingModule({ |
| 125 | + declarations: [TestMaskValidationAttributeComponent], |
| 126 | + imports: [ReactiveFormsModule, NgxMaskModule.forRoot({ |
| 127 | + validation: false, |
| 128 | + })], |
| 129 | + }); |
| 130 | + fixture = TestBed.createComponent(TestMaskValidationAttributeComponent); |
| 131 | + component = fixture.componentInstance; |
| 132 | + fixture.detectChanges(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should be marked as not valid if not valid and validation attribute true', () => { |
| 136 | + equal('12', '12', fixture); |
| 137 | + expect(component.form.valid).toBeFalse(); |
| 138 | + expect(component.form.hasError('Mask error')).toBeTrue(); |
| 139 | + }); |
| 140 | + |
| 141 | + it('should be marked as valid if not valid and validation attribute false', () => { |
| 142 | + component.validate = false; |
| 143 | + equal('12', '12', fixture); |
| 144 | + expect(component.form.valid).toBeTrue(); |
| 145 | + }); |
| 146 | + }); |
| 147 | + |
| 148 | +}); |
0 commit comments