diff --git a/projects/cdk/directives/swipe/test/swipe.directive.spec.ts b/projects/cdk/directives/swipe/test/swipe.directive.spec.ts deleted file mode 100644 index cfbdb22d36f3..000000000000 --- a/projects/cdk/directives/swipe/test/swipe.directive.spec.ts +++ /dev/null @@ -1,97 +0,0 @@ -import {Component, DebugElement} from '@angular/core'; -import {ComponentFixture, TestBed} from '@angular/core/testing'; -import {By} from '@angular/platform-browser'; -import {TuiSwipe, TuiSwipeModule} from '@taiga-ui/cdk'; - -// TODO: need mock Touch -xdescribe('TuiSwipe directive', () => { - @Component({ - template: ` -
- `, - }) - class TestComponent { - swiped = ''; - - onSwipe(swipe: TuiSwipe): void { - this.swiped = swipe.direction; - } - } - - let fixture: ComponentFixture; - let testComponent: TestComponent; - let testElement: DebugElement & {nativeElement: HTMLDivElement}; - - beforeEach(async () => { - TestBed.configureTestingModule({ - imports: [TuiSwipeModule], - declarations: [TestComponent], - }); - await TestBed.compileComponents(); - fixture = TestBed.createComponent(TestComponent); - testComponent = fixture.componentInstance; - testElement = fixture.debugElement.query(By.css('.main')); - - fixture.detectChanges(); - }); - - it('emits events bottom', () => { - sendTouchEvent(0, 0, testElement.nativeElement, 'touchstart', 1); - sendTouchEvent(0, 100, testElement.nativeElement, 'touchend', 1); - - fixture.detectChanges(); - - expect(testComponent.swiped).toBe('bottom'); - }); - - it('emits events right', () => { - sendTouchEvent(0, 0, testElement.nativeElement, 'touchstart', 1); - sendTouchEvent(100, 0, testElement.nativeElement, 'touchend', 1); - - fixture.detectChanges(); - - expect(testComponent.swiped).toBe('right'); - }); - - it('does not emits events due to threshold', () => { - sendTouchEvent(0, 0, testElement.nativeElement, 'touchstart', 1); - sendTouchEvent(0, 20, testElement.nativeElement, 'touchend', 1); - - fixture.detectChanges(); - - expect(testComponent.swiped).toBe(''); - }); - - function sendTouchEvent( - x: number, - y: number, - element: HTMLElement, - eventType: 'touchend' | 'touchstart', - identifier: number, - ): void { - const touchObj = new Touch({ - identifier, - target: element, - clientX: x, - clientY: y, - radiusX: 2.5, - radiusY: 2.5, - rotationAngle: 10, - force: 0.5, - }); - - const touchEvent = new TouchEvent(eventType, { - cancelable: true, - bubbles: true, - touches: [touchObj], - targetTouches: [], - changedTouches: [touchObj], - shiftKey: true, - }); - - element.dispatchEvent(touchEvent); - } -}); diff --git a/projects/demo-cypress/src/tests/swipe.cy.ts b/projects/demo-cypress/src/tests/swipe.cy.ts new file mode 100644 index 000000000000..4f575b55820d --- /dev/null +++ b/projects/demo-cypress/src/tests/swipe.cy.ts @@ -0,0 +1,95 @@ +import {Component} from '@angular/core'; +import {TuiSwipe, TuiSwipeModule} from '@taiga-ui/cdk'; +import {TuiRootModule} from '@taiga-ui/core'; + +describe('TuiSwipe', () => { + let component: TestComponent; + + @Component({ + template: ` + +
+
+ `, + }) + class TestComponent { + swiped = ''; + + onSwipe(swipe: TuiSwipe): void { + this.swiped = swipe.direction; + } + } + + beforeEach(() => + cy + .mount(TestComponent, { + imports: [TuiRootModule, TuiSwipeModule], + }) + .then(wrapper => { + component = wrapper.component; + }), + ); + + it('emits events bottom', () => { + cy.get('section') + .then(query => { + const element = query.get(0); + + sendTouchEvent(0, 0, element, 'touchstart', 1); + sendTouchEvent(0, 100, element, 'touchend', 1); + }) + .then(() => expect(component.swiped).to.eql('bottom')); + }); + + it('emits events right', () => { + cy.get('section') + .then(query => { + const element = query.get(0); + + sendTouchEvent(0, 0, element, 'touchstart', 1); + sendTouchEvent(100, 0, element, 'touchend', 1); + }) + .then(() => expect(component.swiped).to.eql('right')); + }); + + it('does not emit events due to threshold', () => { + cy.get('section') + .then(query => { + const element = query.get(0); + + sendTouchEvent(0, 0, element, 'touchstart', 1); + sendTouchEvent(0, 20, element, 'touchend', 1); + }) + .then(() => expect(component.swiped).to.eql('')); + }); +}); + +function sendTouchEvent( + x: number, + y: number, + element: HTMLElement, + eventType: 'touchend' | 'touchstart', + identifier: number, +): void { + const touchObj = new Touch({ + identifier, + target: element, + clientX: x, + clientY: y, + radiusX: 2.5, + radiusY: 2.5, + rotationAngle: 10, + force: 0.5, + }); + + const touchEvent = new TouchEvent(eventType, { + cancelable: true, + bubbles: true, + touches: [touchObj], + targetTouches: [], + changedTouches: [touchObj], + shiftKey: true, + }); + + element.dispatchEvent(touchEvent); +}