Commit ffe0a408 authored by Lyza Danger Gardner's avatar Lyza Danger Gardner Committed by Lyza Gardner

Add `sortKey` and `selectedTab` selectors

parent dcf59584
...@@ -14,7 +14,7 @@ export default function SortMenu() { ...@@ -14,7 +14,7 @@ export default function SortMenu() {
setSortKey: store.setSortKey, setSortKey: store.setSortKey,
})); }));
// The currently-applied sort order // The currently-applied sort order
const sortKey = useStore(store => store.getState().selection.sortKey); const sortKey = useStore(store => store.sortKey());
// All available sorting options. These change depending on current // All available sorting options. These change depending on current
// "tab" or context. // "tab" or context.
const sortKeysAvailable = useStore(store => store.sortKeys()); const sortKeysAvailable = useStore(store => store.sortKeys());
......
...@@ -7,7 +7,6 @@ import { $imports } from '../sort-menu'; ...@@ -7,7 +7,6 @@ import { $imports } from '../sort-menu';
import mockImportedComponents from '../../../test-util/mock-imported-components'; import mockImportedComponents from '../../../test-util/mock-imported-components';
describe('SortMenu', () => { describe('SortMenu', () => {
let fakeState;
let fakeStore; let fakeStore;
const createSortMenu = () => { const createSortMenu = () => {
...@@ -15,14 +14,9 @@ describe('SortMenu', () => { ...@@ -15,14 +14,9 @@ describe('SortMenu', () => {
}; };
beforeEach(() => { beforeEach(() => {
fakeState = {
selection: {
sortKey: 'Location',
},
};
fakeStore = { fakeStore = {
setSortKey: sinon.stub(), setSortKey: sinon.stub(),
getState: sinon.stub().returns(fakeState), sortKey: sinon.stub().returns('Location'),
sortKeys: sinon.stub().returns(['Newest', 'Oldest', 'Location']), sortKeys: sinon.stub().returns(['Newest', 'Oldest', 'Location']),
}; };
...@@ -55,9 +49,7 @@ describe('SortMenu', () => { ...@@ -55,9 +49,7 @@ describe('SortMenu', () => {
const currentSortKeyMenuItem = wrapper const currentSortKeyMenuItem = wrapper
.find('MenuItem') .find('MenuItem')
.filterWhere( .filterWhere(menuItem => menuItem.prop('label') === fakeStore.sortKey());
menuItem => menuItem.prop('label') === fakeState.selection.sortKey
);
assert.isTrue(currentSortKeyMenuItem.prop('isSelected')); assert.isTrue(currentSortKeyMenuItem.prop('isSelected'));
}); });
......
...@@ -529,6 +529,24 @@ const hasAppliedFilter = createSelector( ...@@ -529,6 +529,24 @@ const hasAppliedFilter = createSelector(
!!filterQuery || focusModeActive || hasSelectedAnnotations !!filterQuery || focusModeActive || hasSelectedAnnotations
); );
/**
* Return the currently-selected tab
*
* @return {'annotation'|'note'|'orphan'}
*/
function selectedTab(state) {
return state.selectedTab;
}
/**
* Retrieve the current sort option key
*
* @return {string}
*/
function sortKey(state) {
return state.sortKey;
}
/** /**
* Retrieve applicable sort options for the currently-selected tab. * Retrieve applicable sort options for the currently-selected tab.
* *
...@@ -585,8 +603,8 @@ const threadState = createSelector( ...@@ -585,8 +603,8 @@ const threadState = createSelector(
filters, filters,
forcedVisible: forcedVisibleAnnotations(selection), forcedVisible: forcedVisibleAnnotations(selection),
selected: selectedAnnotations(selection), selected: selectedAnnotations(selection),
sortKey: selection.sortKey, // TODO: This should have a selector sortKey: sortKey(selection),
selectedTab: selection.selectedTab, // TODO: This should have a selector selectedTab: selectedTab(selection),
}; };
return { annotations, route: routeName, selection: selectionState }; return { annotations, route: routeName, selection: selectionState };
} }
...@@ -619,6 +637,8 @@ const threadState = createSelector( ...@@ -619,6 +637,8 @@ const threadState = createSelector(
* @prop {() => boolean} hasAppliedFilter * @prop {() => boolean} hasAppliedFilter
* @prop {() => boolean} hasSelectedAnnotations * @prop {() => boolean} hasSelectedAnnotations
* @prop {() => string[]} selectedAnnotations * @prop {() => string[]} selectedAnnotations
* @prop {() => string} selectedTab
* @prop {() => string} sortKey
* @prop {() => string[]} sortKeys * @prop {() => string[]} sortKeys
* *
* // Root Selectors * // Root Selectors
...@@ -657,6 +677,8 @@ export default { ...@@ -657,6 +677,8 @@ export default {
hasAppliedFilter, hasAppliedFilter,
hasSelectedAnnotations, hasSelectedAnnotations,
selectedAnnotations, selectedAnnotations,
selectedTab,
sortKey,
sortKeys, sortKeys,
}, },
......
...@@ -517,6 +517,21 @@ describe('sidebar/store/modules/selection', () => { ...@@ -517,6 +517,21 @@ describe('sidebar/store/modules/selection', () => {
}); });
}); });
describe('selectedTab', () => {
it('should return the currently-selected tab', () => {
store.selectTab(uiConstants.TAB_NOTES);
assert.equal(store.selectedTab(), uiConstants.TAB_NOTES);
});
});
describe('sortKey', () => {
it('should return the currently-active sort key', () => {
store.setSortKey('Newest');
assert.equal(store.sortKey(), 'Newest');
});
});
describe('ADD_ANNOTATIONS', () => { describe('ADD_ANNOTATIONS', () => {
it('should select the page notes tab if all top-level annotations are page notes', () => { it('should select the page notes tab if all top-level annotations are page notes', () => {
store.dispatch({ store.dispatch({
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment