Skip to content

Commit

Permalink
test(core/filterModel): Add tests for restoring filters on router tra…
Browse files Browse the repository at this point in the history
…nsitions, remove tests for removed functionality
  • Loading branch information
christopherthielen committed Jul 15, 2019
1 parent f6dbdf5 commit 2dbc696
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 222 deletions.
9 changes: 4 additions & 5 deletions app/scripts/modules/core/src/cluster/cluster.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { IHttpBackendService, mock } from 'angular';
import { find } from 'lodash';

import { REACT_MODULE } from 'core/reactShims';
import * as State from 'core/state';
import { ApplicationModelBuilder } from 'core/application/applicationModel.builder';
import { IInstanceCounts, IServerGroup } from 'core/domain';
Expand All @@ -13,7 +14,7 @@ import { SETTINGS } from 'core/config/settings';
const ClusterState = State.ClusterState;

describe('Service: Cluster', function() {
beforeEach(mock.module(CLUSTER_SERVICE));
beforeEach(mock.module(CLUSTER_SERVICE, REACT_MODULE));

let clusterService: ClusterService;
let $http: IHttpBackendService;
Expand All @@ -28,10 +29,6 @@ describe('Service: Cluster', function() {
};
}

beforeEach(() => {
State.initialize();
});

beforeEach(
mock.inject(($httpBackend: IHttpBackendService, _clusterService_: ClusterService) => {
$http = $httpBackend;
Expand All @@ -53,6 +50,8 @@ describe('Service: Cluster', function() {
}),
);

beforeEach(() => State.initialize());

describe('lazy cluster fetching', () => {
it('switches to lazy cluster fetching if there are more than the on demand threshold for clusters', () => {
const clusters = Array(SETTINGS.onDemandClusterThreshold + 1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { mock } from 'angular';
import * as _ from 'lodash';
import { REACT_MODULE } from 'core/reactShims';
import { CLUSTER_SERVICE } from 'core/cluster/cluster.service';
import { Application } from 'core/application/application.model';
import { ApplicationModelBuilder } from 'core/application/applicationModel.builder';
Expand All @@ -17,7 +18,7 @@ describe('Service: clusterFilterService', function() {
let application: Application;

beforeEach(function() {
mock.module(CLUSTER_SERVICE, require('./mockApplicationData').name, 'ui.router');
mock.module(CLUSTER_SERVICE, require('./mockApplicationData').name, 'ui.router', REACT_MODULE);
mock.inject(function(_applicationJSON_: any, _groupedJSON_: any, _clusterService_: any) {
clusterService = _clusterService_;

Expand Down
73 changes: 42 additions & 31 deletions app/scripts/modules/core/src/filterModel/FilterModelService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { cloneDeep, size, some, isNil, reduce, forOwn, includes, pick } from 'lodash';

import { IFilterModel, IFilterConfig, ISortFilter } from './IFilterModel';
import { IFilterModel, IFilterConfig } from './IFilterModel';
import { ReactInjector } from 'core/reactShims';

export class FilterModelService {
Expand All @@ -10,7 +10,7 @@ export class FilterModelService {
filterModel.groups = [];
filterModel.tags = [];
filterModel.displayOptions = {};
filterModel.sortFilter = {} as ISortFilter;
filterModel.sortFilter = FilterModelService.mapRouterParamsToSortFilter(filterModel, {});

filterModel.addTags = () => {
filterModel.tags = [];
Expand All @@ -27,22 +27,54 @@ export class FilterModelService {
});
};

// TODO: remove usages of this, then remove this
// TODO: Remove all calls to activate
filterModel.activate = () => {};

// Apply any mutations to the current sortFilter values as ui-router state params
filterModel.applyParamsToUrl = () => {
const { sortFilter } = filterModel;
const toParams = filterModel.config.reduce(
(acc, filter) => ({ ...acc, [filter.param]: sortFilter[filter.model] }),
{},
);
const toParams = FilterModelService.mapSortFilterToRouterParams(filterModel);
ReactInjector.$state.go('.', toParams);
};

return filterModel;
}

// Maps the sortFilter data from an IFilterModel object to router params
public static mapSortFilterToRouterParams(filterModel: IFilterModel) {
const { sortFilter, config } = filterModel;
return config.reduce((acc, filter) => ({ ...acc, [filter.param]: sortFilter[filter.model] }), {});
}

// Maps router param values to sortFilter values, applying known default values if the parameter is missing
public static mapRouterParamsToSortFilter(filterModel: IFilterModel, params: any) {
const getValueIfNill = (filterType: string) => {
switch (filterType) {
case 'trueKeyObject':
return {};
case 'string':
return '';
case 'boolean':
return false;
case 'inverse-boolean':
return true;
}
return undefined;
};

const iFilterConfigs = filterModel.config;

return iFilterConfigs.reduce(
(acc, filter) => {
const valueIfNil = getValueIfNill(filter.type);
const rawValue = params[filter.param];
const paramValue = isNil(rawValue) ? valueIfNil : rawValue;
// Clone deep so angularjs mutations happen on a different object reference
return { ...acc, [filter.model]: cloneDeep(paramValue) };
},
{} as any,
);
}

public static registerRouterHooks(filterModel: IFilterModel, stateGlob: string) {
const { transitionService } = ReactInjector.$uiRouter;
const filterParams = filterModel.config.map(cfg => cfg.param);
Expand Down Expand Up @@ -72,32 +104,11 @@ export class FilterModelService {
savedParamsForScreen = {};
});

// Copy the param values to the sortModel for before each transition, applying default values if the param isn't set
// Map transition param values to sortFilter values and save on the filterModel before each transition
// In the future, we should remove the AngularJS code that watches for mutations on the sortFilter object
transitionService.onBefore({ to: stateGlob }, trans => {
const toParams = trans.params();

const getValueIfNill = (filterType: string) => {
switch (filterType) {
case 'trueKeyObject':
return {};
case 'string':
return '';
case 'boolean':
return false;
case 'inverse-boolean':
return true;
}
return undefined;
};

filterModel.config.forEach(filter => {
const valueIfNil = getValueIfNill(filter.type);
const rawValue = toParams[filter.param];
const paramValue = isNil(rawValue) ? valueIfNil : rawValue;
// Clone deep so angularjs mutations happen on a different object reference
filterModel.sortFilter[filter.model] = cloneDeep(paramValue);
});
Object.assign(filterModel.sortFilter, FilterModelService.mapRouterParamsToSortFilter(filterModel, toParams));
});
}

Expand Down
Loading

0 comments on commit 2dbc696

Please sign in to comment.