Skip to content

Commit d43cfcc

Browse files
add test coverage
1 parent 7f3d443 commit d43cfcc

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

src/transaction/components/TransactionButton.test.tsx

+104
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ describe('TransactionButton', () => {
3030
(useShowCallsStatus as Mock).mockReturnValue({
3131
showCallsStatus: vi.fn(),
3232
});
33+
vi.clearAllMocks();
3334
});
3435

3536
it('renders correctly', () => {
@@ -74,6 +75,71 @@ describe('TransactionButton', () => {
7475
expect(text).toBeInTheDocument();
7576
});
7677

78+
it('renders custom error text when error exists', () => {
79+
const mockErrorFunc = vi.fn();
80+
(useTransactionContext as Mock).mockReturnValue({
81+
lifecycleStatus: { statusName: 'init', statusData: null },
82+
errorMessage: 'blah blah',
83+
customStates: {
84+
error: {
85+
text: 'oops',
86+
onClick: mockErrorFunc,
87+
},
88+
},
89+
isLoading: false,
90+
address: '123',
91+
transactions: [{}],
92+
});
93+
render(<TransactionButton text="Transact" />);
94+
const text = screen.getByText('oops');
95+
expect(text).toBeInTheDocument();
96+
const button = screen.getByTestId('ockTransactionButton_Button');
97+
fireEvent.click(button);
98+
expect(mockErrorFunc).toHaveBeenCalled();
99+
});
100+
101+
it('should call custom error handler when error exists', () => {
102+
const mockErrorFunc = vi.fn();
103+
(useTransactionContext as Mock).mockReturnValue({
104+
lifecycleStatus: { statusName: 'init', statusData: null },
105+
errorMessage: 'blah blah',
106+
customStates: {
107+
error: {
108+
text: 'oops',
109+
onClick: mockErrorFunc,
110+
},
111+
},
112+
isLoading: false,
113+
address: '123',
114+
transactions: [{}],
115+
});
116+
render(<TransactionButton text="Transact" />);
117+
const button = screen.getByTestId('ockTransactionButton_Button');
118+
fireEvent.click(button);
119+
expect(mockErrorFunc).toHaveBeenCalled();
120+
});
121+
122+
it('should recall onSubmit when error exists and no custom handler provided', () => {
123+
const mockOnSubmit = vi.fn();
124+
(useTransactionContext as Mock).mockReturnValue({
125+
lifecycleStatus: { statusName: 'init', statusData: null },
126+
errorMessage: 'blah blah',
127+
customStates: {
128+
error: {
129+
text: 'oops',
130+
},
131+
},
132+
isLoading: false,
133+
address: '123',
134+
transactions: [{}],
135+
onSubmit: mockOnSubmit,
136+
});
137+
render(<TransactionButton text="Transact" />);
138+
const button = screen.getByTestId('ockTransactionButton_Button');
139+
fireEvent.click(button);
140+
expect(mockOnSubmit).toHaveBeenCalled();
141+
});
142+
77143
it('should have disabled attribute when isDisabled is true', () => {
78144
const { getByRole } = render(
79145
<TransactionButton disabled={true} text="Submit" />,
@@ -116,6 +182,44 @@ describe('TransactionButton', () => {
116182
expect(showCallsStatus).toHaveBeenCalledWith({ id: '456' });
117183
});
118184

185+
it('should render custom success text when it exists', () => {
186+
const showCallsStatus = vi.fn();
187+
(useShowCallsStatus as Mock).mockReturnValue({ showCallsStatus });
188+
(useTransactionContext as Mock).mockReturnValue({
189+
lifecycleStatus: { statusName: 'init', statusData: null },
190+
receipt: '123',
191+
transactionId: '456',
192+
customStates: {
193+
success: {
194+
text: 'yay',
195+
},
196+
},
197+
});
198+
render(<TransactionButton text="Transact" />);
199+
const button = screen.getByText('yay');
200+
fireEvent.click(button);
201+
expect(showCallsStatus).toHaveBeenCalledWith({ id: '456' });
202+
});
203+
204+
it('should call custom success handler when it exists', () => {
205+
const mockSuccessHandler = vi.fn();
206+
(useTransactionContext as Mock).mockReturnValue({
207+
lifecycleStatus: { statusName: 'init', statusData: null },
208+
receipt: '123',
209+
transactionId: '456',
210+
customStates: {
211+
success: {
212+
text: 'yay',
213+
onClick: mockSuccessHandler,
214+
},
215+
},
216+
});
217+
render(<TransactionButton text="Transact" />);
218+
const button = screen.getByText('yay');
219+
fireEvent.click(button);
220+
expect(mockSuccessHandler).toHaveBeenCalledWith('123');
221+
});
222+
119223
it('should enable button when not in progress, not missing props, and not waiting for receipt', () => {
120224
(useTransactionContext as Mock).mockReturnValue({
121225
isLoading: false,

0 commit comments

Comments
 (0)