123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
- import {act, render} from 'sentry-test/reactTestingLibrary';
- import {useSorts} from 'sentry/views/explore/hooks/useSorts';
- import {RouteContext} from 'sentry/views/routeContext';
- describe('useSorts', function () {
- it('allows changing sorts', function () {
- let sorts, setSorts;
- const fields = ['id', 'timestamp'];
- function TestPage() {
- [sorts, setSorts] = useSorts({fields});
- return null;
- }
- const memoryHistory = createMemoryHistory();
- render(
- <Router
- history={memoryHistory}
- render={props => {
- return (
- <RouteContext.Provider value={props}>
- <RouterContext {...props} />
- </RouteContext.Provider>
- );
- }}
- >
- <Route path="/" component={TestPage} />
- </Router>
- );
- expect(sorts).toEqual([
- {
- kind: 'desc',
- field: 'timestamp',
- },
- ]); // default
- act(() =>
- setSorts([
- {
- kind: 'asc',
- field: 'timestamp',
- },
- ])
- );
- expect(sorts).toEqual([
- {
- kind: 'asc',
- field: 'timestamp',
- },
- ]);
- act(() =>
- setSorts([
- {
- kind: 'desc',
- field: 'id',
- },
- ])
- );
- expect(sorts).toEqual([
- {
- kind: 'desc',
- field: 'id',
- },
- ]);
- });
- it('falls back to timestamp desc if possible', function () {
- let sorts, setSorts;
- const fields = ['id', 'timestamp'];
- function TestPage() {
- [sorts, setSorts] = useSorts({fields});
- return null;
- }
- const memoryHistory = createMemoryHistory();
- render(
- <Router
- history={memoryHistory}
- render={props => {
- return (
- <RouteContext.Provider value={props}>
- <RouterContext {...props} />
- </RouteContext.Provider>
- );
- }}
- >
- <Route path="/" component={TestPage} />
- </Router>
- );
- act(() =>
- setSorts([
- {
- kind: 'asc',
- field: 'foo',
- },
- ])
- );
- expect(sorts).toEqual([
- {
- kind: 'desc',
- field: 'timestamp',
- },
- ]);
- });
- it('falls back to first column desc if timestamp is not available', function () {
- let sorts, setSorts;
- const fields = ['id'];
- function TestPage() {
- [sorts, setSorts] = useSorts({fields});
- return null;
- }
- const memoryHistory = createMemoryHistory();
- render(
- <Router
- history={memoryHistory}
- render={props => {
- return (
- <RouteContext.Provider value={props}>
- <RouterContext {...props} />
- </RouteContext.Provider>
- );
- }}
- >
- <Route path="/" component={TestPage} />
- </Router>
- );
- act(() =>
- setSorts([
- {
- kind: 'asc',
- field: 'foo',
- },
- ])
- );
- expect(sorts).toEqual([
- {
- kind: 'desc',
- field: 'id',
- },
- ]);
- });
- });
|