Commit 36e03af8 authored by Robert Knight's avatar Robert Knight

Replace remaining uses of `store.getState()` in UI components

Replace remaining uses of `store.getState()` in UI components with
selectors. This will avoid unnecessary re-rendering after we change
components to use the new `useStoreProxy` hook for reading from the
store. It also simplifies the tests.
parent 2a4045de
...@@ -91,7 +91,7 @@ Tab.propTypes = { ...@@ -91,7 +91,7 @@ Tab.propTypes = {
* @param {SelectionTabsProps} props * @param {SelectionTabsProps} props
*/ */
function SelectionTabs({ isLoading, settings }) { function SelectionTabs({ isLoading, settings }) {
const selectedTab = useStore(store => store.getState().selection.selectedTab); const selectedTab = useStore(store => store.selectedTab());
const noteCount = useStore(store => store.noteCount()); const noteCount = useStore(store => store.noteCount());
const annotationCount = useStore(store => store.annotationCount()); const annotationCount = useStore(store => store.annotationCount());
const orphanCount = useStore(store => store.orphanCount()); const orphanCount = useStore(store => store.orphanCount());
......
...@@ -35,11 +35,7 @@ describe('SelectionTabs', function () { ...@@ -35,11 +35,7 @@ describe('SelectionTabs', function () {
noteCount: sinon.stub().returns(456), noteCount: sinon.stub().returns(456),
orphanCount: sinon.stub().returns(0), orphanCount: sinon.stub().returns(0),
isWaitingToAnchorAnnotations: sinon.stub().returns(false), isWaitingToAnchorAnnotations: sinon.stub().returns(false),
getState: sinon.stub().returns({ selectedTab: sinon.stub().returns(uiConstants.TAB_ANNOTATIONS),
selection: {
selectedTab: uiConstants.TAB_ANNOTATIONS,
},
}),
}; };
$imports.$mock(mockImportedComponents()); $imports.$mock(mockImportedComponents());
...@@ -74,9 +70,7 @@ describe('SelectionTabs', function () { ...@@ -74,9 +70,7 @@ describe('SelectionTabs', function () {
}); });
it('should display notes tab as selected', function () { it('should display notes tab as selected', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: { selectedTab: uiConstants.TAB_NOTES },
});
const wrapper = createComponent({}); const wrapper = createComponent({});
const tabs = wrapper.find('button'); const tabs = wrapper.find('button');
assert.isTrue(tabs.at(1).hasClass('is-selected')); assert.isTrue(tabs.at(1).hasClass('is-selected'));
...@@ -85,9 +79,7 @@ describe('SelectionTabs', function () { ...@@ -85,9 +79,7 @@ describe('SelectionTabs', function () {
}); });
it('should display orphans tab as selected if there is 1 or more orphans', function () { it('should display orphans tab as selected if there is 1 or more orphans', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_ORPHANS);
selection: { selectedTab: uiConstants.TAB_ORPHANS },
});
fakeStore.orphanCount.returns(1); fakeStore.orphanCount.returns(1);
const wrapper = createComponent({}); const wrapper = createComponent({});
const tabs = wrapper.find('button'); const tabs = wrapper.find('button');
...@@ -98,9 +90,7 @@ describe('SelectionTabs', function () { ...@@ -98,9 +90,7 @@ describe('SelectionTabs', function () {
}); });
it('should not display orphans tab if there are 0 orphans', function () { it('should not display orphans tab if there are 0 orphans', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_ORPHANS);
selection: { selectedTab: uiConstants.TAB_ORPHANS },
});
const wrapper = createComponent({}); const wrapper = createComponent({});
const tabs = wrapper.find('button'); const tabs = wrapper.find('button');
assert.equal(tabs.length, 2); assert.equal(tabs.length, 2);
...@@ -140,18 +130,14 @@ describe('SelectionTabs', function () { ...@@ -140,18 +130,14 @@ describe('SelectionTabs', function () {
}); });
it('should not display the new-note-btn when the notes tab is active and the new-note-btn is disabled', function () { it('should not display the new-note-btn when the notes tab is active and the new-note-btn is disabled', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: { selectedTab: uiConstants.TAB_NOTES },
});
const wrapper = createComponent({}); const wrapper = createComponent({});
assert.equal(wrapper.find('NewNoteButton').length, 0); assert.equal(wrapper.find('NewNoteButton').length, 0);
}); });
it('should display the new-note-btn when the notes tab is active and the new-note-btn is enabled', function () { it('should display the new-note-btn when the notes tab is active and the new-note-btn is enabled', function () {
fakeSettings.enableExperimentalNewNoteButton = true; fakeSettings.enableExperimentalNewNoteButton = true;
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: { selectedTab: uiConstants.TAB_NOTES },
});
const wrapper = createComponent({}); const wrapper = createComponent({});
assert.equal(wrapper.find('NewNoteButton').length, 1); assert.equal(wrapper.find('NewNoteButton').length, 1);
}); });
...@@ -165,9 +151,7 @@ describe('SelectionTabs', function () { ...@@ -165,9 +151,7 @@ describe('SelectionTabs', function () {
}); });
it('should not display a message when its loading notes count is 0', function () { it('should not display a message when its loading notes count is 0', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: { selectedTab: uiConstants.TAB_NOTES },
});
fakeStore.noteCount.returns(0); fakeStore.noteCount.returns(0);
const wrapper = createComponent({ const wrapper = createComponent({
isLoading: true, isLoading: true,
...@@ -185,9 +169,7 @@ describe('SelectionTabs', function () { ...@@ -185,9 +169,7 @@ describe('SelectionTabs', function () {
}); });
it('should display the longer version of the no notes message when there are no notes', function () { it('should display the longer version of the no notes message when there are no notes', function () {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: { selectedTab: uiConstants.TAB_NOTES },
});
fakeStore.noteCount.returns(0); fakeStore.noteCount.returns(0);
const wrapper = createComponent({}); const wrapper = createComponent({});
assert.include( assert.include(
...@@ -222,11 +204,7 @@ describe('SelectionTabs', function () { ...@@ -222,11 +204,7 @@ describe('SelectionTabs', function () {
].forEach(({ label, tab }) => { ].forEach(({ label, tab }) => {
it(`should change the selected tab when "${label}" tab is clicked`, () => { it(`should change the selected tab when "${label}" tab is clicked`, () => {
// Pre-select a different tab than the one we are about to click. // Pre-select a different tab than the one we are about to click.
fakeStore.getState.returns({ fakeStore.selectedTab.returns('other-tab');
selection: {
selectedTab: 'other-tab',
},
});
// Make the "Orphans" tab appear. // Make the "Orphans" tab appear.
fakeStore.orphanCount.returns(1); fakeStore.orphanCount.returns(1);
...@@ -240,11 +218,7 @@ describe('SelectionTabs', function () { ...@@ -240,11 +218,7 @@ describe('SelectionTabs', function () {
}); });
it('does not change the selected tab if it is already selected', () => { it('does not change the selected tab if it is already selected', () => {
fakeStore.getState.returns({ fakeStore.selectedTab.returns(uiConstants.TAB_NOTES);
selection: {
selectedTab: uiConstants.TAB_NOTES,
},
});
const wrapper = createComponent({}); const wrapper = createComponent({});
findButton(wrapper, 'Page Notes').simulate('click'); findButton(wrapper, 'Page Notes').simulate('click');
......
...@@ -22,11 +22,7 @@ describe('TopBar', () => { ...@@ -22,11 +22,7 @@ describe('TopBar', () => {
fakeStore = { fakeStore = {
filterQuery: sinon.stub().returns(null), filterQuery: sinon.stub().returns(null),
getState: sinon.stub().returns({ isSidebarPanelOpen: sinon.stub().returns(false),
sidebarPanels: {
activePanelName: null,
},
}),
pendingUpdateCount: sinon.stub().returns(0), pendingUpdateCount: sinon.stub().returns(0),
setFilterQuery: sinon.stub(), setFilterQuery: sinon.stub(),
toggleSidebarPanel: sinon.stub(), toggleSidebarPanel: sinon.stub(),
...@@ -108,12 +104,9 @@ describe('TopBar', () => { ...@@ -108,12 +104,9 @@ describe('TopBar', () => {
}); });
it('displays a help icon active state when help panel active', () => { it('displays a help icon active state when help panel active', () => {
// state returning active sidebar panel as `PANEL_HELP` triggers active class fakeStore.isSidebarPanelOpen
fakeStore.getState = sinon.stub().returns({ .withArgs(uiConstants.PANEL_HELP)
sidebarPanels: { .returns(true);
activePanelName: uiConstants.PANEL_HELP,
},
});
const wrapper = createTopBar(); const wrapper = createTopBar();
const helpButton = getButton(wrapper, 'help'); const helpButton = getButton(wrapper, 'help');
...@@ -220,11 +213,9 @@ describe('TopBar', () => { ...@@ -220,11 +213,9 @@ describe('TopBar', () => {
}); });
it('adds an active-state class to the "Share" icon when the panel is open', () => { it('adds an active-state class to the "Share" icon when the panel is open', () => {
fakeStore.getState.returns({ fakeStore.isSidebarPanelOpen
sidebarPanels: { .withArgs(uiConstants.PANEL_SHARE_ANNOTATIONS)
activePanelName: uiConstants.PANEL_SHARE_ANNOTATIONS, .returns(true);
},
});
const wrapper = createTopBar(); const wrapper = createTopBar();
const shareButton = getButton(wrapper, 'share'); const shareButton = getButton(wrapper, 'share');
......
...@@ -64,8 +64,11 @@ function TopBar({ ...@@ -64,8 +64,11 @@ function TopBar({
togglePanelFn(uiConstants.PANEL_SHARE_ANNOTATIONS); togglePanelFn(uiConstants.PANEL_SHARE_ANNOTATIONS);
}; };
const currentActivePanel = useStore( const isHelpPanelOpen = useStore(store =>
store => store.getState().sidebarPanels.activePanelName store.isSidebarPanelOpen(uiConstants.PANEL_HELP)
);
const isAnnotationsPanelOpen = useStore(store =>
store.isSidebarPanelOpen(uiConstants.PANEL_SHARE_ANNOTATIONS)
); );
/** /**
...@@ -119,7 +122,7 @@ function TopBar({ ...@@ -119,7 +122,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="help" icon="help"
isExpanded={currentActivePanel === uiConstants.PANEL_HELP} isExpanded={isHelpPanelOpen}
onClick={requestHelp} onClick={requestHelp}
title="Help" title="Help"
/> />
...@@ -147,9 +150,7 @@ function TopBar({ ...@@ -147,9 +150,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="share" icon="share"
isExpanded={ isExpanded={isAnnotationsPanelOpen}
currentActivePanel === uiConstants.PANEL_SHARE_ANNOTATIONS
}
onClick={toggleSharePanel} onClick={toggleSharePanel}
title="Share annotations on this page" title="Share annotations on this page"
/> />
...@@ -157,7 +158,7 @@ function TopBar({ ...@@ -157,7 +158,7 @@ function TopBar({
<Button <Button
className="top-bar__icon-button" className="top-bar__icon-button"
icon="help" icon="help"
isExpanded={currentActivePanel === uiConstants.PANEL_HELP} isExpanded={isHelpPanelOpen}
onClick={requestHelp} onClick={requestHelp}
title="Help" title="Help"
/> />
......
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