-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Filter the
<AccountSwitcher />
on the staking page to only allow sw…
…itching between relevant accounts (#679) * Add an accountsSelector to the Staking slice * Add tests and filter capabilities to AccountSwitcher * Filter by accounts relevant to staking * Disable manual account index entry * Hide the _whole_ button * Address PR comments
- Loading branch information
1 parent
283817a
commit 1a2a191
Showing
5 changed files
with
287 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import { describe, expect, it, vi } from 'vitest'; | ||
import { AccountSwitcher } from './account-switcher'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
|
||
describe('<AccountSwitcher />', () => { | ||
it('renders the current account', () => { | ||
const { getByLabelText } = render(<AccountSwitcher account={123} onChange={vi.fn()} />); | ||
|
||
expect(getByLabelText('Account #123')).toHaveValue(123); | ||
}); | ||
|
||
describe('changing the account via the input field', () => { | ||
it('calls `onChange` with the new value', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render(<AccountSwitcher account={123} onChange={onChange} />); | ||
|
||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.change(getByLabelText('Account #123'), { target: { value: 456 } }); | ||
expect(onChange).toHaveBeenCalledWith(456); | ||
}); | ||
|
||
it('has aria-disabled=false', () => { | ||
const { getByLabelText } = render(<AccountSwitcher account={123} onChange={vi.fn()} />); | ||
|
||
expect(getByLabelText('Account #123')).toHaveAttribute('aria-disabled', 'false'); | ||
}); | ||
|
||
describe('when a filter has been passed', () => { | ||
it('does not call `onChange` -- i.e., it is effectively disabled', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render( | ||
<AccountSwitcher account={123} onChange={onChange} filter={[1, 2, 3]} />, | ||
); | ||
|
||
fireEvent.change(getByLabelText('Account #123'), { target: { value: 456 } }); | ||
expect(onChange).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('has aria-disabled=true', () => { | ||
const { getByLabelText } = render( | ||
<AccountSwitcher account={123} onChange={vi.fn()} filter={[1, 2, 3]} />, | ||
); | ||
|
||
expect(getByLabelText('Account #123')).toHaveAttribute('aria-disabled', 'true'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('the previous button', () => { | ||
it('calls `onChange` with one less than the current value', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render(<AccountSwitcher account={123} onChange={onChange} />); | ||
|
||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.click(getByLabelText('Previous account')); | ||
expect(onChange).toHaveBeenCalledWith(122); | ||
}); | ||
|
||
it("does not render when we're at account 0", () => { | ||
const { queryByLabelText } = render(<AccountSwitcher account={0} onChange={vi.fn()} />); | ||
|
||
expect(queryByLabelText('Previous account')).toBeNull(); | ||
}); | ||
|
||
describe('when a filter has been passed', () => { | ||
it('calls `onChange` with the next lower account index in the filter', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render( | ||
<AccountSwitcher account={123} onChange={onChange} filter={[123, 100]} />, | ||
); | ||
|
||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.click(getByLabelText('Previous account')); | ||
expect(onChange).toHaveBeenCalledWith(100); | ||
}); | ||
|
||
it("does not render when we're at the lowest account index in the filter", () => { | ||
const { queryByLabelText } = render( | ||
<AccountSwitcher account={100} onChange={vi.fn()} filter={[123, 100]} />, | ||
); | ||
|
||
expect(queryByLabelText('Previous account')).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('the next button', () => { | ||
it('calls `onChange` with one more than the current value', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render(<AccountSwitcher account={123} onChange={onChange} />); | ||
|
||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.click(getByLabelText('Next account')); | ||
expect(onChange).toHaveBeenCalledWith(124); | ||
}); | ||
|
||
it("does not render when we're at the maximum account index", () => { | ||
const { queryByLabelText } = render(<AccountSwitcher account={2 ** 32} onChange={vi.fn()} />); | ||
|
||
expect(queryByLabelText('Next account')).toBeNull(); | ||
}); | ||
|
||
describe('when a filter has been passed', () => { | ||
it('calls `onChange` with the next higher account index in the filter', () => { | ||
const onChange = vi.fn(); | ||
const { getByLabelText } = render( | ||
<AccountSwitcher account={100} onChange={onChange} filter={[123, 100]} />, | ||
); | ||
|
||
expect(onChange).not.toHaveBeenCalled(); | ||
fireEvent.click(getByLabelText('Next account')); | ||
expect(onChange).toHaveBeenCalledWith(123); | ||
}); | ||
|
||
it("does not render when we're at the highest account index in the filter", () => { | ||
const { queryByLabelText } = render( | ||
<AccountSwitcher account={123} onChange={vi.fn()} filter={[123, 100]} />, | ||
); | ||
|
||
expect(queryByLabelText('Next account')).toBeNull(); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters