Commit 207d49da authored by samdbeckham's avatar samdbeckham

Adds changes from @leipert review

- Better documentation and testing for activeFilters getter
- Aligns the filters to the grid
- Renames gl-filter to dasboard-filter
- Fixes a bug with pagination and filter mixing
parent 60cd9c28
<script> <script>
import { mapState } from 'vuex'; import { mapState } from 'vuex';
import GlFilter from './filter.vue'; import DashboardFilter from './filter.vue';
export default { export default {
components: { components: {
GlFilter, DashboardFilter,
}, },
props: { props: {
dashboardDocumentation: { dashboardDocumentation: {
...@@ -21,17 +21,14 @@ export default { ...@@ -21,17 +21,14 @@ export default {
<template> <template>
<div class="dashboard-filters border-bottom bg-light"> <div class="dashboard-filters border-bottom bg-light">
<div class="row mx-0 p-2"> <div class="row mx-0 p-2">
<gl-filter <dashboard-filter
v-for="filter in filters" v-for="filter in filters"
:key="filter.id" :key="filter.id"
class="col-sm p-2 js-filter" class="col-sm-6 col-md-4 col-lg-2 p-2 js-filter"
:filter-id="filter.id" :filter-id="filter.id"
:dashboard-documentation="dashboardDocumentation" :dashboard-documentation="dashboardDocumentation"
@change="$emit('change');" @change="$emit('change');"
/> />
<div class="col-sm p-2 d-md-block d-none"></div>
<div class="col-sm p-2 d-md-block d-none"></div>
<div class="col-sm p-2 d-md-block d-none"></div>
</div> </div>
</div> </div>
</template> </template>
...@@ -29,6 +29,7 @@ export default { ...@@ -29,6 +29,7 @@ export default {
'pageInfo', 'pageInfo',
'vulnerabilities', 'vulnerabilities',
]), ]),
...mapGetters('filters', ['activeFilters']),
...mapGetters('vulnerabilities', ['dashboardListError']), ...mapGetters('vulnerabilities', ['dashboardListError']),
showEmptyState() { showEmptyState() {
return ( return (
...@@ -48,7 +49,7 @@ export default { ...@@ -48,7 +49,7 @@ export default {
methods: { methods: {
...mapActions('vulnerabilities', ['fetchVulnerabilities', 'openModal']), ...mapActions('vulnerabilities', ['fetchVulnerabilities', 'openModal']),
fetchPage(page) { fetchPage(page) {
this.fetchVulnerabilities({ page }); this.fetchVulnerabilities({ ...this.activeFilters, page });
}, },
}, },
}; };
......
export const getFilter = state => filterId => state.filters.find(filter => filter.id === filterId); export const getFilter = state => filterId => state.filters.find(filter => filter.id === filterId);
export const getSelectedOptions = (state, getters) => filterId => export const getSelectedOptions = (state, getters) => filterId =>
getters.getFilter(filterId).options.filter(option => option.selected); getters.getFilter(filterId).options.filter(option => option.selected);
/**
* Loops through all the filters and returns all the selected/active ones
* stripping out any that are set to 'all'
* @returns Object
* e.g. { type: ['sast'], severity: ['high', 'medium'] }
*/
export const activeFilters = (state, getters) => export const activeFilters = (state, getters) =>
state.filters state.filters
.map(filter => filter.id) .map(filter => filter.id)
.reduce( .reduce(
(obj, filterId) => ({ (result, filterId) => ({
...obj, ...result,
[filterId]: getters [filterId]: getters
.getSelectedOptions(filterId) .getSelectedOptions(filterId)
.map(option => option.id) .map(option => option.id)
......
...@@ -2,10 +2,16 @@ import createState from 'ee/security_dashboard/store/modules/filters/state'; ...@@ -2,10 +2,16 @@ import createState from 'ee/security_dashboard/store/modules/filters/state';
import * as getters from 'ee/security_dashboard/store/modules/filters/getters'; import * as getters from 'ee/security_dashboard/store/modules/filters/getters';
describe('filters module getters', () => { describe('filters module getters', () => {
const mockedGetters = {}; const mockedGetters = state => {
mockedGetters.getFilter = filterId => getters.getFilter(createState())(filterId); const getFilter = filterId => getters.getFilter(state)(filterId);
mockedGetters.getSelectedOptions = filterId => const getSelectedOptions = filterId =>
getters.getSelectedOptions(createState(), mockedGetters)(filterId); getters.getSelectedOptions(state, { getFilter })(filterId);
return {
getFilter,
getSelectedOptions,
};
};
describe('getFilter', () => { describe('getFilter', () => {
it('should return the type filter information', () => { it('should return the type filter information', () => {
...@@ -19,7 +25,7 @@ describe('filters module getters', () => { ...@@ -19,7 +25,7 @@ describe('filters module getters', () => {
describe('getSelectedOptions', () => { describe('getSelectedOptions', () => {
it('should return "SAST" as the selcted option', () => { it('should return "SAST" as the selcted option', () => {
const state = createState(); const state = createState();
const selectedOptions = getters.getSelectedOptions(state, mockedGetters)('type'); const selectedOptions = getters.getSelectedOptions(state, mockedGetters(state))('type');
expect(selectedOptions).toHaveLength(1); expect(selectedOptions).toHaveLength(1);
expect(selectedOptions[0].name).toEqual('SAST'); expect(selectedOptions[0].name).toEqual('SAST');
...@@ -29,17 +35,29 @@ describe('filters module getters', () => { ...@@ -29,17 +35,29 @@ describe('filters module getters', () => {
describe('activeFilters', () => { describe('activeFilters', () => {
it('should return no severity filters', () => { it('should return no severity filters', () => {
const state = createState(); const state = createState();
const activeFilters = getters.activeFilters(state, mockedGetters); const activeFilters = getters.activeFilters(state, mockedGetters(state));
expect(activeFilters.severity).toHaveLength(0); expect(activeFilters.severity).toHaveLength(0);
}); });
it('should return the SAST type filter', () => { it('should return the SAST type filter', () => {
const state = createState(); const state = createState();
const activeFilters = getters.activeFilters(state, mockedGetters); const activeFilters = getters.activeFilters(state, mockedGetters(state));
expect(activeFilters.type).toHaveLength(1); expect(activeFilters.type).toHaveLength(1);
expect(activeFilters.type[0]).toEqual('sast'); expect(activeFilters.type[0]).toEqual('sast');
}); });
it('should return multiple project filters"', () => {
const state = createState();
const projectFilter = {
id: 'project',
options: [{ id: 'one', selected: true }, { id: 'anotherone', selected: true }],
};
state.filters.push(projectFilter);
const activeFilters = getters.activeFilters(state, mockedGetters(state));
expect(activeFilters.project).toHaveLength(2);
});
}); });
}); });
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