Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scc 3773/utils testing #31

Merged
merged 29 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ba1e24f
test utils
charmingduchess Sep 29, 2023
a77dc10
linting
charmingduchess Sep 29, 2023
433ba91
add searchutils tests
charmingduchess Sep 29, 2023
0ec82ae
search utils
charmingduchess Sep 29, 2023
27b4f91
linting and more tests
charmingduchess Oct 2, 2023
e2775f7
mvdir
charmingduchess Oct 2, 2023
3c1deb2
rename file
charmingduchess Oct 2, 2023
77eee0e
add empty string handling for sort_direction
charmingduchess Oct 2, 2023
eefdbc7
404 tests
charmingduchess Oct 3, 2023
a2226b4
linting
charmingduchess Oct 3, 2023
c62107d
wip
charmingduchess Oct 3, 2023
0fb8085
merge main
charmingduchess Oct 3, 2023
f9c2f97
huzzah
charmingduchess Oct 3, 2023
d9d7400
search results tests
charmingduchess Oct 4, 2023
3ffcd11
start adv search tests
charmingduchess Oct 4, 2023
33984c8
finish advanced search
charmingduchess Oct 6, 2023
65b1c39
update tests directory
charmingduchess Oct 6, 2023
e5f9e3e
add Layout tests
charmingduchess Oct 6, 2023
b88b223
rc link test
charmingduchess Oct 6, 2023
a782551
update ts/testing config
charmingduchess Oct 6, 2023
0275010
add subnav tests
charmingduchess Oct 6, 2023
f08c473
rm unused vars
charmingduchess Oct 6, 2023
7988bb8
fix test label
charmingduchess Oct 10, 2023
e596945
rm await act
charmingduchess Oct 10, 2023
9069c1b
add comments
charmingduchess Oct 10, 2023
38997fb
finish 404 tests
charmingduchess Oct 10, 2023
2ea9baf
replace await and move tests
charmingduchess Oct 10, 2023
d912ffa
move more tests
charmingduchess Oct 10, 2023
4ec81b0
Merge branch 'SCC-3772/page-component-tests' into SCC-3773/utils-testing
charmingduchess Oct 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/utils/searchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ import type {
function getSortQuery(sortBy = "", order = ""): string {
const reset = sortBy === "relevance"
let sortQuery = ""
const sortDirectionQuery = order === "" ? "" : `&sort_direction=${order}`

if (sortBy?.length && !reset) {
sortQuery = `&sort=${sortBy}&sort_direction=${order}`
sortQuery = `&sort=${sortBy}${sortDirectionQuery}`
EdwinGuzman marked this conversation as resolved.
Show resolved Hide resolved
}

return sortQuery
Expand Down Expand Up @@ -85,7 +86,7 @@ function getFilterQuery(filters: SearchFilters) {

/**
* getQueryString
* Builds a query string from a SearchParams object, setting defaults on some undefined params.
* Builds a query string from a SearchParams object
*/
export function getQueryString({
sortBy = "relevance",
Expand Down Expand Up @@ -162,17 +163,18 @@ export function mapQueryToSearchParams({
* mapRequestBodyToSearchParams
* Maps the POST request body from an JS disabled advanced search to a SearchParams object
*/
export function mapRequestBodyToSearchParams({
q,
page,
contributor,
title,
subject,
language,
materialType,
dateAfter,
dateBefore,
}): SearchParams {
export function mapRequestBodyToSearchParams(reqBody): SearchParams {
const {
q,
page,
contributor,
title,
subject,
language,
materialType,
dateAfter,
dateBefore,
} = reqBody
return {
q,
page,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from "react"
import { render, screen } from "@testing-library/react"
import "@testing-library/jest-dom"
import Home from "../../pages/index"
import Home from "../../../pages/index"

describe("Home", () => {
it("should render an H2", () => {
Expand Down
24 changes: 24 additions & 0 deletions test/__test__/utils/appUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { getActivePage } from "../../../src/utils/appUtils"
import { PATHS } from "../../../src/config/constants"

describe("appUtils", () => {
describe("getActivePage", () => {
it("should return search for / or /search", () => {
expect(getActivePage(PATHS.HOME)).toBe("search")
expect(getActivePage(PATHS.SEARCH)).toBe("search")
})
it("should return advanced for ADVANCED SEARCH", () => {
expect(getActivePage(PATHS.ADVANCED_SEARCH)).toBe("advanced")
})
it("should return advanced for PATHS[404]", () => {
expect(getActivePage(PATHS["404"])).toBe("404")
expect(getActivePage(PATHS["404_REDIRECT"])).toBe("404")
})
it("should return an empty string for all other cases", () => {
expect(getActivePage("spaghetti")).toBe("")
})
it("should handle an empty string", () => {
expect(getActivePage("")).toBe("")
})
})
})
36 changes: 36 additions & 0 deletions test/__test__/utils/bibUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { standardizeBibId } from "../../../src/utils/bibUtils"

describe("bibUtils", () => {
describe("standardizeBibId", () => {
describe("standardizeBib", () => {
it("doesn't mess with kosher id", () => {
expect(standardizeBibId("b12345678")).toBe("b12345678")
expect(standardizeBibId("hb123456789123456789")).toBe(
"hb123456789123456789"
)
})
it("removes check digit", () => {
expect(standardizeBibId("b12345678x")).toBe("b12345678")
expect(standardizeBibId("b12345678X")).toBe("b12345678")
expect(standardizeBibId("b123456781")).toBe("b12345678")
})
it("lower cases everything", () => {
expect(standardizeBibId("B12345678")).toBe("b12345678")
expect(standardizeBibId("CB1234567")).toBe("cb1234567")
expect(standardizeBibId("Hb123456789123456789")).toBe(
"hb123456789123456789"
)
expect(standardizeBibId("PB1234567")).toBe("pb1234567")
expect(standardizeBibId("PB1234567812345678")).toBe(
"pb1234567812345678"
)
})
it("returns value provided if input does not match bib id regexes", () => {
expect(standardizeBibId("b1234567899")).toBe("b1234567899")
expect(standardizeBibId("i am not a bib id hb123")).toBe(
"i am not a bib id hb123"
)
})
})
})
})
109 changes: 109 additions & 0 deletions test/__test__/utils/drbUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { getDRBQueryStringFromSearchParams } from "../../../src/utils/drbUtils"
import { DRB_RESULTS_PER_PAGE } from "../../../src/config/constants"
import { queryParamsEquality } from "../../helpers/searchHelpers"

const checkQueryParamsEquality = queryParamsEquality(
getDRBQueryStringFromSearchParams
)

describe("researchNowUtils", () => {
charmingduchess marked this conversation as resolved.
Show resolved Hide resolved
describe("getDRBQueryStringFromSearchParams", () => {
it("should handle empty query", () => {
const testQuery = `?query=keyword%3A*&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(checkQueryParamsEquality(testQuery, {})).toBe(true)
})

it("should handle simple keyword query", () => {
const testQuery = `?query=keyword%3Atoast&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(checkQueryParamsEquality(testQuery, { q: "toast" })).toBe(true)
})

it("should handle field", () => {
const testQuery = `?query=title%3Atoast&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(
checkQueryParamsEquality(testQuery, { q: "toast", field: "title" })
).toBe(true)
})

it("should handle keyword & subject query", () => {
const testQuery = `?query=keyword%3Atoast,subject%3ASnacks&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(
checkQueryParamsEquality(testQuery, {
q: "toast",
filters: { subjectLiteral: "Snacks" },
})
).toBe(true)
})

it("should handle contributor filter", () => {
const testQuery = `?query=keyword%3A*,author%3APoe&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(
checkQueryParamsEquality(testQuery, {
filters: { contributorLiteral: "Poe" },
})
).toBe(true)
})

it("should handle lang filter", () => {
const testQuery = `?query=keyword%3A*&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}&filter=language%3Aen`
expect(
checkQueryParamsEquality(testQuery, {
filters: { language: "lang:en" },
})
).toBe(true)
})

it("should handle dateAfter filter", () => {
const testQuery = `?query=keyword%3A*&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}&filter=startYear%3A2000`
expect(
checkQueryParamsEquality(testQuery, { filters: { dateAfter: "2000" } })
).toBe(true)
})

it("should handle dateBefore filter", () => {
const testQuery = `?query=keyword%3A*&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}&filter=endYear%3A2020`
expect(
checkQueryParamsEquality(testQuery, { filters: { dateBefore: "2020" } })
).toBe(true)
})

it("should handle dateAfter & dateBefore filter", () => {
const testQuery = `?query=keyword%3A*&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}&filter=startYear%3A2000,endYear%3A2020`
expect(
checkQueryParamsEquality(testQuery, {
filters: { dateAfter: "2000", dateBefore: "2020" },
})
).toBe(true)
})

it("should handle contributor param", () => {
const testQuery = `?query=keyword%3A*%2Cauthor%3APoe&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(checkQueryParamsEquality(testQuery, { contributor: "Poe" })).toBe(
true
)
})

it("should handle title param", () => {
const testQuery = `?query=keyword%3A*%2Ctitle%3ARaven&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(checkQueryParamsEquality(testQuery, { title: "Raven" })).toBe(true)
})

it("should handle subject param", () => {
const testQuery = `?query=keyword%3A*%2Csubject%3Acorvids&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(checkQueryParamsEquality(testQuery, { subject: "corvids" })).toBe(
true
)
})

it("should combine query params", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The more I look at this file the more complex the query object vs the url query is. All seems fine but just pointing out the complexity.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how so?

const testQuery = `?query=keyword%3ARaven%2Cauthor%3APoe%2Csubject%3Acorvids&page=1&source=catalog&size=${DRB_RESULTS_PER_PAGE}`
expect(
checkQueryParamsEquality(testQuery, {
contributor: "Poe",
subject: "corvids",
q: "Raven",
})
).toBe(true)
})
})
})
66 changes: 66 additions & 0 deletions test/__test__/utils/searchUtils.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import {
getQueryString,
mapQueryToSearchParams,
mapRequestBodyToSearchParams,
} from "../../../src/utils/searchUtils"
import { queryParamsEquality } from "../../helpers/searchHelpers"

const checkQueryParamsEquality = queryParamsEquality(getQueryString)

describe("searchUtils", () => {
describe("getQueryString", () => {
it("constructs a basic query", () => {
const testQuery =
"?q=shel%20silverstein&search_scope=contributor&sort=datePublished&sort_direction=asc"
expect(
checkQueryParamsEquality(testQuery, {
q: "shel silverstein",
field: "contributor",
sortBy: "datePublished",
order: "asc",
})
).toBe(true)
})
})
describe("mapQueryToSearchParams", () => {
it("should consolidate identifiers and change some keys", () => {
expect(
mapQueryToSearchParams({
issn: "123",
isbn: "456",
search_scope: "contributor",
sort_direction: "asc",
sort: "spaghetti",
})
).toEqual({
identifiers: {
issn: "123",
isbn: "456",
},
field: "contributor",
order: "asc",
sortBy: "spaghetti",
})
})
})
describe("mapRequestBodyToSearchParams", () => {
it("combines filters", () => {
const params = mapRequestBodyToSearchParams({
q: "spaghetti",
language: "igbo",
materialType: "scroll",
dateAfter: "1900",
dateBefore: "1902",
})
expect(params).toEqual({
q: "spaghetti",
filters: {
language: "igbo",
materialType: "scroll",
dateAfter: "1900",
dateBefore: "1902",
},
})
})
})
})
21 changes: 21 additions & 0 deletions test/helpers/searchHelpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* An effort to avoid brittleness due to potentially variable order in query
* parameters. This testing util splits query strings into arrays that are
* compared for matching elements instead of comparing for strict equality of
* string literals.
* */

export const queryParamsEquality = (queryConstructor) => {
return (test, queryParamsToConstruct) => {
const testQueries = test.substring(1).split("&")
const constructedQueries = queryConstructor(queryParamsToConstruct)
.substring(1)
.split("&")
return (
testQueries.every((queryParam) =>
constructedQueries.includes(queryParam)
) &&
constructedQueries.every((queryParam) => testQueries.includes(queryParam))
)
}
}