Skip to content

Commit 269eac4

Browse files
committed
Update change-arrow tests to use data-direction attribute instead of CSS classes
1 parent 4b10507 commit 269eac4

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

assets/js/dashboard/stats/reports/change-arrow.test.tsx

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,56 +3,61 @@ import { render, screen } from '@testing-library/react'
33
import { ChangeArrow } from './change-arrow'
44

55
jest.mock('@heroicons/react/24/solid', () => ({
6-
ArrowUpRightIcon: ({ className }: { className: string }) => (
7-
<span className={className}></span>
6+
ArrowUpRightIcon: ({ className, ...props }: { className: string }) => (
7+
<span className={className} {...props}>
8+
9+
</span>
810
),
9-
ArrowDownRightIcon: ({ className }: { className: string }) => (
10-
<span className={className}></span>
11+
ArrowDownRightIcon: ({ className, ...props }: { className: string }) => (
12+
<span className={className} {...props}>
13+
14+
</span>
1115
)
1216
}))
1317

14-
it('renders green for positive change', () => {
18+
it('renders up arrow for positive change', () => {
1519
render(<ChangeArrow change={1} className="text-xs" metric="visitors" />)
1620

1721
const arrowElement = screen.getByTestId('change-arrow')
1822

1923
expect(arrowElement).toHaveTextContent('↑ 1%')
20-
expect(arrowElement.children[0]).toHaveClass('text-green-500')
24+
expect(arrowElement.children[0]).toHaveAttribute('data-direction', 'up')
2125
})
2226

23-
it('renders red for positive change', () => {
27+
it('renders down arrow for negative change', () => {
2428
render(<ChangeArrow change={-10} className="text-xs" metric="visitors" />)
2529

2630
const arrowElement = screen.getByTestId('change-arrow')
2731

2832
expect(arrowElement).toHaveTextContent('↓ 10%')
29-
expect(arrowElement.children[0]).toHaveClass('text-red-400')
33+
expect(arrowElement.children[0]).toHaveAttribute('data-direction', 'down')
3034
})
3135

32-
it('renders tilde for no change', () => {
36+
it('renders no arrow for no change', () => {
3337
render(<ChangeArrow change={0} className="text-xs" metric="visitors" />)
3438

3539
const arrowElement = screen.getByTestId('change-arrow')
3640

3741
expect(arrowElement).toHaveTextContent('0%')
42+
expect(arrowElement.children).toHaveLength(0)
3843
})
3944

40-
it('inverts colors for positive bounce_rate change', () => {
45+
it('inverts arrow direction for positive bounce_rate change', () => {
4146
render(<ChangeArrow change={15} className="text-xs" metric="bounce_rate" />)
4247

4348
const arrowElement = screen.getByTestId('change-arrow')
4449

4550
expect(arrowElement).toHaveTextContent('↑ 15%')
46-
expect(arrowElement.children[0]).toHaveClass('text-red-400')
51+
expect(arrowElement.children[0]).toHaveAttribute('data-direction', 'up')
4752
})
4853

49-
it('inverts colors for negative bounce_rate change', () => {
54+
it('inverts arrow direction for negative bounce_rate change', () => {
5055
render(<ChangeArrow change={-3} className="text-xs" metric="bounce_rate" />)
5156

5257
const arrowElement = screen.getByTestId('change-arrow')
5358

5459
expect(arrowElement).toHaveTextContent('↓ 3%')
55-
expect(arrowElement.children[0]).toHaveClass('text-green-500')
60+
expect(arrowElement.children[0]).toHaveAttribute('data-direction', 'down')
5661
})
5762

5863
it('renders with text hidden', () => {
@@ -63,7 +68,7 @@ it('renders with text hidden', () => {
6368
const arrowElement = screen.getByTestId('change-arrow')
6469

6570
expect(arrowElement).toHaveTextContent('↓')
66-
expect(arrowElement.children[0]).toHaveClass('text-red-400')
71+
expect(arrowElement.children[0]).toHaveAttribute('data-direction', 'down')
6772
})
6873

6974
it('renders no content with text hidden and 0 change', () => {

assets/js/dashboard/stats/reports/change-arrow.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ export function ChangeArrow({
2222
)
2323

2424
if (change > 0) {
25-
icon = <ArrowUpRightIcon className={arrowClassName} />
25+
icon = <ArrowUpRightIcon className={arrowClassName} data-direction="up" />
2626
} else if (change < 0) {
27-
icon = <ArrowDownRightIcon className={arrowClassName} />
27+
icon = (
28+
<ArrowDownRightIcon className={arrowClassName} data-direction="down" />
29+
)
2830
}
2931

3032
const formattedChange = hideNumber

0 commit comments

Comments
 (0)