Skip to content

Commit

Permalink
ISSUE #5257 - add new filters system tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Amantini1997 committed Nov 28, 2024
1 parent 92be531 commit 4238d0b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const templatesToFilters = (templates: ITemplate[]): TicketFilterDescript
];
};

const toCardFilter = (typesByProperty: FilterTypesByProperty): CardFilter[] => (
export const toCardFilter = (typesByProperty: FilterTypesByProperty): CardFilter[] => (
Object.entries(typesByProperty).flatMap(([property, filterByType]) => (
Object.entries(filterByType).map(([type, filter]: [CardFilterType, BaseFilter]) => ({ property, type, filter }))),
)
Expand Down
103 changes: 71 additions & 32 deletions frontend/test/tickets/card/ticketsCard.store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import { TicketsCardActions } from "@/v5/store/tickets/card/ticketsCard.redux";
import { selectFilteringCompleted, selectFilteringQueries, selectFilteringTemplates, selectIsEditingGroups, selectIsShowingPins, selectReadOnly, selectSelectedTemplateId, selectSelectedTicketId, selectSelectedTicketPinId, selectView } from "@/v5/store/tickets/card/ticketsCard.selectors";
import { selectAvailableTemplatesFilters, selectFilters, selectFiltersByModule, selectIsEditingGroups, selectIsShowingPins, selectReadOnly, selectSelectedTemplateId, selectSelectedTicketId, selectSelectedTicketPinId, selectView } from "@/v5/store/tickets/card/ticketsCard.selectors";
import { TicketsCardViews } from "@/v5/ui/routes/viewer/tickets/tickets.constants";
import { createTestStore } from "../../test.helpers";
import { BaseFilter, CardFilter, TicketCardFilter } from '@components/viewer/cards/cardFilters/cardFilters.types';
import { templatesToFilters, toCardFilter } from '@components/viewer/cards/cardFilters/filtersSelection/tickets/ticketFilters.helpers';


describe('Tickets: store', () => {
let dispatch, getState;
const ticketId = 'ticketId';
const templateId = 'templateId';
const pinId = 'pinId';
const query = 'query';

beforeEach(() => {
({ dispatch, getState } = createTestStore());
Expand Down Expand Up @@ -63,54 +64,92 @@ describe('Tickets: store', () => {
});

describe('filters', () => {
it('should toggle the completed filter', () => {
dispatch(TicketsCardActions.toggleCompleteFilter());
const completedFilterFromState1 = selectFilteringCompleted(getState());

expect(completedFilterFromState1).toEqual(true);

dispatch(TicketsCardActions.toggleCompleteFilter());
const completedFilterFromState2 = selectFilteringCompleted(getState());
const [ticketTitleFilter, ticketIdFilter, templateIdFilter] = templatesToFilters([]);
const baseFilter: BaseFilter = {
operator: 'eq',
values: [],
};
const editedBaseFilter: BaseFilter = {
operator: 'gt',
values: [2],
};
const ticketIdCardFilter: TicketCardFilter = { ...ticketIdFilter, filter: baseFilter };
const ticketTitleCardFilter: TicketCardFilter = { ...ticketTitleFilter, filter: baseFilter };
const updatedTicketTitleCardFilter: TicketCardFilter = { ...ticketTitleCardFilter, filter: editedBaseFilter };

describe('existing filters', () => {
it('should add a filter', () => {
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
const filtersInStore = selectFiltersByModule(getState());
const { module, ...rest } = ticketTitleCardFilter;
const moduleAndticketIdCardFilter: [string, CardFilter[]] = [module, [rest as CardFilter]];
expect(filtersInStore).toEqual([moduleAndticketIdCardFilter]);
});

expect(completedFilterFromState2).toEqual(false);
});
it('should set template filters', () => {
dispatch(TicketsCardActions.setTemplateFilters([templateId]));
const templateFiltersFromState = selectFilteringTemplates(getState());

expect(templateFiltersFromState).toEqual([templateId]);
});
it('should edit a filter', () => {
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
dispatch(TicketsCardActions.upsertFilter(updatedTicketTitleCardFilter));
const filtersInStore = selectFiltersByModule(getState());
const { module, ...rest } = updatedTicketTitleCardFilter;
const moduleAndticketIdCardFilter: [string, CardFilter[]] = [module, [rest as CardFilter]];
expect(filtersInStore).toEqual([moduleAndticketIdCardFilter]);
});

it('should delete a filter', () => {
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
dispatch(TicketsCardActions.deleteFilter(ticketTitleCardFilter));
const filtersInStore = selectFiltersByModule(getState());
expect(filtersInStore).toEqual([]);
});
})

it('should set query filters', () => {
dispatch(TicketsCardActions.setQueryFilters([query]));
const queryFiltersFromState = selectFilteringQueries(getState());

expect(queryFiltersFromState).toEqual([query]);
describe('available template filters', () => {
const getAvailableFilters = () => selectAvailableTemplatesFilters(getState());
it('all the default filters should be available originally', () => {
expect(getAvailableFilters()).toEqual([ticketTitleFilter, ticketIdFilter, templateIdFilter]);
})
it('adding filters should make the unavailable', () => {
// add first filter
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
expect(getAvailableFilters()).toEqual([ticketIdFilter, templateIdFilter]);
// add second filter
dispatch(TicketsCardActions.upsertFilter(ticketIdCardFilter));
expect(getAvailableFilters()).toEqual([templateIdFilter]);
})
it('editing a filter shouldn\'t affect the available filters', () => {
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
dispatch(TicketsCardActions.upsertFilter(ticketIdCardFilter));
const availableFiltersBeforeEditing = getAvailableFilters();
// editing filter
dispatch(TicketsCardActions.upsertFilter(updatedTicketTitleCardFilter));
expect(getAvailableFilters()).toEqual(availableFiltersBeforeEditing);
})
it('removing a filter should make it available', () => {
dispatch(TicketsCardActions.upsertFilter(ticketTitleCardFilter));
dispatch(TicketsCardActions.upsertFilter(ticketIdCardFilter));
// delete filter
dispatch(TicketsCardActions.deleteFilter(ticketTitleCardFilter));
expect(getAvailableFilters()).toEqual([ticketTitleFilter, templateIdFilter]);
})
});
})

it('should reset the state', () => {
dispatch(TicketsCardActions.setSelectedTicket(ticketId));
dispatch(TicketsCardActions.setSelectedTemplate(templateId));
dispatch(TicketsCardActions.setSelectedTicketPin(pinId));
dispatch(TicketsCardActions.toggleCompleteFilter());
dispatch(TicketsCardActions.setTemplateFilters([templateId]));
dispatch(TicketsCardActions.setQueryFilters([query]));
dispatch(TicketsCardActions.resetFilters());
dispatch(TicketsCardActions.resetState());

const selectedTicketIdFromState = selectSelectedTicketId(getState());
const selectedTemplateIdFromState = selectSelectedTemplateId(getState());
const selectedTicketPinIdFromState = selectSelectedTicketPinId(getState());
const completeFilterFromState = selectFilteringCompleted(getState());
const templateFiltersFromState = selectFilteringTemplates(getState());
const queryFiltersFromState = selectFilteringQueries(getState());
const filters = selectFilters(getState());

expect(selectedTicketIdFromState).toEqual(null);
expect(selectedTemplateIdFromState).toEqual(null);
expect(selectedTicketPinIdFromState).toEqual(null);
expect(completeFilterFromState).toEqual(true);
expect(templateFiltersFromState).toEqual([templateId]);
expect(queryFiltersFromState).toEqual([query]);
expect(filters).toEqual({});
});
});
});

0 comments on commit 4238d0b

Please sign in to comment.