Commit f178f8fe authored by Hannah Stepanek's avatar Hannah Stepanek

Add getCurrentlyViewing/My/FeaturedGroups to store

parent c0219680
......@@ -86,6 +86,7 @@ function GroupListController(
this.showGroupsMenu = () => {
if (features.flagEnabled('community_groups')) {
// Only show the drop down menu if there is more than one group.
return this.groups.all().length > 1;
} else {
return !(this.isThirdPartyService && this.groups.all().length <= 1);
......
'use strict';
const util = require('../util');
const { selectors: sessionSelectors } = require('./session');
const { isLoggedIn } = sessionSelectors;
function init() {
return {
......@@ -118,6 +120,43 @@ function getGroup(state, id) {
return state.groups.find(g => g.id === id);
}
/**
* Return groups that don't show up in Featured and My Groups.
*
* @return {Group[]}
*/
function getCurrentlyViewingGroups(state) {
const myGroups = getMyGroups(state);
const featuredGroups = getFeaturedGroups(state);
return state.groups.filter(
g => !myGroups.includes(g) && !featuredGroups.includes(g)
);
}
/**
* Return groups the logged in user is a member of.
*
* @return {Group[]}
*/
function getMyGroups(state) {
// If logged out, the Public group still has isMember set to true so only
// return groups with membership in logged in state.
if (isLoggedIn(state)) {
return state.groups.filter(g => g.isMember);
}
return [];
}
/**
* Return groups the user isn't a member of that are scoped to the URI.
*
* @return {Group[]}
*/
function getFeaturedGroups(state) {
return state.groups.filter(group => !group.isMember && group.isScopedToUri);
}
module.exports = {
init,
update,
......@@ -128,6 +167,9 @@ module.exports = {
selectors: {
allGroups,
getGroup,
getCurrentlyViewingGroups,
getFeaturedGroups,
getMyGroups,
focusedGroup,
focusedGroupId,
},
......
'use strict';
const immutable = require('seamless-immutable');
const createStore = require('../../create-store');
const groups = require('../groups');
describe('sidebar.store.modules.groups', () => {
const publicGroup = {
const publicGroup = immutable({
id: '__world__',
name: 'Public',
};
isMember: true,
isScopedToUri: true,
});
const privateGroup = {
id: 'foo',
const privateGroup = immutable({
id: 'privateid',
name: 'Private',
isMember: true,
isScopedToUri: true,
});
const restrictedGroup = immutable({
id: 'restrictid',
name: 'Restricted',
isMember: false,
isScopedToUri: true,
});
const restrictedOutOfScopeGroup = immutable({
id: 'rstrctdOOSid',
name: 'Restricted OOS',
isMember: false,
isScopedToUri: false,
});
const restrictedOutOfScopeMemberGroup = immutable({
id: 'rstrctdOOSmemberid',
name: 'Restricted OOS Mem',
isMember: true,
isScopedToUri: false,
});
const openGroup = immutable({
id: 'openid',
isMember: false,
isScopedToUri: true,
});
/*
* Returns groups from the specified group list in the store and asserts
* that none of the lists contain the same groups. The group lists are:
* myGroups, featuredGroups, or currentlyViewingGroups.
*/
const getListAssertNoDupes = (store, list) => {
const allLists = {
myGroups: store.getMyGroups(),
featuredGroups: store.getFeaturedGroups(),
currentlyViewingGroups: store.getCurrentlyViewingGroups(),
};
let allGroups = [];
for (let groups of Object.values(allLists)) {
allGroups = allGroups.concat(groups);
}
const hasDuplicates = new Set(allGroups).size !== allGroups.length;
assert.isFalse(hasDuplicates);
return allLists[list];
};
let store;
......@@ -108,4 +164,131 @@ describe('sidebar.store.modules.groups', () => {
assert.equal(store.focusedGroupId(), privateGroup.id);
});
});
describe('getFeaturedGroups', () => {
[
{
description:
'If logged in and there is a restricted group the user is not a member of, show it in `Featured Groups`.',
isLoggedIn: true,
allGroups: [restrictedGroup],
expectedFeaturedGroups: [restrictedGroup],
},
{
description:
'If logged in and there is an open group, show it in `Featured Groups`.',
isLoggedIn: true,
allGroups: [openGroup],
expectedFeaturedGroups: [openGroup],
},
{
description:
'If logged in and the user is a member of all the groups, do not show them in `Featured Groups`.',
isLoggedIn: true,
allGroups: [publicGroup],
expectedFeaturedGroups: [],
},
{
description:
'If logged out and there is an in-scope restricted group, show it in `Featured Groups`.',
isLoggedIn: false,
allGroups: [restrictedGroup],
expectedFeaturedGroups: [restrictedGroup],
},
{
description:
'If logged out and there is an open group, show it in `Featured Groups`.',
isLoggedIn: false,
allGroups: [openGroup],
expectedFeaturedGroups: [openGroup],
},
].forEach(
({ description, isLoggedIn, allGroups, expectedFeaturedGroups }) => {
it(description, () => {
store.getState().session = { userid: isLoggedIn ? '1234' : null };
store.loadGroups(allGroups);
const featuredGroups = getListAssertNoDupes(store, 'featuredGroups');
assert.deepEqual(featuredGroups, expectedFeaturedGroups);
});
}
);
});
describe('getMyGroups', () => {
[
{
description: 'If not logged in, do not show groups in `My Groups`',
isLoggedIn: false,
allGroups: [publicGroup],
expectedMyGroups: [],
},
{
description:
'If logged in and the user is a member of the group, show it in `My Groups`',
isLoggedIn: true,
allGroups: [openGroup],
expectedMyGroups: [],
},
{
description:
'If logged in and the user is a member of the unscoped group, show it in `My Groups`',
isLoggedIn: true,
allGroups: [restrictedOutOfScopeMemberGroup],
expectedMyGroups: [restrictedOutOfScopeMemberGroup],
},
{
description:
'If logged in and the user is a member of the group, show it in `My Groups`',
isLoggedIn: true,
allGroups: [publicGroup],
expectedMyGroups: [publicGroup],
},
].forEach(({ description, isLoggedIn, allGroups, expectedMyGroups }) => {
it(description, () => {
store.getState().session = { userid: isLoggedIn ? '1234' : null };
store.loadGroups(allGroups);
const myGroups = getListAssertNoDupes(store, 'myGroups');
assert.deepEqual(myGroups, expectedMyGroups);
});
});
});
describe('getCurrentlyViewingGroups', () => {
[
{
description:
'If logged out and there is an out-of-scope restricted group, show it in `Currently Viewing`',
isLoggedIn: false,
allGroups: [restrictedOutOfScopeGroup],
},
{
description:
'If logged out and only the Public group is present, show it in `Currently Viewing`',
isLoggedIn: false,
allGroups: [publicGroup],
},
{
description:
'If logged in and there is an out-of-scope restricted group that the user is not a memberof, show it in `Currently Viewing`',
isLoggedIn: true,
allGroups: [restrictedOutOfScopeGroup],
},
].forEach(({ description, isLoggedIn, allGroups }) => {
it(description, () => {
store.getState().session = { userid: isLoggedIn ? '1234' : null };
store.loadGroups(allGroups);
const currentlyViewing = getListAssertNoDupes(
store,
'currentlyViewingGroups'
);
assert.deepEqual(currentlyViewing, allGroups);
});
});
});
});
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