Skip to content

Commit

Permalink
Update unit tests for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
marekrozmus committed Jun 24, 2023
1 parent 19ac63b commit 03d4f48
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/LeadingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const LeadingActions = ({ children }) => {
return React.cloneElement(child, {
leading: true,
main: index === 0,
})
});
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/TrailingActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const TrailingActions = ({ children }) => {
return React.cloneElement(child, {
main: index === children.length - 1,
trailing: true,
})
});
});
}

Expand Down
50 changes: 50 additions & 0 deletions src/__tests__/LeadingActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'regenerator-runtime/runtime';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import { render, screen } from '@testing-library/react';
import LeadingActions from '../LeadingActions';
import SwipeAction from '../SwipeAction';
import SwipeableListItem from '../SwipeableListItem';

describe('LeadingActions', () => {
test('should render the action', () => {
render(
<SwipeableListItem
leadingActions={
<LeadingActions>
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
</LeadingActions>
}
/>
);

expect(screen.getByText('Left swipe content')).toBeInTheDocument();
});

test.each([0, 1, 2])('should render %i number of items', itemsCount => {
render(
<SwipeableListItem
leadingActions={
<LeadingActions>
{itemsCount >= 1 && (
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
)}
{itemsCount === 2 && (
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
)}
</LeadingActions>
}
/>
);

expect(screen.queryAllByText('Left swipe content')).toHaveLength(
itemsCount
);
});
});
50 changes: 50 additions & 0 deletions src/__tests__/TrailingActions.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'regenerator-runtime/runtime';
import '@testing-library/jest-dom/extend-expect';
import React from 'react';
import { render, screen } from '@testing-library/react';
import TrailingActions from '../TrailingActions';
import SwipeAction from '../SwipeAction';
import SwipeableListItem from '../SwipeableListItem';

describe('TrailingActions', () => {
test('should render the action', () => {
render(
<SwipeableListItem
trailingActions={
<TrailingActions>
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
</TrailingActions>
}
/>
);

expect(screen.getByText('Left swipe content')).toBeInTheDocument();
});

test.each([0, 1, 2])('should render %i number of items', itemsCount => {
render(
<SwipeableListItem
trailingActions={
<TrailingActions>
{itemsCount >= 1 && (
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
)}
{itemsCount === 2 && (
<SwipeAction onClick={jest.fn()}>
<span>Left swipe content</span>
</SwipeAction>
)}
</TrailingActions>
}
/>
);

expect(screen.queryAllByText('Left swipe content')).toHaveLength(
itemsCount
);
});
});

0 comments on commit 03d4f48

Please sign in to comment.