Commit 5f478ff4 authored by Hannah Stepanek's avatar Hannah Stepanek

Remove loading based on conditional feature flag

Since the feature flag isn't available until the profile api request
completes, and the profile api request is run asynchronously with the
group api requests, it doesn't make sense to load conditionaly. Instead,
call all the group api requests and filter the groups for the old groups
menu later. In this case, filtering is done in the groups.all
conditionally based on the feature flag.
parent 1cde80cd
...@@ -77,7 +77,9 @@ function groups( ...@@ -77,7 +77,9 @@ function groups(
// If the main document URL has no groups associated with it, always show // If the main document URL has no groups associated with it, always show
// the "Public" group. // the "Public" group.
const pageHasAssociatedGroups = groups.some(g => g.id !== '__world__'); const pageHasAssociatedGroups = groups.some(
g => g.id !== '__world__' && g.isScopedToUri
);
if (!pageHasAssociatedGroups) { if (!pageHasAssociatedGroups) {
return Promise.resolve(groups); return Promise.resolve(groups);
} }
...@@ -144,7 +146,7 @@ function groups( ...@@ -144,7 +146,7 @@ function groups(
return uri return uri
.then(uri => { .then(uri => {
const params = { const params = {
expand: 'organization', expand: ['organization', 'scopes'],
}; };
if (authority) { if (authority) {
params.authority = authority; params.authority = authority;
...@@ -154,27 +156,19 @@ function groups( ...@@ -154,27 +156,19 @@ function groups(
} }
documentUri = uri; documentUri = uri;
if (features.flagEnabled('community_groups')) { const profileGroupsApi = api.profile.groups.read({
params.expand = ['organization', 'scopes']; expand: params.expand,
const profileParams = { });
expand: ['organization', 'scopes'], const listGroupsApi = api.groups.list(params);
};
const profileGroupsApi = api.profile.groups.read(profileParams); return Promise.all([
const listGroupsApi = api.groups.list(params); profileGroupsApi,
return Promise.all([ listGroupsApi,
profileGroupsApi, auth.tokenGetter(),
listGroupsApi, ]).then(([myGroups, featuredGroups, token]) => [
auth.tokenGetter(), combineGroups(myGroups, featuredGroups, documentUri),
]).then(([myGroups, featuredGroups, token]) => [ token,
combineGroups(myGroups, featuredGroups, documentUri), ]);
token,
]);
} else {
// Fetch groups from the API.
return api.groups
.list(params, null, { includeMetadata: true })
.then(({ data, token }) => [data, token]);
}
}) })
.then(([groups, token]) => { .then(([groups, token]) => {
const isLoggedIn = token !== null; const isLoggedIn = token !== null;
...@@ -197,7 +191,10 @@ function groups( ...@@ -197,7 +191,10 @@ function groups(
} }
function all() { function all() {
return store.allGroups(); if (features.flagEnabled('community_groups')) {
return store.allGroups();
}
return store.getInScopeGroups();
} }
// Return the full object for the group with the given id. // Return the full object for the group with the given id.
......
...@@ -70,6 +70,9 @@ describe('groups', function() { ...@@ -70,6 +70,9 @@ describe('groups', function() {
allGroups() { allGroups() {
return this.getState().groups; return this.getState().groups;
}, },
getInScopeGroups() {
return this.getState().groups;
},
focusedGroup() { focusedGroup() {
return this.getState().focusedGroup; return this.getState().focusedGroup;
}, },
...@@ -114,12 +117,7 @@ describe('groups', function() { ...@@ -114,12 +117,7 @@ describe('groups', function() {
}, },
}, },
groups: { groups: {
list: sandbox.stub().returns( list: sandbox.stub().returns(dummyGroups),
Promise.resolve({
data: dummyGroups,
token: '1234',
})
),
}, },
profile: { profile: {
groups: { groups: {
...@@ -151,15 +149,24 @@ describe('groups', function() { ...@@ -151,15 +149,24 @@ describe('groups', function() {
} }
describe('#all', function() { describe('#all', function() {
it('returns all groups', function() { it('returns all groups from store.allGroups when community-groups feature flag is enabled', () => {
const svc = service();
fakeStore.allGroups = sinon.stub().returns(dummyGroups);
fakeFeatures.flagEnabled.withArgs('community_groups').returns(true);
assert.deepEqual(svc.all(), dummyGroups);
assert.called(fakeStore.allGroups);
});
it('returns all groups from store.getInScopeGroups when community-groups feature flag is disabled', () => {
const svc = service(); const svc = service();
fakeStore.setState({ groups: dummyGroups }); fakeStore.getInScopeGroups = sinon.stub().returns(dummyGroups);
assert.deepEqual(svc.all(), dummyGroups); assert.deepEqual(svc.all(), dummyGroups);
assert.called(fakeStore.getInScopeGroups);
}); });
}); });
describe('#load', function() { describe('#load', function() {
it('combines groups from both endpoints if community-groups feature flag is set', function() { it('combines groups from both endpoints', function() {
const svc = service(); const svc = service();
const groups = [ const groups = [
...@@ -169,7 +176,6 @@ describe('groups', function() { ...@@ -169,7 +176,6 @@ describe('groups', function() {
fakeApi.profile.groups.read.returns(Promise.resolve(groups)); fakeApi.profile.groups.read.returns(Promise.resolve(groups));
fakeApi.groups.list.returns(Promise.resolve([groups[0]])); fakeApi.groups.list.returns(Promise.resolve([groups[0]]));
fakeFeatures.flagEnabled.withArgs('community_groups').returns(true);
return svc.load().then(() => { return svc.load().then(() => {
assert.calledWith(fakeStore.loadGroups, groups); assert.calledWith(fakeStore.loadGroups, groups);
...@@ -184,21 +190,11 @@ describe('groups', function() { ...@@ -184,21 +190,11 @@ describe('groups', function() {
}); });
}); });
it('always sends the `expand` parameter', function() { it('sends `expand` parameter', function() {
const svc = service();
return svc.load().then(() => {
const call = fakeApi.groups.list.getCall(0);
assert.isObject(call.args[0]);
assert.equal(call.args[0].expand, 'organization');
});
});
it('sends `expand` parameter when community-groups feature flag is enabled', function() {
const svc = service(); const svc = service();
fakeApi.groups.list.returns( fakeApi.groups.list.returns(
Promise.resolve([{ id: 'groupa', name: 'GroupA' }]) Promise.resolve([{ id: 'groupa', name: 'GroupA' }])
); );
fakeFeatures.flagEnabled.withArgs('community_groups').returns(true);
return svc.load().then(() => { return svc.load().then(() => {
assert.calledWith( assert.calledWith(
...@@ -241,7 +237,7 @@ describe('groups', function() { ...@@ -241,7 +237,7 @@ describe('groups', function() {
return loaded.then(() => { return loaded.then(() => {
assert.calledWith(fakeApi.groups.list, { assert.calledWith(fakeApi.groups.list, {
document_uri: 'https://asite.com', document_uri: 'https://asite.com',
expand: 'organization', expand: ['organization', 'scopes'],
}); });
}); });
}); });
...@@ -257,7 +253,7 @@ describe('groups', function() { ...@@ -257,7 +253,7 @@ describe('groups', function() {
const svc = service(); const svc = service();
return svc.load().then(() => { return svc.load().then(() => {
assert.calledWith(fakeApi.groups.list, { assert.calledWith(fakeApi.groups.list, {
expand: 'organization', expand: ['organization', 'scopes'],
}); });
}); });
}); });
...@@ -277,12 +273,7 @@ describe('groups', function() { ...@@ -277,12 +273,7 @@ describe('groups', function() {
it('injects a defalt organization if group is missing an organization', function() { it('injects a defalt organization if group is missing an organization', function() {
const svc = service(); const svc = service();
const groups = [{ id: '39r39f', name: 'Ding Dong!' }]; const groups = [{ id: '39r39f', name: 'Ding Dong!' }];
fakeApi.groups.list.returns( fakeApi.groups.list.returns(Promise.resolve(groups));
Promise.resolve({
token: '1234',
data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
assert.isObject(groups[0].organization); assert.isObject(groups[0].organization);
assert.hasAllKeys(groups[0].organization, ['id', 'logo']); assert.hasAllKeys(groups[0].organization, ['id', 'logo']);
...@@ -317,12 +308,9 @@ describe('groups', function() { ...@@ -317,12 +308,9 @@ describe('groups', function() {
groups.push({ name: 'BioPub', id: 'biopub' }); groups.push({ name: 'BioPub', id: 'biopub' });
} }
fakeApi.groups.list.returns( fakeAuth.tokenGetter.returns(loggedIn ? '1234' : null);
Promise.resolve({ fakeApi.groups.list.returns(Promise.resolve(groups));
token: loggedIn ? '1234' : null, fakeApi.profile.groups.read.returns(Promise.resolve([]));
data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
const publicGroupShown = groups.some(g => g.id === '__world__'); const publicGroupShown = groups.some(g => g.id === '__world__');
...@@ -375,12 +363,8 @@ describe('groups', function() { ...@@ -375,12 +363,8 @@ describe('groups', function() {
{ name: 'DEF', id: 'def456', groupid: null }, { name: 'DEF', id: 'def456', groupid: null },
]; ];
fakeApi.groups.list.returns( fakeApi.groups.list.returns(Promise.resolve(groups));
Promise.resolve({ fakeApi.profile.groups.read.returns(Promise.resolve([]));
token: '1234',
data: groups,
})
);
return svc.load().then(groups => { return svc.load().then(groups => {
let displayedGroups = groups.map(g => g.id); let displayedGroups = groups.map(g => g.id);
......
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