Skip to content

Commit

Permalink
typecheck, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alixlahuec committed Oct 16, 2024
1 parent 33d8996 commit 38e5614
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 30 deletions.
50 changes: 29 additions & 21 deletions src/components/Dashboard/Explorer/QueryItems/filters.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mock } from "vitest-mock-extended";

import { TypemapProvider } from "Components/UserSettings";

import { QueryFilter } from "@services/search";
import { filterHasEvaluate, QueryFilter } from "@services/search";

import { useItemFilters } from "./filters";
import { setupInitialSettings } from "../../../../setup";
Expand All @@ -18,6 +18,14 @@ const wrapper: WrapperComponent<{ children: ReactChildren }> = ({ children }) =>
return <TypemapProvider init={typemap} updater={vi.fn()}>{children}</TypemapProvider>;
};

const assertFilterResult = (filter: QueryFilter, { query, item, expected }: { query: string, item: any, expected: boolean }) => {
if (filterHasEvaluate(filter)) {
expect(filter.evaluate(query, item)).toEqual(expected);
} else {
expect(Boolean(filter.filter(query, [item]))).toEqual(expected);

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 0 - 'today', '2023-08-02T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 2 - 'this week', '2023-07-30T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 4 - 'this year', '2023-01-02T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 5 - 'this year', '2023-01-01T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 7 - 'last 2 weeks', '2023-07-19T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 9 - 'Monday', '2023-07-31T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > addedBefore > 11 - 'bad input', '2020-03-01T00:00'

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:144:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > type > 0

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:159:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > tags > 0

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:227:5

Check failure on line 25 in src/components/Dashboard/Explorer/QueryItems/filters.test.tsx

View workflow job for this annotation

GitHub Actions / Lint, test, and build

src/components/Dashboard/Explorer/QueryItems/filters.test.tsx > useItemFilters > tags > 1

AssertionError: expected true to deeply equal false - Expected + Received - false + true ❯ assertFilterResult src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:25:49 ❯ src/components/Dashboard/Explorer/QueryItems/filters.test.tsx:227:5
}
}

