-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lib): allow mask change to trigger onChange (#969)
Co-authored-by: Manuel Meister <[email protected]>
- Loading branch information
1 parent
963eb8e
commit a99eae2
Showing
13 changed files
with
247 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
projects/ngx-mask-lib/src/test/trigger-on-mask-change.cy-spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { mount } from '@jscutlery/cypress-angular/mount'; | ||
|
||
import { CypressTestTriggerOnMaskChangeComponent } from './utils/cypress-test-trigger-on-mask-change.component'; | ||
import { CypressTestMaskModule } from './utils/cypress-test.module'; | ||
|
||
describe('Directive: Mask (Trigger on mask change) [Cypress]', () => { | ||
it('should put back initial value if mask is toggled', async () => { | ||
mount(CypressTestTriggerOnMaskChangeComponent, { | ||
imports: [CypressTestMaskModule], | ||
}); | ||
|
||
cy.get('input#masked').type('7912345678').should('have.value', '7912345678'); | ||
cy.get('.formvalue').should('have.text', '7912345678'); | ||
|
||
cy.get('input[value="ch"]').click(); | ||
cy.get('input#masked').should('have.value', '79 123 45 67'); | ||
cy.get('.formvalue').should('have.text', '791234567'); | ||
|
||
cy.get('input[value="de"]').click(); | ||
cy.get('input#masked').should('have.value', '7912345678'); | ||
cy.get('.formvalue').should('have.text', '7912345678'); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
projects/ngx-mask-lib/src/test/trigger-on-mask-change.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing'; | ||
|
||
import { By } from '@angular/platform-browser'; | ||
import { NgxMaskModule } from '../lib/ngx-mask.module'; | ||
import { ReactiveFormsModule } from '@angular/forms'; | ||
import { TestMaskComponent } from './utils/test-component.component'; | ||
|
||
describe('Directive: Mask (Trigger on mask change)', () => { | ||
let fixture: ComponentFixture<TestMaskComponent>; | ||
let component: TestMaskComponent; | ||
|
||
beforeEach(() => { | ||
TestBed.configureTestingModule({ | ||
declarations: [TestMaskComponent], | ||
imports: [ReactiveFormsModule, NgxMaskModule.forRoot()], | ||
}); | ||
fixture = TestBed.createComponent(TestMaskComponent); | ||
component = fixture.componentInstance; | ||
fixture.detectChanges(); | ||
}); | ||
|
||
afterEach(() => { | ||
fixture.destroy(); | ||
}); | ||
|
||
it('should trigger form value update if mask is changed', async () => { | ||
component.mask = ''; | ||
component.triggerOnMaskChange = true; | ||
fixture.detectChanges(); | ||
|
||
component.form.setValue('7912345678'); | ||
fixture.detectChanges(); | ||
await fixture.whenStable(); | ||
let inputEl = fixture.debugElement.query(By.css('input')); | ||
expect(inputEl.nativeElement.value).toEqual('7912345678'); | ||
expect(component.form.value).toEqual('7912345678'); | ||
|
||
component.mask = '00 000 00 00'; | ||
fixture.detectChanges(); | ||
await fixture.whenStable(); | ||
inputEl = fixture.debugElement.query(By.css('input')); | ||
expect(inputEl.nativeElement.value).toEqual('79 123 45 67'); | ||
expect(component.form.value).toEqual('791234567'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
projects/ngx-mask-lib/src/test/utils/cypress-test-trigger-on-mask-change.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { Component, OnDestroy, OnInit } from '@angular/core'; | ||
import { FormControl } from '@angular/forms'; | ||
import { Subject, takeUntil } from 'rxjs'; | ||
|
||
@Component({ | ||
selector: 'mask-cypress-test-mask', | ||
styles: [ | ||
'code { border: 1px solid #ddd; background-color: #eee; padding: 0 5px; border-radius: 3px; }', | ||
], | ||
template: ` | ||
<div> | ||
<label> | ||
<input type="radio" [formControl]="radio" value="de" /> | ||
DE | ||
</label> | ||
<label> | ||
<input type="radio" [formControl]="radio" value="ch" /> | ||
CH | ||
</label> | ||
<input | ||
id="masked" | ||
[formControl]="form" | ||
[mask]="mask" | ||
[hiddenInput]="false" | ||
[triggerOnMaskChange]="true" | ||
prefix="" | ||
/> | ||
</div> | ||
<div> | ||
<span>Mask: </span><code class="mask">{{ mask }}</code> | ||
<br /> | ||
<span>Form Value: </span><code class="formvalue">{{ form.value }}</code> | ||
</div> | ||
`, | ||
}) | ||
export class CypressTestTriggerOnMaskChangeComponent implements OnInit, OnDestroy { | ||
public mask: string = ''; | ||
|
||
public form: FormControl = new FormControl(''); | ||
|
||
public radio: FormControl = new FormControl('de'); | ||
|
||
private destroyed = new Subject<void>(); | ||
|
||
public ngOnInit(): void { | ||
this.radio.valueChanges.pipe(takeUntil(this.destroyed)).subscribe((value) => { | ||
this.mask = value === 'de' ? '' : '00 000 00 00'; | ||
}); | ||
} | ||
|
||
public ngOnDestroy(): void { | ||
this.destroyed.next(); | ||
this.destroyed.complete(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
projects/ngx-mask-lib/src/test/utils/cypress-test.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { NgxMaskModule } from 'ngx-mask'; | ||
import { CypressTestTriggerOnMaskChangeComponent } from './cypress-test-trigger-on-mask-change.component'; | ||
import { CypressTestMaskComponent } from './cypress-test-component.component'; | ||
|
||
@NgModule({ | ||
imports: [CommonModule, ReactiveFormsModule, FormsModule, NgxMaskModule.forRoot()], | ||
declarations: [CypressTestMaskComponent, CypressTestTriggerOnMaskChangeComponent], | ||
exports: [CypressTestMaskComponent, CypressTestTriggerOnMaskChangeComponent], | ||
}) | ||
export class CypressTestMaskModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.