generated from Tinkoff/angular-open-source-starter
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react): race condition when
options
are changed before long ele…
…ment predicate is resolved
- Loading branch information
1 parent
2bb4a51
commit 29e5b82
Showing
8 changed files
with
98 additions
and
19 deletions.
There are no files selected for viewing
File renamed without changes.
30 changes: 30 additions & 0 deletions
30
...integrations/src/tests/component-testing/react/async-predicate-options-race/react-app.tsx
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,30 @@ | ||
import type {MaskitoElementPredicate, MaskitoOptions} from '@maskito/core'; | ||
import {useMaskito} from '@maskito/react'; | ||
import type {ComponentType} from 'react'; | ||
import {useEffect, useState} from 'react'; | ||
|
||
import {AwesomeInput} from '../awesome-input'; | ||
|
||
export const SWITCH_OPTIONS_TIME = 1_000; | ||
export const PREDICATE_RESOLVING_TIME = 2_000; | ||
|
||
const numberOptions: MaskitoOptions = {mask: /^\d+$/}; | ||
const engLettersOptions: MaskitoOptions = {mask: /^[a-z]+$/i}; | ||
|
||
const elementPredicate: MaskitoElementPredicate = async (element) => | ||
new Promise((resolve) => { | ||
setTimeout(() => resolve(element.querySelector('.real-input') as HTMLInputElement), PREDICATE_RESOLVING_TIME); | ||
}); | ||
|
||
export const App: ComponentType = () => { | ||
const [options, setOptions] = useState(numberOptions); | ||
const maskRef = useMaskito({options, elementPredicate}); | ||
|
||
useEffect(() => { | ||
setTimeout(() => { | ||
setOptions(engLettersOptions); | ||
}, SWITCH_OPTIONS_TIME); | ||
}, []); | ||
|
||
return <AwesomeInput ref={maskRef} />; | ||
}; |
24 changes: 24 additions & 0 deletions
24
...onent-testing/react/async-predicate-options-race/react-async-predicate-options-race.cy.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,24 @@ | ||
import {TestWrapper} from './angular-wrapper'; | ||
import {PREDICATE_RESOLVING_TIME, SWITCH_OPTIONS_TIME} from './react-app'; | ||
|
||
describe('React async predicate + maskitoOptions race', () => { | ||
beforeEach(() => { | ||
cy.clock(); | ||
cy.mount(TestWrapper); | ||
cy.get('.real-input').should('be.visible').as('textfield'); | ||
}); | ||
|
||
it('can enter any value before no predicate is resolved', () => { | ||
cy.get('@textfield').focus().type('12abc3').should('have.value', '12abc3'); | ||
}); | ||
|
||
it('enabling of the first mask should be skipped if `options` were changed during resolving of element predicate', () => { | ||
cy.smartTick(PREDICATE_RESOLVING_TIME); // predicate is resolved only once for digit cases | ||
cy.get('@textfield').focus().type('12abc3').should('have.value', '12abc3'); | ||
}); | ||
|
||
it('only the last mask should be applied if [maskitoOptions] were changed during resolving of element predicates', () => { | ||
cy.smartTick(SWITCH_OPTIONS_TIME + PREDICATE_RESOLVING_TIME); // enough time to resolve element predicated for both cases | ||
cy.get('@textfield').focus().type('12abc3').should('have.value', 'abc'); | ||
}); | ||
}); |
14 changes: 14 additions & 0 deletions
14
...-integrations/src/tests/component-testing/react/async-predicates-race/angular-wrapper.tsx
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,14 @@ | ||
import {isPlatformBrowser} from '@angular/common'; | ||
import {ChangeDetectionStrategy, Component, ElementRef, inject, PLATFORM_ID} from '@angular/core'; | ||
import {createRoot} from 'react-dom/client'; | ||
|
||
import {App} from './react-app'; | ||
|
||
@Component({standalone: true, selector: 'test-wrapper', template: '', changeDetection: ChangeDetectionStrategy.OnPush}) | ||
export class TestWrapper { | ||
constructor() { | ||
if (isPlatformBrowser(inject(PLATFORM_ID))) { | ||
createRoot(inject(ElementRef).nativeElement).render(<App />); | ||
} | ||
} | ||
} |
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
File renamed without changes.
16 changes: 16 additions & 0 deletions
16
projects/demo-integrations/src/tests/component-testing/react/awesome-input.tsx
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,16 @@ | ||
import {forwardRef, type InputHTMLAttributes} from 'react'; | ||
|
||
const hiddenInputStyles = { | ||
display: 'none', | ||
}; | ||
|
||
export const AwesomeInput = forwardRef<HTMLDivElement, InputHTMLAttributes<HTMLInputElement>>((props, ref) => ( | ||
<div ref={ref}> | ||
<input style={hiddenInputStyles} /> | ||
<input | ||
className="real-input" | ||
{...props} | ||
/> | ||
<input style={hiddenInputStyles} /> | ||
</div> | ||
)); |
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