From 508961288898a5fdd21cc0e26b23ecc8845f9068 Mon Sep 17 00:00:00 2001 From: Alex Inkin Date: Fri, 31 Mar 2023 20:32:35 +0800 Subject: [PATCH] fix(angular): Jest throws `Class constructor DefaultValueAccessor cannot be invoked without 'new'` (#232) --- .github/workflows/test.yml | 2 +- projects/angular/src/lib/maskito.cva.ts | 21 +++++++++++++-------- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f7d933759..7ca2f9eec 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -16,7 +16,7 @@ jobs: uses: ./.github/actions/nodejs - name: Run tests - run: npx nx run-many --target test --all + run: npx nx run-many --target test --all --coverage - uses: codecov/codecov-action@v2 with: diff --git a/projects/angular/src/lib/maskito.cva.ts b/projects/angular/src/lib/maskito.cva.ts index 9b2aac1e2..a38772b34 100644 --- a/projects/angular/src/lib/maskito.cva.ts +++ b/projects/angular/src/lib/maskito.cva.ts @@ -5,24 +5,29 @@ import {MASKITO_DEFAULT_OPTIONS, MaskitoOptions, maskitoTransform} from '@maskit @Directive({ selector: 'input[maskito], textarea[maskito]', providers: [ + DefaultValueAccessor, { provide: NG_VALUE_ACCESSOR, multi: true, - useExisting: MaskitoCva, + useExisting: DefaultValueAccessor, }, ], host: { - '(input)': '$any(this)._handleInput($event.target.value)', - '(blur)': 'onTouched()', - '(compositionstart)': '$any(this)._compositionStart()', - '(compositionend)': '$any(this)._compositionEnd($event.target.value)', + '(input)': '$any(this.accessor)._handleInput($event.target.value)', + '(blur)': 'accessor.onTouched()', + '(compositionstart)': '$any(this.accessor)._compositionStart()', + '(compositionend)': '$any(this.accessor)._compositionEnd($event.target.value)', }, }) -export class MaskitoCva extends DefaultValueAccessor { +export class MaskitoCva { @Input() maskito: MaskitoOptions = MASKITO_DEFAULT_OPTIONS; - override writeValue(value: unknown): void { - super.writeValue(maskitoTransform(String(value ?? ''), this.maskito)); + constructor(readonly accessor: DefaultValueAccessor) { + const original = accessor.writeValue.bind(accessor); + + accessor.writeValue = (value: unknown) => { + original(maskitoTransform(String(value ?? ''), this.maskito)); + }; } }