-
Notifications
You must be signed in to change notification settings - Fork 283
/
Copy pathLoadMoreButton.test.js
85 lines (73 loc) · 2.74 KB
/
LoadMoreButton.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import React from 'react';
import { cleanup, fireEvent, render, waitFor } from '@testing-library/react';
import renderer from 'react-test-renderer';
import '@testing-library/jest-dom';
import { LoadMoreButton } from '../LoadMoreButton';
import { TranslationProvider } from '../../../context';
import { mockTranslationContext } from '../../../mock-builders';
describe('LoadMoreButton', () => {
afterEach(cleanup);
it('should render component with default props', () => {
const tree = renderer
.create(
<TranslationProvider value={mockTranslationContext}>
<LoadMoreButton isLoading={false} onClick={() => null} />
</TranslationProvider>,
)
.toJSON();
expect(tree).toMatchInlineSnapshot(`
<div
className="str-chat__load-more-button"
>
<button
aria-label="Load More Channels"
className="str-chat__load-more-button__button str-chat__cta-button"
data-testid="load-more-button"
disabled={false}
onClick={[Function]}
>
Load more
</button>
</div>
`);
});
it('should trigger onClick function when clicked', () => {
const onClickMock = jest.fn();
const { getByTestId } = render(<LoadMoreButton isLoading={false} onClick={onClickMock} />);
fireEvent.click(getByTestId('load-more-button'));
expect(onClickMock).toHaveBeenCalledTimes(1);
});
it('should be disabled and show loading indicator when refreshing is true', () => {
const onClickMock = jest.fn();
const { getByTestId } = render(<LoadMoreButton isLoading={true} onClick={onClickMock} />);
fireEvent.click(getByTestId('load-more-button'));
expect(onClickMock).not.toHaveBeenCalledTimes(1);
const loadingIndicator = getByTestId('load-more-button').querySelector(
'.str-chat__loading-indicator',
);
expect(loadingIndicator).toBeInTheDocument();
});
it('deprecates prop refreshing in favor of isLoading', () => {
const consoleWarnSpy = jest.spyOn(console, 'warn').mockImplementation(() => null);
const onClickMock = jest.fn();
const { getByTestId } = render(
<LoadMoreButton isLoading={false} onClick={onClickMock} refreshing={true} />,
);
fireEvent.click(getByTestId('load-more-button'));
const loadingIndicator = getByTestId('load-more-button').querySelector(
'.str-chat__loading-indicator',
);
consoleWarnSpy.mockRestore();
expect(loadingIndicator).not.toBeInTheDocument();
});
it('should display children', () => {
const { getByText } = render(
<LoadMoreButton isLoading={true} onClick={() => null}>
Test Button
</LoadMoreButton>,
);
waitFor(() => {
expect(getByText('Test Button')).toBeInTheDocument();
});
});
});