* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as React from 'react';
+import { connect } from 'react-redux';
import ComponentContainerNotFound from './ComponentContainerNotFound';
import ComponentNav from './nav/component/ComponentNav';
import { Branch, Component } from '../types';
import { getBranches } from '../../api/branches';
import { getComponentData } from '../../api/components';
import { getComponentNavigation } from '../../api/nav';
+import { fetchOrganizations } from '../../store/rootActions';
+import { areThereCustomOrganizations } from '../../store/rootReducer';
interface Props {
children: any;
+ fetchOrganizations: (organizations: string[]) => void;
location: {
query: { branch?: string; id: string };
};
+ organizationsEnabled?: boolean;
}
interface State {
component: Component | null;
}
-export default class ComponentContainer extends React.PureComponent<Props, State> {
+export class ComponentContainer extends React.PureComponent<Props, State> {
mounted: boolean;
constructor(props: Props) {
Promise.all([getComponentNavigation(id), getComponentData(id, branch)]).then(([nav, data]) => {
const component = this.addQualifier({ ...nav, ...data });
+
+ if (this.props.organizationsEnabled) {
+ this.props.fetchOrganizations([component.organization]);
+ }
+
this.fetchBranches(component).then(branches => {
if (this.mounted) {
this.setState({ loading: false, branches, component });
);
}
}
+
+const mapStateToProps = (state: any) => ({
+ organizationsEnabled: areThereCustomOrganizations(state)
+});
+
+const mapDispatchToProps = { fetchOrganizations };
+
+export default connect<any, any, any>(mapStateToProps, mapDispatchToProps)(ComponentContainer);
import * as React from 'react';
import { shallow, mount } from 'enzyme';
-import ComponentContainer from '../ComponentContainer';
+import { ComponentContainer } from '../ComponentContainer';
import { getBranches } from '../../../api/branches';
import { getComponentData } from '../../../api/components';
import { getComponentNavigation } from '../../../api/nav';
it('changes component', () => {
const wrapper = shallow(
- <ComponentContainer location={{ query: { id: 'foo' } }}>
+ <ComponentContainer fetchOrganizations={jest.fn()} location={{ query: { id: 'foo' } }}>
<Inner />
</ComponentContainer>
);
);
mount(
- <ComponentContainer location={{ query: { id: 'moduleKey' } }}>
+ <ComponentContainer fetchOrganizations={jest.fn()} location={{ query: { id: 'moduleKey' } }}>
<Inner />
</ComponentContainer>
);
);
const wrapper = mount(
- <ComponentContainer location={{ query: { id: 'portfolioKey' } }}>
+ <ComponentContainer fetchOrganizations={jest.fn()} location={{ query: { id: 'portfolioKey' } }}>
<Inner />
</ComponentContainer>
);
it('updates branches on change', () => {
(getBranches as jest.Mock<any>).mockImplementation(() => Promise.resolve([]));
const wrapper = shallow(
- <ComponentContainer location={{ query: { id: 'portfolioKey' } }}>
+ <ComponentContainer fetchOrganizations={jest.fn()} location={{ query: { id: 'portfolioKey' } }}>
<Inner />
</ComponentContainer>
);
(wrapper.find(Inner).prop('onBranchesChange') as Function)();
expect(getBranches).toBeCalledWith('projectKey');
});
+
+it('loads organization', () => {
+ (getComponentData as jest.Mock<any>).mockImplementation(() =>
+ Promise.resolve({ organization: 'org' })
+ );
+
+ const fetchOrganizations = jest.fn();
+ mount(
+ <ComponentContainer
+ fetchOrganizations={fetchOrganizations}
+ location={{ query: { id: 'foo' } }}
+ organizationsEnabled={true}>
+ <Inner />
+ </ComponentContainer>
+ );
+
+ return doAsync().then(() => {
+ expect(fetchOrganizations).toBeCalledWith(['org']);
+ });
+});