Skip to content

Commit 6ebf1b1

Browse files
author
Marek Rozmus
committed
Update tests for animations functionality
1 parent 5ca78f5 commit 6ebf1b1

File tree

2 files changed

+145
-1
lines changed

2 files changed

+145
-1
lines changed

src/__tests__/SwipeableList.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,26 @@ test('list rendering with items', () => {
2929
expect(getByText('Item content 2')).toBeInTheDocument();
3030
});
3131

32+
test('list rendering with items when child as function', () => {
33+
const { getByText } = render(
34+
<SwipeableList>
35+
{props => (
36+
<>
37+
<SwipeableListItem {...props}>
38+
<span>Item content 1</span>
39+
</SwipeableListItem>
40+
<SwipeableListItem {...props}>
41+
<span>Item content 2</span>
42+
</SwipeableListItem>
43+
</>
44+
)}
45+
</SwipeableList>
46+
);
47+
48+
expect(getByText('Item content 1')).toBeInTheDocument();
49+
expect(getByText('Item content 2')).toBeInTheDocument();
50+
});
51+
3252
test('blocking swipe on scroll', () => {
3353
const callbackLeft = jest.fn();
3454
const callbackRight = jest.fn();

src/__tests__/SwipeableListItem.test.js

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import '@testing-library/jest-dom/extend-expect';
22
import React from 'react';
33
import { render, cleanup, fireEvent } from '@testing-library/react';
44

5-
import SwipeableListItem from '../SwipeableListItem';
5+
import SwipeableListItem, { ActionAnimation } from '../SwipeableListItem';
66
import {
77
swipeRightMouse,
88
swipeRightTouch,
@@ -327,3 +327,127 @@ test('start and end callbacks triggered if swipe content is defined', () => {
327327
expect(callbackSwipeStart).toHaveBeenCalledTimes(4);
328328
expect(callbackSwipeEnd).toHaveBeenCalledTimes(4);
329329
});
330+
331+
test('if remove animation is applied', () => {
332+
const callbackLeft = jest.fn();
333+
const callbackRight = jest.fn();
334+
335+
const { getByTestId } = render(
336+
<SwipeableListItem
337+
swipeLeft={{
338+
content: <span>Left swipe content</span>,
339+
actionAnimation: ActionAnimation.REMOVE,
340+
action: callbackLeft
341+
}}
342+
swipeRight={{
343+
content: <span>Right swipe content</span>,
344+
actionAnimation: ActionAnimation.REMOVE,
345+
action: callbackRight
346+
}}
347+
>
348+
<span>Item content</span>
349+
</SwipeableListItem>
350+
);
351+
352+
const contentContainer = getByTestId('content');
353+
354+
swipeLeftMouse(contentContainer);
355+
expect(contentContainer).toHaveClass('contentRemove');
356+
357+
swipeLeftTouch(contentContainer);
358+
expect(contentContainer).toHaveClass('contentRemove');
359+
360+
swipeRightMouse(contentContainer);
361+
expect(contentContainer).toHaveClass('contentRemove');
362+
363+
swipeRightTouch(contentContainer);
364+
expect(contentContainer).toHaveClass('contentRemove');
365+
366+
expect(callbackLeft).toBeCalledTimes(2);
367+
expect(callbackRight).toBeCalledTimes(2);
368+
});
369+
370+
test('if return animation is applied', () => {
371+
const callbackLeft = jest.fn();
372+
const callbackRight = jest.fn();
373+
374+
const { getByTestId } = render(
375+
<SwipeableListItem
376+
swipeLeft={{
377+
content: <span>Left swipe content</span>,
378+
actionAnimation: ActionAnimation.RETURN,
379+
action: callbackLeft
380+
}}
381+
swipeRight={{
382+
content: <span>Right swipe content</span>,
383+
action: callbackRight
384+
}}
385+
>
386+
<span>Item content</span>
387+
</SwipeableListItem>
388+
);
389+
390+
const contentContainer = getByTestId('content');
391+
392+
swipeLeftMouse(contentContainer);
393+
expect(contentContainer).toHaveClass('contentReturn');
394+
395+
swipeLeftTouch(contentContainer);
396+
expect(contentContainer).toHaveClass('contentReturn');
397+
398+
swipeRightMouse(contentContainer);
399+
expect(contentContainer).toHaveClass('contentReturn');
400+
401+
swipeRightTouch(contentContainer);
402+
expect(contentContainer).toHaveClass('contentReturn');
403+
404+
expect(callbackLeft).toBeCalledTimes(2);
405+
expect(callbackRight).toBeCalledTimes(2);
406+
});
407+
408+
test('if none animation is applied', () => {
409+
const callbackLeft = jest.fn();
410+
const callbackRight = jest.fn();
411+
412+
const { getByTestId } = render(
413+
<SwipeableListItem
414+
swipeLeft={{
415+
content: <span>Left swipe content</span>,
416+
actionAnimation: ActionAnimation.NONE,
417+
action: callbackLeft
418+
}}
419+
swipeRight={{
420+
content: <span>Right swipe content</span>,
421+
actionAnimation: ActionAnimation.NONE,
422+
action: callbackRight
423+
}}
424+
>
425+
<span>Item content</span>
426+
</SwipeableListItem>
427+
);
428+
429+
const contentContainer = getByTestId('content');
430+
431+
swipeLeftMouse(contentContainer);
432+
expect(contentContainer).toHaveClass('content');
433+
expect(contentContainer).not.toHaveClass('contentReturn');
434+
expect(contentContainer).not.toHaveClass('contentRemove');
435+
436+
swipeLeftTouch(contentContainer);
437+
expect(contentContainer).toHaveClass('content');
438+
expect(contentContainer).not.toHaveClass('contentReturn');
439+
expect(contentContainer).not.toHaveClass('contentRemove');
440+
441+
swipeRightMouse(contentContainer);
442+
expect(contentContainer).toHaveClass('content');
443+
expect(contentContainer).not.toHaveClass('contentReturn');
444+
expect(contentContainer).not.toHaveClass('contentRemove');
445+
446+
swipeRightTouch(contentContainer);
447+
expect(contentContainer).toHaveClass('content');
448+
expect(contentContainer).not.toHaveClass('contentReturn');
449+
expect(contentContainer).not.toHaveClass('contentRemove');
450+
451+
expect(callbackLeft).toBeCalledTimes(2);
452+
expect(callbackRight).toBeCalledTimes(2);
453+
});

0 commit comments

Comments
 (0)