describe("useItemFilters", async () => {
const { result, waitFor } = renderHook(() => useItemFilters(), { wrapper });
await waitFor(() => expect(result.current).toBeDefined());
Expand All @@ -32,7 +40,7 @@ describe("useItemFilters", async () => {
});

describe("abstract", () => {
const { evaluate } = filtersMap["abstract"];
const filter = filtersMap["abstract"];
const cases = [
{ item: mockItem({ abstract: "" }), query: "text", expected: false },
{ item: mockItem({ abstract: "text" }), query: "string", expected: false },
Expand All @@ -43,13 +51,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("hasAbstract", () => {
const { evaluate } = filtersMap["hasAbstract"];
const filter = filtersMap["hasAbstract"];
const cases = [
{ item: mockItem({ abstract: "" }), query: "true", expected: false },
{ item: mockItem({ abstract: "" }), query: "false", expected: true },
Expand All @@ -60,13 +68,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("hasCitekey", () => {
const { evaluate } = filtersMap["hasCitekey"];
const filter = filtersMap["hasCitekey"];
const cases = [
{ item: mockItem({ raw: { has_citekey: true } }), query: "true", expected: true },
{ item: mockItem({ raw: { has_citekey: true } }), query: "false", expected: false },
Expand All @@ -77,13 +85,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("hasDOI", () => {
const { evaluate } = filtersMap["hasDOI"];
const filter = filtersMap["hasDOI"];
const cases = [
{ item: mockItem({ raw: { data: { DOI: null } } }), query: "true", expected: false },
{ item: mockItem({ raw: { data: { DOI: null } } }), query: "false", expected: true },
Expand All @@ -98,13 +106,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("addedBefore", () => {
const { evaluate } = filtersMap["addedBefore"];
const filter = filtersMap["addedBefore"];

beforeAll(() => {
vi.useFakeTimers()
Expand Down Expand Up @@ -133,13 +141,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%# - $query, $item.raw.data.dateAdded",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("type", () => {
const { evaluate } = filtersMap["type"];
const filter = filtersMap["type"];
const cases = [
{ item: mockItem({ itemType: "book" }), query: "article", expected: false },
{ item: mockItem({ itemType: "book" }), query: "book", expected: true }
Expand All @@ -148,13 +156,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("hasNotes", () => {
const { evaluate } = filtersMap["hasNotes"];
const filter = filtersMap["hasNotes"];
const cases = [
{ item: mockItem({ children: { notes: [] } }), query: "true", expected: false },
{ item: mockItem({ children: { notes: [] } }), query: "false", expected: true },
Expand All @@ -165,13 +173,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("hasPDFs", () => {
const { evaluate } = filtersMap["hasPDFs"];
const filter = filtersMap["hasPDFs"];
const cases = [
{ item: mockItem({ children: { pdfs: [] } }), query: "true", expected: false },
{ item: mockItem({ children: { pdfs: [] } }), query: "false", expected: true },
Expand All @@ -182,13 +190,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("inRoam", () => {
const { evaluate } = filtersMap["inRoam"];
const filter = filtersMap["inRoam"];
const cases = [
{ item: mockItem({ inGraph: false }), query: "true", expected: false },
{ item: mockItem({ inGraph: false }), query: "false", expected: true },
Expand All @@ -199,13 +207,13 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("tags", () => {
const { evaluate } = filtersMap["tags"];
const filter = filtersMap["tags"];
const cases = [
{ item: mockItem({ tags: [] }), query: "systems", expected: false },
{ item: mockItem({ tags: ["todo"] }), query: "systems", expected: false },
Expand All @@ -216,7 +224,7 @@ describe("useItemFilters", async () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});
Expand Down
1 change: 0 additions & 1 deletion src/components/Dashboard/Explorer/QueryItems/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useTypemapSettings } from "Components/UserSettings";
import { QueryFilter, evaluateBoolean, filterWithPastDate, filterWithQuery } from "@services/search";

import { searchEngine } from "../../../../utils";
import { parseDateInThePast, parseDateRangeInThePast } from "../helpers";
import { ZCleanItemTop } from "Types/transforms";


Expand Down
14 changes: 11 additions & 3 deletions src/components/Dashboard/Explorer/QueryNotes/filters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mock } from "vitest-mock-extended";

import { QueryFilter } from "@services/search";
import { filterHasEvaluate, QueryFilter } from "@services/search";

import { noteFilters } from "./filters";
import { ZItemAnnotation, ZItemNote } from "Types/transforms";
Expand All @@ -11,8 +11,16 @@ const filtersMap = noteFilters.reduce<Record<string, QueryFilter<ZItemNote | ZIt
...obj, [filter.value]: filter
}), {});

const assertFilterResult = (filter: QueryFilter, { query, item, expected }: { query: string, item: any, expected: boolean }) => {
if (filterHasEvaluate(filter)) {
expect(filter.evaluate(query, item)).toEqual(expected);
} else {
expect(Boolean(filter.filter(query, [item]))).toEqual(expected);
}
}

describe("tags", () => {
const { evaluate } = filtersMap["tags"];
const filter = filtersMap["tags"];
const cases = [
{ item: mockAnnotation({ data: { tags: [] }}), query: "systems", expected: false },
{ item: mockAnnotation({ data: { tags: [{ tag: "todo" }] }}), query: "systems", expected: false },
Expand All @@ -23,7 +31,7 @@ describe("tags", () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});
18 changes: 13 additions & 5 deletions src/components/Dashboard/Explorer/QueryPDFs/filters.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mock } from "vitest-mock-extended";

import { QueryFilter } from "@services/search";
import { filterHasEvaluate, QueryFilter } from "@services/search";

import { pdfFilters } from "./filters";
import { ZCleanItemPDF, ZItemAnnotation } from "Types/transforms";
Expand All @@ -11,8 +11,16 @@ const filtersMap = pdfFilters.reduce<Record<string, QueryFilter<ZCleanItemPDF>>>
...obj, [filter.value]: filter
}), {});

const assertFilterResult = (filter: QueryFilter, { query, item, expected }: { query: string, item: any, expected: boolean }) => {
if (filterHasEvaluate(filter)) {
expect(filter.evaluate(query, item)).toEqual(expected);
} else {
expect(Boolean(filter.filter(query, [item]))).toEqual(expected);
}
}

describe("hasAnnotations", () => {
const { evaluate } = filtersMap["hasAnnotations"];
const filter = filtersMap["hasAnnotations"];
const cases = [
{ item: mockPDF({ annotations: [] }), query: "true", expected: false },
{ item: mockPDF({ annotations: [] }), query: "false", expected: true },
Expand All @@ -23,13 +31,13 @@ describe("hasAnnotations", () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

describe("tags", () => {
const { evaluate } = filtersMap["tags"];
const filter = filtersMap["tags"];
const cases = [
{ item: mockPDF({ tags: [] }), query: "systems", expected: false },
{ item: mockPDF({ tags: ["todo"] }), query: "systems", expected: false },
Expand All @@ -40,7 +48,7 @@ describe("tags", () => {
test.each(cases)(
"%#",
({ item, query, expected }) => {
expect(evaluate(query, item)).toEqual(expected);
assertFilterResult(filter, { query, item, expected })
}
)
});

0 comments on commit 38e5614

Please sign in to comment.