Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entities loading #753

Merged
merged 17 commits into from
Jun 27, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Don't user filter string directly in store
By using the filter string directly it would have been possible to
overrride the byId and default state properties which MUST NOT be
possible at all.
bjoernricks committed Jun 27, 2018
commit 0482d833e6e299a331b79b68a3be54ccda279cec
29 changes: 16 additions & 13 deletions gsa/src/web/store/entities/utils/__tests__/reducers.js
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ import {is_function} from 'gmp/utils/identity';
import Filter from 'gmp/models/filter';

import {createLoadingTypes, createActionCreators} from '../actions';
import {createReducer} from '../reducers';
import {createReducer, filterIdentifier} from '../reducers';

describe('entities reducers test', () => {

@@ -72,7 +72,7 @@ describe('entities reducers test', () => {

expect(reducer(undefined, action)).toEqual({
byId: {},
'name=foo': {
[filterIdentifier(filter)]: {
isLoading: true,
error: null,
entities: [],
@@ -85,9 +85,10 @@ describe('entities reducers test', () => {
const actions = createActionCreators(types);
const reducer = createReducer(types);
const filter = Filter.fromString('name=foo');
const otherFilter = Filter.fromString('name=bar');
const action = actions.request(filter);
const state = {
'name=bar': {
[filterIdentifier(otherFilter)]: {
isLoading: false,
entities: [],
error: null,
@@ -96,12 +97,12 @@ describe('entities reducers test', () => {

expect(reducer(state, action)).toEqual({
byId: {},
'name=foo': {
[filterIdentifier(filter)]: {
isLoading: true,
error: null,
entities: [],
},
'name=bar': {
[filterIdentifier(otherFilter)]: {
isLoading: false,
entities: [],
error: null,
@@ -116,7 +117,7 @@ describe('entities reducers test', () => {
const filter = Filter.fromString('name=foo');
const action = actions.request(filter);
const state = {
'name=foo': {
[filterIdentifier(filter)]: {
isLoading: false,
entities: ['foo', 'bar'],
error: 'An error',
@@ -125,7 +126,7 @@ describe('entities reducers test', () => {

expect(reducer(state, action)).toEqual({
byId: {},
'name=foo': {
[filterIdentifier(filter)]: {
isLoading: true,
entities: ['foo', 'bar'],
error: 'An error',
@@ -195,9 +196,10 @@ describe('entities reducers test', () => {
const actions = createActionCreators(types);
const reducer = createReducer(types);
const filter = Filter.fromString('name=bar');
const otherFilter = Filter.fromString('name=foo');
const action = actions.success([{id: 'foo'}, {id: 'bar'}], filter);
const state = {
'name=foo': {
[filterIdentifier(otherFilter)]: {
isLoading: true,
entities: ['lorem', 'ipsum'],
error: 'An error',
@@ -213,12 +215,12 @@ describe('entities reducers test', () => {
id: 'bar',
},
},
'name=foo': {
[filterIdentifier(otherFilter)]: {
isLoading: true,
entities: ['lorem', 'ipsum'],
error: 'An error',
},
'name=bar': {
[filterIdentifier(filter)]: {
isLoading: false,
entities: ['foo', 'bar'],
error: null,
@@ -274,6 +276,7 @@ describe('entities reducers test', () => {
const actions = createActionCreators(types);
const reducer = createReducer(types);
const filter = Filter.fromString('name=bar');
const otherFilter = Filter.fromString('name=foo');
const action = actions.error('An error', filter);
const state = {
byId: {
@@ -284,7 +287,7 @@ describe('entities reducers test', () => {
id: 'ipsum',
},
},
'name=foo': {
[filterIdentifier(otherFilter)]: {
isLoading: true,
entities: ['lorem', 'ipsum'],
error: 'Another error',
@@ -300,12 +303,12 @@ describe('entities reducers test', () => {
id: 'ipsum',
},
},
'name=foo': {
[filterIdentifier(otherFilter)]: {
isLoading: true,
entities: ['lorem', 'ipsum'],
error: 'Another error',
},
'name=bar': {
[filterIdentifier(filter)]: {
isLoading: false,
entities: [],
error: 'An error',
13 changes: 7 additions & 6 deletions gsa/src/web/store/entities/utils/__tests__/selectors.js
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ import Filter from 'gmp/models/filter';
import {createRootState, createState} from '../testing';

import {createSelector} from '../selectors';
import {filterIdentifier} from '../reducers';

describe('EntitiesSelector getIsLoading tests', () => {

@@ -78,17 +79,17 @@ describe('EntitiesSelector getIsLoading tests', () => {
});

test('isLoading should be true with filter', () => {
const filter = Filter.fromString('name=foo');
const selector = createSelector('foo');
const rootState = createState('foo', {
default: {
isLoading: false,
},
'name=foo': {
[filterIdentifier(filter)]: {
isLoading: true,
},
});
const fooSelector = selector(rootState);
const filter = Filter.fromString('name=foo');

expect(fooSelector.getIsLoading(filter)).toEqual(true);
});
@@ -200,6 +201,7 @@ describe('EntitiesSelector getEntities tests', () => {
});

test('getEntities should return entities with filter', () => {
const filter = Filter.fromString('name=foo');
const selector = createSelector('foo');
const rootState = createState('foo', {
byId: {
@@ -219,12 +221,11 @@ describe('EntitiesSelector getEntities tests', () => {
default: {
entities: ['foo', 'bar'],
},
'name=foo': {
[filterIdentifier(filter)]: {
entities: ['lorem', 'ipsum'],
},
});
const fooSelector = selector(rootState);
const filter = Filter.fromString('name=foo');

expect(fooSelector.getEntities(filter))
.toEqual([{id: 'lorem'}, {id: 'ipsum'}]);
@@ -313,17 +314,17 @@ describe('EntitiesSelector getError tests', () => {
});

test('getError should return error with filter', () => {
const filter = Filter.fromString('name=foo');
const selector = createSelector('foo');
const rootState = createState('foo', {
default: {
error: 'An error',
},
'name=foo': {
[filterIdentifier(filter)]: {
error: 'Another error',
},
});
const fooSelector = selector(rootState);
const filter = Filter.fromString('name=foo');

expect(fooSelector.getError(filter)).toEqual('Another error');
});
7 changes: 5 additions & 2 deletions gsa/src/web/store/entities/utils/reducers.js
Original file line number Diff line number Diff line change
@@ -24,6 +24,10 @@ import {combineReducers} from 'redux';

import {is_defined} from 'gmp/utils/identity';

export const filterIdentifier = filter => is_defined(filter) ?
`filter:${filter.toFilterString()}` :
'default';

export const createReducer = types => {

const isLoading = (state = false, action) => {
@@ -84,8 +88,7 @@ export const createReducer = types => {
case types.REQUEST:
case types.SUCCESS:
case types.ERROR:
const filterString = is_defined(action.filter) ?
action.filter.toFilterString() : 'default';
const filterString = filterIdentifier(action.filter);
return {
...state,
byId: byId(state.byId, action),
6 changes: 3 additions & 3 deletions gsa/src/web/store/entities/utils/selectors.js
Original file line number Diff line number Diff line change
@@ -22,16 +22,16 @@
*/
import {is_defined} from 'gmp/utils/identity';

import {filterIdentifier} from './reducers';

class EntitiesSelector {

constructor(state = {}) {
this.state = state;
}

_getByFilter(filter) {
const filterString = is_defined(filter) ? filter.toFilterString() :
'default';
return this.state[filterString];
return this.state[filterIdentifier(filter)];
}

getIsLoading(filter) {