Skip to content

Commit

Permalink
chore(table-state): add missing comment, fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
josip janus committed Jan 18, 2023
1 parent 6e50c40 commit 2f0170f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ describe('Table sate', () => {
};

await changeFilters(router, filters);
expect(callbacks.next.mock.calls[callbacks.next.mock.calls.length - 1][0]).not.toBe(null);
expect(callbacks.next.mock.calls[callbacks.next.mock.calls.length - 1][0]).toBe(null);
});

fit('should not set custom filter observable value to null if object filter is truthy', async () => {
it('should not set custom filter observable value to null if object filter is truthy', async () => {
const callbacks = mockSubscribeCallbacks();

createCustomFiltersObservable(route, router).subscribe(callbacks);
Expand Down Expand Up @@ -244,6 +244,19 @@ describe('Table sate', () => {

expect(navigationSpy).toHaveBeenCalledTimes(1);
});

it('should set scoped custom filter observable value to null if filter is empty', async () => {
const scopedFilters = 'scoped-filters';
const callbacks = mockSubscribeCallbacks();

createCustomFiltersObservable(route, router, scopedFilters).subscribe(callbacks);
expect(callbacks.next).toHaveBeenLastCalledWith(null);

const filters = {};

await changeFilters(router, filters, scopedFilters);
expect(callbacks.next).toHaveBeenLastCalledWith(null);
});
});

function mockSubscribeCallbacks() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export function createCustomFiltersObservable<TFilterValue extends Record<string
}

if (filters && isFilterEmpty(filters)) {
clearFilters(router);
clearFilters(router, queryParamsScope);
return null;
}

Expand Down Expand Up @@ -147,6 +147,7 @@ export function changeSort(router: Router, sort: ISortInfo): Promise<boolean> {
* Creates query params and encodes them based on passed filter value.
* @param {Router} router - Router instance used in component.
* @param {TFilterValue} filterValue - Object containing properties used for filtering
* @param {string} queryParamsScope - String value representing your scoped queryParam
*/
export function changeFilters<TFilterValue extends Record<string, unknown> | null = null>(
router: Router,
Expand Down Expand Up @@ -190,12 +191,19 @@ function isFilterEmpty<TFilterValue extends Record<string, unknown> | null = nul
return !filterHasSomeValue;
}

function clearFilters(router: Router): void {
/**
*
* Creates query params and encodes them based on passed filter value.
* @param {Router} router - Router instance used in component.
* @param {string} queryParamsScope - String value representing your scoped queryParam
*/

function clearFilters(router: Router, queryParamsScope?: string): void {
router.navigate([], {
replaceUrl: true,
queryParamsHandling: 'merge',
queryParams: {
[TableQueryParam.CUSTOM_FILTERS]: null,
[queryParamsScope || TableQueryParam.CUSTOM_FILTERS]: null,
},
});
}

0 comments on commit 2f0170f

Please sign in to comment.