Unverified Commit e7a363ab authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #1317 from hypothesis/namespace-direct-linked-module

Namespace the direct-linked module
parents 4bc0827e f89877a0
......@@ -151,7 +151,7 @@ function SidebarContentController(
this.scrollTo = scrollToAnnotation;
this.selectedGroupUnavailable = function() {
return store.getState().directLinkedGroupFetchFailed;
return store.getRootState().directLinked.directLinkedGroupFetchFailed;
};
this.selectedAnnotationUnavailable = function() {
......@@ -169,7 +169,7 @@ function SidebarContentController(
// If user has not landed on a direct linked annotation
// don't show the CTA.
if (!store.getState().directLinkedAnnotationId) {
if (!store.getRootState().directLinked.directLinkedAnnotationId) {
return false;
}
......
......@@ -179,7 +179,8 @@ function groups(
// If there is a direct-linked annotation, fetch the annotation in case
// the associated group has not already been fetched and we need to make
// an additional request for it.
const directLinkedAnnId = store.getState().directLinkedAnnotationId;
const directLinkedAnnId = store.getRootState().directLinked
.directLinkedAnnotationId;
let directLinkedAnnApi = null;
if (directLinkedAnnId) {
directLinkedAnnApi = api.annotation
......@@ -193,7 +194,8 @@ function groups(
// If there is a direct-linked group, add an API request to get that
// particular group since it may not be in the set of groups that are
// fetched by other requests.
const directLinkedGroupId = store.getState().directLinkedGroupId;
const directLinkedGroupId = store.getRootState().directLinked
.directLinkedGroupId;
let directLinkedGroupApi = null;
if (directLinkedGroupId) {
directLinkedGroupApi = fetchGroup({
......
......@@ -60,8 +60,10 @@ describe('groups', function() {
mainFrame: { uri: 'http://example.org' },
focusedGroup: null,
groups: [],
directLinkedGroupId: null,
directLinkedAnnotationId: null,
directLinked: {
directLinkedGroupId: null,
directLinkedAnnotationId: null,
},
},
{
focusGroup: sinon.stub(),
......@@ -164,7 +166,9 @@ describe('groups', function() {
scopes: { enforced: true, uri_patterns: ['http://foo.com'] },
};
fakeStore.setState({
directLinkedGroupId: outOfScopeEnforcedGroup.id,
directLinked: {
directLinkedGroupId: outOfScopeEnforcedGroup.id,
},
});
fakeApi.group.read.returns(Promise.resolve(outOfScopeEnforcedGroup));
return svc.load().then(groups => {
......@@ -180,7 +184,11 @@ describe('groups', function() {
it('catches error from api.group.read request', () => {
const svc = service();
fakeLocalStorage.getItem.returns(dummyGroups[0].id);
fakeStore.setState({ directLinkedGroupId: 'does-not-exist' });
fakeStore.setState({
directLinked: {
directLinkedGroupId: 'does-not-exist',
},
});
fakeApi.group.read.returns(
Promise.reject(
new Error(
......@@ -219,7 +227,11 @@ describe('groups', function() {
const svc = service();
// Set the direct-linked group to dummyGroups[0].
fakeStore.setState({ directLinkedGroupId: dummyGroups[0].id });
fakeStore.setState({
directLinkedGroupId: {
directLinkedGroupId: dummyGroups[0].id,
},
});
fakeApi.group.read.returns(Promise.resolve(dummyGroups[0]));
// Include the dummyGroups[0] in the featured groups.
......@@ -235,7 +247,11 @@ describe('groups', function() {
it('combines groups from all 3 endpoints if there is a selectedGroup', () => {
const svc = service();
fakeStore.setState({ directLinkedGroupId: 'selected-id' });
fakeStore.setState({
directLinked: {
directLinkedGroupId: 'selected-id',
},
});
const groups = [
{ id: 'groupa', name: 'GroupA' },
{ id: 'groupb', name: 'GroupB' },
......@@ -254,7 +270,11 @@ describe('groups', function() {
it('passes the direct-linked group id from the store to the api.group.read call', () => {
const svc = service();
fakeStore.setState({ directLinkedGroupId: 'selected-id' });
fakeStore.setState({
directLinked: {
directLinkedGroupId: 'selected-id',
},
});
const group = { id: 'selected-id', name: 'Selected Group' };
fakeApi.profile.groups.read.returns(Promise.resolve([]));
......@@ -284,7 +304,11 @@ describe('groups', function() {
fakeApi.groups.list.returns(
Promise.resolve([{ id: 'groupa', name: 'GroupA' }])
);
fakeStore.setState({ directLinkedGroupId: 'group-id' });
fakeStore.setState({
directLinked: {
directLinkedGroupId: 'group-id',
},
});
return svc.load().then(() => {
assert.calledWith(
......@@ -313,8 +337,10 @@ describe('groups', function() {
it("sets the direct-linked annotation's group to take precedence over the group saved in local storage and the direct-linked group", () => {
const svc = service();
fakeStore.setState({
directLinkedAnnotationId: 'ann-id',
directLinkedGroupId: dummyGroups[1].id,
directLinked: {
directLinkedAnnotationId: 'ann-id',
directLinkedGroupId: dummyGroups[1].id,
},
});
fakeLocalStorage.getItem.returns(dummyGroups[0].id);
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
......@@ -331,7 +357,11 @@ describe('groups', function() {
it("sets the focused group to the direct-linked annotation's group", () => {
const svc = service();
fakeStore.setState({ directLinkedAnnotationId: 'ann-id' });
fakeStore.setState({
directLinked: {
directLinkedAnnotationId: 'ann-id',
},
});
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
fakeLocalStorage.getItem.returns(dummyGroups[0].id);
fakeApi.annotation.get.returns(
......@@ -347,7 +377,11 @@ describe('groups', function() {
it('sets the direct-linked group to take precedence over the group saved in local storage', () => {
const svc = service();
fakeStore.setState({ directLinkedGroupId: dummyGroups[1].id });
fakeStore.setState({
directLinked: {
directLinkedGroupId: dummyGroups[1].id,
},
});
fakeLocalStorage.getItem.returns(dummyGroups[0].id);
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
return svc.load().then(() => {
......@@ -357,7 +391,11 @@ describe('groups', function() {
it('sets the focused group to the direct-linked group', () => {
const svc = service();
fakeStore.setState({ directLinkedGroupId: dummyGroups[1].id });
fakeStore.setState({
directLinked: {
directLinkedGroupId: dummyGroups[1].id,
},
});
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
return svc.load().then(() => {
assert.calledWith(fakeStore.focusGroup, dummyGroups[1].id);
......@@ -366,7 +404,11 @@ describe('groups', function() {
it('clears the directLinkedGroupFetchFailed state if loading a direct-linked group', () => {
const svc = service();
fakeStore.setState({ directLinkedGroupId: dummyGroups[1].id });
fakeStore.setState({
directLinked: {
directLinkedGroupId: dummyGroups[1].id,
},
});
fakeApi.groups.list.returns(Promise.resolve(dummyGroups));
return svc.load().then(() => {
assert.called(fakeStore.clearDirectLinkedGroupFetchFailed);
......@@ -501,7 +543,11 @@ describe('groups', function() {
it("includes the direct-linked annotation's group when it is not in the normal list of groups", () => {
const svc = service();
fakeStore.setState({ directLinkedAnnotationId: 'ann-id' });
fakeStore.setState({
directLinked: {
directLinkedAnnotationId: 'ann-id',
},
});
fakeApi.profile.groups.read.returns(Promise.resolve([]));
fakeApi.groups.list.returns(
......@@ -534,8 +580,10 @@ describe('groups', function() {
const svc = service();
fakeStore.setState({
directLinkedGroupId: 'out-of-scope',
directLinkedAnnotationId: 'ann-id',
directLinked: {
directLinkedGroupId: 'out-of-scope',
directLinkedAnnotationId: 'ann-id',
},
});
fakeApi.profile.groups.read.returns(Promise.resolve([]));
......@@ -573,8 +621,10 @@ describe('groups', function() {
const svc = service();
fakeStore.setState({
directLinkedGroupId: '__world__',
directLinkedAnnotationId: null,
directLinked: {
directLinkedGroupId: '__world__',
directLinkedAnnotationId: null,
},
});
fakeApi.profile.groups.read.returns(Promise.resolve([]));
......@@ -614,7 +664,7 @@ describe('groups', function() {
})
);
fakeStore.setState({
directLinkedAnnotationId: 'direct-linked-ann',
directLinked: { directLinkedAnnotationId: 'direct-linked-ann' },
});
}
......
......@@ -129,6 +129,7 @@ function clearDirectLinkedIds() {
module.exports = {
init,
namespace: 'directLinked',
update,
actions: {
setDirectLinkedGroupFetchFailed,
......
......@@ -7,6 +7,10 @@ describe('sidebar/store/modules/direct-linked', () => {
let store;
let fakeSettings = {};
const getDirectLinkedState = () => {
return store.getRootState().directLinked;
};
beforeEach(() => {
store = createStore([directLinked], [fakeSettings]);
});
......@@ -15,7 +19,7 @@ describe('sidebar/store/modules/direct-linked', () => {
it('sets directLinkedGroupFetchFailed to true', () => {
store.setDirectLinkedGroupFetchFailed();
assert.isTrue(store.getState().directLinkedGroupFetchFailed);
assert.isTrue(getDirectLinkedState().directLinkedGroupFetchFailed);
});
});
......@@ -25,7 +29,7 @@ describe('sidebar/store/modules/direct-linked', () => {
store.clearDirectLinkedGroupFetchFailed();
assert.isFalse(store.getState().directLinkedGroupFetchFailed);
assert.isFalse(getDirectLinkedState().directLinkedGroupFetchFailed);
});
});
......@@ -34,14 +38,14 @@ describe('sidebar/store/modules/direct-linked', () => {
store = createStore([directLinked], [fakeSettings]);
assert.equal(store.getState().directLinkedAnnotationId, 'ann-id');
assert.equal(getDirectLinkedState().directLinkedAnnotationId, 'ann-id');
});
describe('setDirectLinkedAnnotationId', () => {
it('sets directLinkedAnnotationId to the specified annotation id', () => {
store.setDirectLinkedAnnotationId('ann-id');
assert.equal(store.getState().directLinkedAnnotationId, 'ann-id');
assert.equal(getDirectLinkedState().directLinkedAnnotationId, 'ann-id');
});
});
......@@ -50,14 +54,14 @@ describe('sidebar/store/modules/direct-linked', () => {
store = createStore([directLinked], [fakeSettings]);
assert.equal(store.getState().directLinkedGroupId, 'group-id');
assert.equal(getDirectLinkedState().directLinkedGroupId, 'group-id');
});
describe('setDirectLinkedGroupId', () => {
it('sets directLinkedGroupId to the specified group id', () => {
store.setDirectLinkedGroupId('group-id');
assert.equal(store.getState().directLinkedGroupId, 'group-id');
assert.equal(getDirectLinkedState().directLinkedGroupId, 'group-id');
});
});
......@@ -68,8 +72,8 @@ describe('sidebar/store/modules/direct-linked', () => {
store.clearDirectLinkedIds();
assert.equal(store.getState().directLinkedAnnotationId, null);
assert.equal(store.getState().directLinkedGroupId, null);
assert.equal(getDirectLinkedState().directLinkedAnnotationId, null);
assert.equal(getDirectLinkedState().directLinkedGroupId, null);
});
});
});
......@@ -76,17 +76,19 @@ describe('store', function() {
it('sets `directLinkedGroupFetchFailed` to false', () => {
store.clearSelection();
assert.isFalse(store.getState().directLinkedGroupFetchFailed);
assert.isFalse(
store.getRootState().directLinked.directLinkedGroupFetchFailed
);
});
it('sets `directLinkedAnnotationId` to null', () => {
store.clearSelection();
assert.isNull(store.getState().directLinkedAnnotationId);
assert.isNull(store.getRootState().directLinked.directLinkedAnnotationId);
});
it('sets `directLinkedGroupId` to null', () => {
store.clearSelection();
assert.isNull(store.getState().directLinkedGroupId);
assert.isNull(store.getRootState().directLinked.directLinkedGroupId);
});
it('sets `sortKey` to default annotation sort key if set to Orphans', () => {
......
......@@ -24,6 +24,10 @@ function fakeStore(initialState, methods) {
const store = redux.createStore(update, initialState);
store.getRootState = () => {
return store.getState();
};
store.setState = function(state) {
store.dispatch({ type: 'SET_STATE', state: state });
};
......
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