Skip to content

Commit

Permalink
add tests for save and load filters
Browse files Browse the repository at this point in the history
  • Loading branch information
vish9812 committed Nov 29, 2024
1 parent a7c2511 commit e99c8e5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
109 changes: 105 additions & 4 deletions ui/src/components/filters/useViewModel.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createRoot } from "solid-js";
import useViewModel, { GridsRefs, defaultFilters } from "./useViewModel";
import useViewModel, {
FiltersData,
GridsRefs,
defaultFilters,
savedFilterKeyName,
} from "./useViewModel";
import { FiltersProps } from "./useViewModel";
import comparer from "@al/services/comparer";
import LogData, { Summary } from "@al/models/logData";
Expand Down Expand Up @@ -89,13 +94,14 @@ describe("useViewModel", () => {
});

afterEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
});

test("initial values", () => {
createRoot((dispose) => {
const vm = useViewModel(null as any);

expect(vm.savedFilterName(), "savedFilterName").toEqual("");
expect(vm.addedLogs(), "addedLogs").toEqual(comparer.added);
expect(vm.removedLogs(), "removedLogs").toEqual(comparer.removed);
expect(vm.unchangedLogs(), "unchangedLogs").toEqual(comparer.unchanged);
Expand All @@ -111,6 +117,99 @@ describe("useViewModel", () => {
});
});

describe("manageSavedFilters", () => {
const filterName = "test_filter";

const filterToSave: FiltersData = {
startTime: "2023-10-20T01:00:00.000Z",
endTime: "2023-10-20T11:00:00.000Z",
errorsOnly: true,
logs: [
{
some_key: "some_value",
},
],
regex: "^some.*regex$",
terms: [
{
and: true,
contains: true,
field: "some_field",
value: "ands",
},
],
firstN: 1,
lastN: 1,
};

const expectedSavedFilter = { ...filterToSave, logs: [] };

beforeEach(() => {
localStorage.clear();
});

test("handleSaveFilter", () => {
createRoot((dispose) => {
const vm = useViewModel(props);

vm.setFilters(filterToSave);
vm.setSavedFilterName(filterName);
vm.handleSaveFilter();

expect(vm.savedFilterName(), "savedFilterName").toEqual(filterName);
expect(localStorage.length, "localStorage.length").toEqual(1);
const loadedFilterStr = localStorage.getItem(
savedFilterKeyName(filterName)
);
expect(loadedFilterStr, "loadedFilterStr").toBeTruthy();
const loadedFilterJSON = JSON.parse(loadedFilterStr!);
expect(loadedFilterJSON, "loadedFilterJSON").toEqual(
expectedSavedFilter
);

dispose();
});
});

describe("handleLoadFilter", () => {
test("whenNoFilterIsAvailableToLoad", () => {
createRoot((dispose) => {
const vm = useViewModel(props);

vm.handleLoadFilter(filterName);

expect(vm.savedFilterName(), "savedFilterName").toEqual(filterName);
expect(vm.filters, "filters").toEqual(defaultFilters());
expect(props.onFiltersChange, "onFiltersChange").toBeCalledWith(
vm.filters
);

dispose();
});
});

test("whenAFilterIsAvailableToLoad", () => {
createRoot((dispose) => {
const vm = useViewModel(props);

localStorage.setItem(
savedFilterKeyName(filterName),
JSON.stringify(expectedSavedFilter)
);
vm.handleLoadFilter(filterName);

expect(vm.savedFilterName(), "savedFilterName").toEqual(filterName);
expect(vm.filters, "filters").toEqual(expectedSavedFilter);
expect(props.onFiltersChange, "onFiltersChange").toBeCalledWith(
vm.filters
);

dispose();
});
});
});
});

test("handleFiltersChange", () => {
createRoot((dispose) => {
const vm = useViewModel(props);
Expand Down Expand Up @@ -248,7 +347,7 @@ describe("useViewModel", () => {
unchanged: {
api: {
getSelectedRows: () => [
{ logs: [{ id: 2 }, {id: 11}, { id: 4 }] },
{ logs: [{ id: 2 }, { id: 11 }, { id: 4 }] },
{ logs: [{ id: 5 }] },
],
},
Expand Down Expand Up @@ -302,7 +401,9 @@ describe("useViewModel", () => {
expect(vm.filters.errorsOnly, "errorsOnly").toEqual(checked);
expect(vm.addedLogs(), "addedLogs").toEqual(errAddedTopLogs);
expect(vm.removedLogs(), "removedLogs").toEqual(errRemovedTopLogs);
expect(vm.unchangedLogs(), "unchangedLogs").toEqual(errUnchangedTopLogs);
expect(vm.unchangedLogs(), "unchangedLogs").toEqual(
errUnchangedTopLogs
);
expect(vm.msgs(), "msgs").toEqual(errMsgs);
expect(vm.httpCodes(), "httpCodes").toEqual(errHTTPCodes);
expect(vm.jobs(), "jobs").toEqual(errJobs);
Expand Down
6 changes: 1 addition & 5 deletions ui/src/components/filters/useViewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,6 @@ function deleteAllSavedFilters(): void {
savedFilterKeys().forEach((key) => localStorage.removeItem(key));
}

function deleteSavedFilter(filterName: string) {
localStorage.removeItem(savedFilterKeyName(filterName));
}

function useViewModel(props: FiltersProps) {
const [savedFilterName, setSavedFilterName] = createSignal("");
const [filters, setFilters] = createStore(defaultFilters());
Expand Down Expand Up @@ -235,5 +231,5 @@ function useViewModel(props: FiltersProps) {
}

export default useViewModel;
export { defaultFilters, savedFilterKeys };
export { defaultFilters, savedFilterKeys, savedFilterKeyName };
export type { SearchTerm, FiltersData, FiltersProps, GridsRefs };
3 changes: 0 additions & 3 deletions ui/src/pages/analyze/useViewModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ function useViewModel() {
updateCurrCondition(term);
}

if (!currCondition) {
}

keep = currCondition;
}

Expand Down

0 comments on commit e99c8e5

Please sign in to comment.