Skip to content

Commit

Permalink
chore: add predicate test
Browse files Browse the repository at this point in the history
  • Loading branch information
waterplea committed Mar 2, 2023
1 parent 09b2210 commit f96d362
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 31 deletions.
4 changes: 2 additions & 2 deletions projects/angular/src/lib/maskito.directive.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Directive, ElementRef, Inject, Input, OnChanges, OnDestroy} from '@angular/core';
import {Maskito, MaskitoOptions} from '@maskito/core';
import {Maskito, MASKITO_DEFAULT_OPTIONS, MaskitoOptions} from '@maskito/core';

export type MaskitoPredicate = (
element: HTMLElement,
Expand All @@ -12,7 +12,7 @@ export class MaskitoDirective implements OnDestroy, OnChanges {
private maskedElement: Maskito | null = null;

@Input()
maskito: MaskitoOptions = {mask: []};
maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS;

constructor(
@Inject(ElementRef) private readonly elementRef: ElementRef<HTMLElement>,
Expand Down
26 changes: 0 additions & 26 deletions projects/demo-integrations/cypress/tests/angular/di-approach.cy.ts

This file was deleted.

16 changes: 16 additions & 0 deletions projects/demo-integrations/cypress/tests/angular/predicate.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {DemoPath} from '@demo/path';

describe('@maskito/angular | Predicate', () => {
it('can detect run-time changes', () => {
cy.visit(`/${DemoPath.Cypress}`);
cy.get('#predicate input').should('be.visible').first().as('card');
cy.get('#predicate input').should('be.visible').last().as('name');

cy.get('@card')
.focus()
.type('12341234abcd12341234')
.should('have.value', '1234 1234 1234 1234');

cy.get('@name').focus().type('12341234abcd12341234').should('have.value', 'ABCD');
});
});
8 changes: 8 additions & 0 deletions projects/demo/src/app/app.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ export const appRoutes: Routes = [
title: `Stackblitz Starter`,
},
},
{
path: DemoPath.Cypress,
loadChildren: async () =>
import(`../pages/cypress/cypress.module`).then(m => m.CypressDocPageModule),
data: {
title: `Cypress tests 🤫`,
},
},
{
path: '**',
redirectTo: DemoPath.WhatIsMaskito,
Expand Down
1 change: 1 addition & 0 deletions projects/demo/src/app/demo-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ export const enum DemoPath {
BrowserSupport = 'browser-support',
Changelog = 'changelog',
Stackblitz = 'stackblitz',
Cypress = 'cypress',
}
8 changes: 8 additions & 0 deletions projects/demo/src/pages/cypress/cypress.component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';

@Component({
selector: 'cypress-doc-page',
templateUrl: './cypress.template.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CypressDocPageComponent {}
26 changes: 26 additions & 0 deletions projects/demo/src/pages/cypress/cypress.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import {CommonModule} from '@angular/common';
import {NgModule} from '@angular/core';
import {FormsModule} from '@angular/forms';
import {RouterModule} from '@angular/router';
import {MaskitoModule} from '@maskito/angular';
import {TuiAddonDocModule, tuiGenerateRoutes} from '@taiga-ui/addon-doc';
import {TuiGroupModule} from '@taiga-ui/core';
import {TuiInputModule} from '@taiga-ui/kit';

import {CypressDocPageComponent} from './cypress.component';
import {TestDocExample1} from './examples/1-predicate/component';

@NgModule({
imports: [
CommonModule,
FormsModule,
MaskitoModule,
TuiInputModule,
TuiGroupModule,
TuiAddonDocModule,
RouterModule.forChild(tuiGenerateRoutes(CypressDocPageComponent)),
],
declarations: [CypressDocPageComponent, TestDocExample1],
exports: [CypressDocPageComponent],
})
export class CypressDocPageModule {}
5 changes: 5 additions & 0 deletions projects/demo/src/pages/cypress/cypress.template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<tui-doc-page header="Cypress">
<ng-template pageTab="Tests">
<test-doc-example-1 id="predicate"></test-doc-example-1>
</ng-template>
</tui-doc-page>
38 changes: 38 additions & 0 deletions projects/demo/src/pages/cypress/examples/1-predicate/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {ChangeDetectionStrategy, Component} from '@angular/core';
import {MaskitoPredicate} from '@maskito/angular';
import {MaskitoOptions} from '@maskito/core';

@Component({
selector: 'test-doc-example-1',
templateUrl: './template.html',
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class TestDocExample1 {
value = {
number: '',
name: '',
};

readonly cardMask: MaskitoOptions = {
mask: [
...Array(4).fill(/\d/),
' ',
...Array(4).fill(/\d/),
' ',
...Array(4).fill(/\d/),
' ',
...Array(4).fill(/\d/),
],
};

readonly nameMask: MaskitoOptions = {
mask: /^[a-zA-Z\s]+$/,
postprocessor: ({value, selection}) => ({value: value.toUpperCase(), selection}),
};

readonly cardPredicate: MaskitoPredicate = element =>
element.querySelectorAll('input')[0]!;

readonly namePredicate: MaskitoPredicate = element =>
element.querySelectorAll('input')[1]!;
}
13 changes: 13 additions & 0 deletions projects/demo/src/pages/cypress/examples/1-predicate/template.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<div
tuiGroup
[maskito]="card.focused ? cardMask : nameMask"
[maskitoElement]="card.focused ? cardPredicate : namePredicate"
>
<tui-input
#card
[(ngModel)]="value.number"
>
Card number
</tui-input>
<tui-input [(ngModel)]="value.name">Name</tui-input>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ <h2>Nested input element</h2>
</tui-notification>

<tui-doc-code
filename="custom-input.component.ts"
filename="your.component.ts"
[code]="customInputExample"
></tui-doc-code>
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import {MaskitoOptions} from '@maskito/core';
</custom-input-wrapper>
`,
})
export class CustomInputComponent {
export class YourComponent {
readonly maskitoOptions: MaskitoOptions = {
mask: /^\d+$/,
};

readonly predicate: MaskitoPredicate = element => element.querySelector('input')!;
readonly predicate: MaskitoPredicate = element => element.querySelector('input[id="my-input"]')!;
}
```

0 comments on commit f96d362

Please sign in to comment.