Skip to content

Commit

Permalink
Test(L10nNumberValidatorDirective): complete the cases
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Jan 4, 2018
1 parent 73f33cb commit ceaf32a
Showing 1 changed file with 64 additions and 27 deletions.
91 changes: 64 additions & 27 deletions tests/directives/l10n-number-validator.directive.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { TestBed, ComponentFixture, async } from '@angular/core/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { FormsModule, NgForm, AbstractControl } from '@angular/forms';
import { FormsModule, NgForm, AbstractControl, FormControl } from '@angular/forms';
import { Component } from '@angular/core';

import { L10nNumberValidatorDirective } from './../../angular-l10n';
import { l10nValidateNumber } from "./../../angular-l10n";
import {
L10nConfig,
L10nLoader,
Expand Down Expand Up @@ -50,9 +50,8 @@ describe('L10nNumberValidatorDirective', () => {
});

it('should validate format', async(() => {
fixture.detectChanges();

comp.decimal = "12.34";
comp.digits = "1.2-2";

fixture.detectChanges();
fixture.whenStable().then(() => {
Expand All @@ -71,7 +70,8 @@ describe('L10nNumberValidatorDirective', () => {
}));

it('should validate minValue', async(() => {
comp.decimal = "-123,45";
comp.decimal = "-1234,56";
comp.digits = "1.2-2";

fixture.detectChanges();
fixture.whenStable().then(() => {
Expand All @@ -90,7 +90,8 @@ describe('L10nNumberValidatorDirective', () => {
}));

it('should validate maxValue', async(() => {
comp.decimal = "123,45";
comp.decimal = "1234,56";
comp.digits = "1.2-2";

fixture.detectChanges();
fixture.whenStable().then(() => {
Expand All @@ -110,6 +111,7 @@ describe('L10nNumberValidatorDirective', () => {

it('should validate', async(() => {
comp.decimal = "12,34";
comp.digits = "1.2-2";

fixture.detectChanges();
fixture.whenStable().then(() => {
Expand All @@ -127,31 +129,66 @@ describe('L10nNumberValidatorDirective', () => {
});
}));

it('should validate with thousand separator', async(() => {
comp.decimal = "1.012,34";
comp.maxValue = 1100;
comp.digits = "4.2-2";
it('should validate decimal & thousand separators', () => {
let control: FormControl;

// Valid.
control = new FormControl('12', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('12,34', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('12,3', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('12,', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('0,12', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('12', [l10nValidateNumber('1.0-0')]);
expect(control.valid).toBe(true);
control = new FormControl('1.012,34', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(true);
control = new FormControl('1.012.012,34', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(true);
control = new FormControl('1.012', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('1.012,34', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(true);
control = new FormControl('1.012', [l10nValidateNumber('1.0-0')]);
expect(control.valid).toBe(true);
control = new FormControl('012,34', [l10nValidateNumber('3.2-2')]);
expect(control.valid).toBe(true);
control = new FormControl('01.012,34', [l10nValidateNumber('5.2-2')]);
expect(control.valid).toBe(true);
control = new FormControl('-12,34', [l10nValidateNumber('1.0-2', -1100)]);
expect(control.valid).toBe(true);
control = new FormControl('-1.012,34', [l10nValidateNumber('1.0-2', -1100)]);
expect(control.valid).toBe(true);

// Invalid.
control = new FormControl('1.012,345', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('0.012,34', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('1..012,34', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('10121.012,34', [l10nValidateNumber('1.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('012,34', [l10nValidateNumber('4.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('1.012,34', [l10nValidateNumber('5.2-2')]);
expect(control.valid).toBe(false);
control = new FormControl('1.012', [l10nValidateNumber('5.0-2')]);
expect(control.valid).toBe(false);
control = new FormControl('1.01', [l10nValidateNumber('1.0-2')]);
expect(control.valid).toBe(false);

fixture.detectChanges();
fixture.whenStable().then(() => {
const form: NgForm = fixture.debugElement.children[0].injector.get(NgForm);
const control: AbstractControl | null = form.control.get('decimal');

if (control) {
expect(control.valid).toBe(true);
expect(control.hasError('format')).toBe(false);
expect(control.hasError('minValue')).toBe(false);
expect(control.hasError('maxValue')).toBe(false);
} else {
throw new Error("Control is null");
}
});
}));
});

it('should validate right to left', async(() => {
locale.setDefaultLocale('ar', 'SA');

comp.decimal = "٣٫١٤";
comp.digits = "1.2-2";

fixture.detectChanges();
fixture.whenStable().then(() => {
Expand Down Expand Up @@ -187,7 +224,7 @@ class L10nNumberValidatorComponent {
decimal: string;

digits: string = "1.2-2";
minValue: number = -100;
maxValue: number = 100;
minValue: number = -1100;
maxValue: number = 1100;

}

0 comments on commit ceaf32a

Please sign in to comment.