Commit c8299a3d authored by Robert Knight's avatar Robert Knight

Convert '<group-list-section>' to React

parent 30d967f5
'use strict'; 'use strict';
// @ngInject const { Fragment, createElement } = require('preact');
function GroupListSectionController() { const propTypes = require('prop-types');
this.isSelectable = function(groupId) {
const group = this.sectionGroups.find(g => g.id === groupId); const GroupListItem = require('./group-list-item');
const GroupListItemOutOfScope = require('./group-list-item-out-of-scope');
/**
* A labeled section of the groups list.
*/
function GroupListSection({ analytics, heading, sectionGroups, store }) {
const isSelectable = groupId => {
const group = sectionGroups.find(g => g.id === groupId);
return !group.scopes.enforced || group.isScopedToUri; return !group.scopes.enforced || group.isScopedToUri;
}; };
return (
<Fragment>
<h2 className="group-list-section__heading">{heading}</h2>
<ul className="group-list-section__content">
{sectionGroups.map(group => (
<li
className="dropdown-menu__row dropdown-menu__row--no-border dropdown-menu__row--unpadded"
key={group.id}
>
{isSelectable(group.id) ? (
<GroupListItem
className="group-list-item"
group={group}
analytics={analytics}
store={store}
/>
) : (
<GroupListItemOutOfScope
className="group-list-item-out-of-scope"
group={group}
analytics={analytics}
/>
)}
</li>
))}
</ul>
</Fragment>
);
} }
module.exports = { GroupListSection.propTypes = {
controller: GroupListSectionController, /* The list of groups to be displayed in the group list section. */
controllerAs: 'vm', sectionGroups: propTypes.arrayOf(propTypes.object),
bindings: { /* The string name of the group list section. */
/* The list of groups to be displayed in the group list section. */ heading: propTypes.string,
sectionGroups: '<',
/* The string name of the group list section. */ // TODO - These are only used by child components. It shouldn't be necessary
heading: '<', // to pass them down manually.
}, analytics: propTypes.object.isRequired,
template: require('../templates/group-list-section.html'), store: propTypes.object.isRequired,
}; };
GroupListSection.injectedProps = ['analytics', 'store'];
module.exports = GroupListSection;
'use strict'; 'use strict';
const angular = require('angular'); const { shallow } = require('enzyme');
const groupListSection = require('../group-list-section'); const { createElement } = require('preact');
const util = require('../../directive/test/util');
describe('groupListSection', () => { const GroupListSection = require('../group-list-section');
before(() => { const GroupListItem = require('../group-list-item');
angular.module('app', []).component('groupListSection', groupListSection); const GroupListItemOutOfScope = require('../group-list-item-out-of-scope');
});
beforeEach(() => {
angular.mock.module('app', {});
});
describe('GroupListSection', () => {
const createGroupListSection = fakeSectionGroups => { const createGroupListSection = fakeSectionGroups => {
return util.createDirective(document, 'groupListSection', { const props = {
sectionGroups: fakeSectionGroups, sectionGroups: fakeSectionGroups,
}); analytics: {},
store: {},
};
return shallow(<GroupListSection {...props} />);
}; };
describe('isSelectable', () => { describe('group item types', () => {
[ [
{ {
description: description:
'returns false if group is out of scope and scope is enforced', 'renders GroupListItem if group is out of scope but scope is not enforced',
scopesEnforced: true, scopesEnforced: false,
expectedIsSelectable: [true, false], expectedIsSelectable: [true, true],
}, },
{ {
description: description:
'returns true if group is out of scope but scope is not enforced', 'renders GroupListItemOutOfScope if group is out of scope and scope is enforced',
scopesEnforced: false, scopesEnforced: true,
expectedIsSelectable: [true, true], expectedIsSelectable: [true, false],
}, },
].forEach(({ description, scopesEnforced, expectedIsSelectable }) => { ].forEach(({ description, scopesEnforced, expectedIsSelectable }) => {
it(description, () => { it(description, () => {
...@@ -48,14 +46,20 @@ describe('groupListSection', () => { ...@@ -48,14 +46,20 @@ describe('groupListSection', () => {
}, },
]; ];
const element = createGroupListSection(fakeSectionGroups); const wrapper = createGroupListSection(fakeSectionGroups);
fakeSectionGroups.forEach(g => const itemTypes = wrapper
assert.equal( .findWhere(
element.ctrl.isSelectable(g.id), n =>
expectedIsSelectable[g.id] n.type() === GroupListItem || n.type() === GroupListItemOutOfScope
) )
.map(item => item.type().name);
const expectedItemTypes = fakeSectionGroups.map(g =>
expectedIsSelectable[g.id]
? 'GroupListItem'
: 'GroupListItemOutOfScope'
); );
assert.deepEqual(itemTypes, expectedItemTypes);
}); });
}); });
}); });
......
...@@ -164,7 +164,10 @@ function startAngularApp(config) { ...@@ -164,7 +164,10 @@ function startAngularApp(config) {
'groupListItemOutOfScope', 'groupListItemOutOfScope',
wrapReactComponent(require('./components/group-list-item-out-of-scope')) wrapReactComponent(require('./components/group-list-item-out-of-scope'))
) )
.component('groupListSection', require('./components/group-list-section')) .component(
'groupListSection',
wrapReactComponent(require('./components/group-list-section'))
)
.component('helpLink', require('./components/help-link')) .component('helpLink', require('./components/help-link'))
.component('helpPanel', require('./components/help-panel')) .component('helpPanel', require('./components/help-panel'))
.component('loggedoutMessage', require('./components/loggedout-message')) .component('loggedoutMessage', require('./components/loggedout-message'))
......
<h2 class="group-list-section__heading">
{{ vm.heading }}
</h2>
<ul class="group-list-section__content">
<li
class="dropdown-menu__row dropdown-menu__row--no-border dropdown-menu__row--unpadded"
ng-repeat="group in vm.sectionGroups track by group.id"
>
<group-list-item
class="group-list-item"
group="group"
ng-if="vm.isSelectable(group.id)"
>
</group-list-item>
<group-list-item-out-of-scope
class="group-list-item-out-of-scope"
group="group"
ng-if="!vm.isSelectable(group.id)"
>
</group-list-item-out-of-scope>
</li>
</ul>
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