* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { getChildren } from '../../../api/components';
+import { getBreadcrumbs, getChildren, getComponent } from '../../../api/components';
import { mockMainBranch, mockPullRequest } from '../../../helpers/mocks/branch-like';
-import { addComponent, addComponentChildren, getComponentBreadcrumbs } from '../bucket';
-import { getCodeMetrics, loadMoreChildren, retrieveComponentChildren } from '../utils';
+import {
+ addComponent,
+ addComponentBreadcrumbs,
+ addComponentChildren,
+ getComponentBreadcrumbs
+} from '../bucket';
+import {
+ getCodeMetrics,
+ loadMoreChildren,
+ retrieveComponent,
+ retrieveComponentChildren
+} from '../utils';
jest.mock('../../../api/components', () => ({
- getChildren: jest.fn().mockRejectedValue({})
+ getBreadcrumbs: jest.fn().mockRejectedValue({}),
+ getChildren: jest.fn().mockRejectedValue({}),
+ getComponent: jest.fn().mockRejectedValue({})
}));
jest.mock('../bucket', () => ({
paging: { total: 2, pageIndex: 0 }
});
- await retrieveComponentChildren('key', 'TRK', mockMainBranch());
+ await retrieveComponentChildren('key', 'TRK', { mounted: true }, mockMainBranch());
expect(addComponentChildren).toHaveBeenCalledWith('key', components, 2, 0);
expect(addComponent).toHaveBeenCalledTimes(2);
});
});
+describe('retrieveComponent', () => {
+ it('should update bucket when component is mounted', async () => {
+ const components = [{}, {}];
+ (getChildren as jest.Mock).mockResolvedValueOnce({
+ components,
+ paging: { total: 2, pageIndex: 0 }
+ });
+ (getComponent as jest.Mock).mockResolvedValueOnce({
+ component: {}
+ });
+ (getBreadcrumbs as jest.Mock).mockResolvedValueOnce([]);
+
+ await retrieveComponent('key', 'TRK', { mounted: true }, mockMainBranch());
+
+ expect(addComponentChildren).toHaveBeenCalled();
+ expect(addComponent).toHaveBeenCalledTimes(3);
+ expect(addComponentBreadcrumbs).toHaveBeenCalled();
+ });
+
+ it('should not update bucket when component is not mounted', async () => {
+ const components = [{}, {}];
+ (getChildren as jest.Mock).mockResolvedValueOnce({
+ components,
+ paging: { total: 2, pageIndex: 0 }
+ });
+ (getComponent as jest.Mock).mockResolvedValueOnce({
+ component: {}
+ });
+ (getBreadcrumbs as jest.Mock).mockResolvedValueOnce([]);
+
+ await retrieveComponent('key', 'TRK', { mounted: false }, mockMainBranch());
+
+ expect(addComponentChildren).not.toHaveBeenCalled();
+ expect(addComponent).not.toHaveBeenCalled();
+ expect(addComponentBreadcrumbs).not.toHaveBeenCalled();
+ });
+});
+
describe('loadMoreChildren', () => {
it('should load more children', async () => {
const components = [{}, {}, {}];
paging: { total: 6, pageIndex: 1 }
});
- await loadMoreChildren('key', 1, 'TRK', mockMainBranch());
+ await loadMoreChildren('key', 1, 'TRK', { mounted: true }, mockMainBranch());
expect(addComponentChildren).toHaveBeenCalledWith('key', components, 6, 1);
expect(addComponent).toHaveBeenCalledTimes(3);
loadComponent = (componentKey: string) => {
this.setState({ loading: true });
- retrieveComponent(componentKey, this.props.component.qualifier, this.props.branchLike).then(
- r => {
- if (this.mounted) {
- if (['FIL', 'UTS'].includes(r.component.qualifier)) {
- this.setState({
- breadcrumbs: r.breadcrumbs,
- components: r.components,
- loading: false,
- page: 0,
- searchResults: undefined,
- sourceViewer: r.component,
- total: 0
- });
- } else {
- this.setState({
- baseComponent: r.component,
- breadcrumbs: r.breadcrumbs,
- components: r.components,
- loading: false,
- page: r.page,
- searchResults: undefined,
- sourceViewer: undefined,
- total: r.total
- });
- }
+ retrieveComponent(
+ componentKey,
+ this.props.component.qualifier,
+ this,
+ this.props.branchLike
+ ).then(r => {
+ if (this.mounted) {
+ if (['FIL', 'UTS'].includes(r.component.qualifier)) {
+ this.setState({
+ breadcrumbs: r.breadcrumbs,
+ components: r.components,
+ loading: false,
+ page: 0,
+ searchResults: undefined,
+ sourceViewer: r.component,
+ total: 0
+ });
+ } else {
+ this.setState({
+ baseComponent: r.component,
+ breadcrumbs: r.breadcrumbs,
+ components: r.components,
+ loading: false,
+ page: r.page,
+ searchResults: undefined,
+ sourceViewer: undefined,
+ total: r.total
+ });
}
- },
- this.stopLoading
- );
+ }
+ }, this.stopLoading);
};
stopLoading = () => {
addComponentBreadcrumbs(component.key, component.breadcrumbs);
this.setState({ loading: true });
- retrieveComponentChildren(component.key, component.qualifier, branchLike).then(() => {
+ retrieveComponentChildren(component.key, component.qualifier, this, branchLike).then(() => {
addComponent(component);
if (this.mounted) {
this.handleUpdate();
baseComponent.key,
page + 1,
this.props.component.qualifier,
+ this,
this.props.branchLike
).then(r => {
if (this.mounted && r.components.length) {
rootComponent={rootComponent}
/>
)}
- {baseComponent && (
- <tbody>
- <Component
- branchLike={branchLike}
- canBePinned={canBePinned}
- component={baseComponent}
- key={baseComponent.key}
- metrics={metrics}
- rootComponent={rootComponent}
- />
- <tr className="blank">
- <td colSpan={3}> </td>
- <td colSpan={colSpan}> </td>
- </tr>
- </tbody>
- )}
<tbody>
+ {baseComponent && (
+ <>
+ <Component
+ branchLike={branchLike}
+ canBePinned={canBePinned}
+ component={baseComponent}
+ key={baseComponent.key}
+ metrics={metrics}
+ rootComponent={rootComponent}
+ />
+ <tr className="blank">
+ <td colSpan={3}> </td>
+ <td colSpan={colSpan}> </td>
+ </tr>
+ </>
+ )}
+
{components.length ? (
components.map((component, index, list) => (
<Component
</td>
</tr>
- </tbody>
- <tbody>
<withScrollTo(Component)
canBePinned={true}
canBrowse={true}
</td>
</tr>
- </tbody>
- <tbody>
<withScrollTo(Component)
branchLike={
Object {
return [...METRICS];
}
-function retrieveComponentBase(componentKey: string, qualifier: string, branchLike?: BranchLike) {
+function retrieveComponentBase(
+ componentKey: string,
+ qualifier: string,
+ instance: { mounted: boolean },
+ branchLike?: BranchLike
+) {
const existing = getComponentFromBucket(componentKey);
if (existing) {
return Promise.resolve(existing);
metricKeys: metrics.join(),
...getBranchLikeQuery(branchLike)
}).then(({ component }) => {
- addComponent(component);
+ if (instance.mounted) {
+ addComponent(component);
+ }
return component;
});
}
export function retrieveComponentChildren(
componentKey: string,
qualifier: string,
+ instance: { mounted: boolean },
branchLike?: BranchLike
): Promise<{ components: T.ComponentMeasure[]; page: number; total: number }> {
const existing = getComponentChildren(componentKey);
})
.then(prepareChildren)
.then(r => {
- addComponentChildren(componentKey, r.components, r.total, r.page);
- storeChildrenBase(r.components);
- storeChildrenBreadcrumbs(componentKey, r.components);
+ if (instance.mounted) {
+ addComponentChildren(componentKey, r.components, r.total, r.page);
+ storeChildrenBase(r.components);
+ storeChildrenBreadcrumbs(componentKey, r.components);
+ }
return r;
});
}
function retrieveComponentBreadcrumbs(
component: string,
+ instance: { mounted: boolean },
branchLike?: BranchLike
): Promise<T.Breadcrumb[]> {
const existing = getComponentBreadcrumbs(component);
return getBreadcrumbs({ component, ...getBranchLikeQuery(branchLike) })
.then(skipRootDir)
.then(breadcrumbs => {
- addComponentBreadcrumbs(component, breadcrumbs);
+ if (instance.mounted) {
+ addComponentBreadcrumbs(component, breadcrumbs);
+ }
return breadcrumbs;
});
}
export function retrieveComponent(
componentKey: string,
qualifier: string,
+ instance: { mounted: boolean },
branchLike?: BranchLike
): Promise<{
breadcrumbs: T.Breadcrumb[];
total: number;
}> {
return Promise.all([
- retrieveComponentBase(componentKey, qualifier, branchLike),
- retrieveComponentChildren(componentKey, qualifier, branchLike),
- retrieveComponentBreadcrumbs(componentKey, branchLike)
+ retrieveComponentBase(componentKey, qualifier, instance, branchLike),
+ retrieveComponentChildren(componentKey, qualifier, instance, branchLike),
+ retrieveComponentBreadcrumbs(componentKey, instance, branchLike)
]).then(r => {
return {
breadcrumbs: r[2],
componentKey: string,
page: number,
qualifier: string,
+ instance: { mounted: boolean },
branchLike?: BranchLike
): Promise<Children> {
const metrics = getCodeMetrics(qualifier, branchLike, { includeQGStatus: true });
})
.then(prepareChildren)
.then(r => {
- addComponentChildren(componentKey, r.components, r.total, r.page);
- storeChildrenBase(r.components);
- storeChildrenBreadcrumbs(componentKey, r.components);
+ if (instance.mounted) {
+ addComponentChildren(componentKey, r.components, r.total, r.page);
+ storeChildrenBase(r.components);
+ storeChildrenBreadcrumbs(componentKey, r.components);
+ }
return r;
});
}