Skip to content

Commit 9a18b95

Browse files
authored
Merge pull request #15 from blue-core-lod/t11-search-bluecore
Initial Support for Blue Core Search
2 parents 5d5af68 + 9674ec3 commit 9a18b95

File tree

19 files changed

+212
-179
lines changed

19 files changed

+212
-179
lines changed

__tests__/actionCreators/resources.newResourceFromDataset.test.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ describe("newResourceFromDataset", () => {
296296
(action) => action.type === "ADD_SUBJECT"
297297
)
298298

299-
// console.log(JSON.safeStringify(addSubjectAction))
300299
expect(safeAction(addSubjectAction)).toEqual(expectedOrderedAction)
301300

302301
expect(actions).toHaveAction("SET_UNUSED_RDF", {

__tests__/components/search/SinopiaSearchResults.test.js

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,9 @@ import { screen } from "@testing-library/react"
55
import { createStore, renderComponent } from "testUtils"
66
import { createState } from "stateUtils"
77

8-
let mockKeycloak
9-
10-
jest.mock("keycloak-js", () => {
11-
mockKeycloak = {
12-
init: jest.fn(() => Promise.resolve(true)),
13-
token: "Secret-Token",
14-
authenticated: true,
15-
isTokenExpired: jest.fn(),
16-
updateToken: jest.fn(),
17-
tokenParsed: {
18-
preferred_username: "Foo McBar",
19-
},
20-
}
21-
22-
return jest.fn().mockImplementation((config) => {
23-
return mockKeycloak
24-
})
25-
})
8+
jest.mock("KeycloakContext", () => ({
9+
useKeycloak: jest.fn().mockReturnValue({}),
10+
}))
2611

2712
describe("<SinopiaSearchResults />", () => {
2813
describe("when there are no search results", () => {

__tests__/components/search/SinopiaSort.test.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ describe("<SinopiaSort />", () => {
4444

4545
it("clicking changes the sort order", async () => {
4646
const mockGetSearchResults = jest.fn()
47+
const keycloak = jest.fn().mockReturnValue({
48+
token: "abcdefg12345",
49+
})
50+
4751
server.getSearchResultsWithFacets = mockGetSearchResults.mockResolvedValue([
4852
{
4953
totalHits: 1,
@@ -67,20 +71,22 @@ describe("<SinopiaSort />", () => {
6771
}
6872

6973
const store = createStore(state)
70-
const keycloak = jest.fn().mockReturnValue({
71-
token: "abcdefg12345",
72-
})
74+
7375
renderComponent(<SinopiaSort />, store)
7476

7577
fireEvent.click(screen.getByText("Sort by"))
7678
fireEvent.click(screen.getByText("Label, ascending"))
7779

7880
await screen.findByText("Label, ascending", { selector: ".active" })
79-
expect(server.getSearchResultsWithFacets).toHaveBeenCalledWith("twain", {
80-
startOfRange: 0,
81-
resultsPerPage: 15,
82-
sortField: "label",
83-
sortOrder: "asc",
84-
})
81+
expect(server.getSearchResultsWithFacets).toHaveBeenCalledWith(
82+
"twain",
83+
{
84+
startOfRange: 0,
85+
resultsPerPage: 15,
86+
sortField: "label",
87+
sortOrder: "asc",
88+
},
89+
undefined
90+
)
8591
})
8692
})

__tests__/feature/search.test.js

Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ describe("sinopia resource search", () => {
1515
_index: "sinopia_resources",
1616
_type: "sinopia",
1717
_id: "resource/cornell/34ef053e-f558-4299-a8a7-c8b79a598d99",
18-
_score: 0.2876821,
19-
_source: {
20-
title: ["foo bar"],
21-
uri: "http://platform:8080/resource/cornell/34ef053e-f558-4299-a8a7-c8b79a598d99",
22-
label: "foo bar",
23-
type: ["http://id.loc.gov/ontologies/bibframe/AbbreviatedTitle"],
24-
group: "cornell",
18+
uri: "http://platform:8080/resource/cornell/34ef053e-f558-4299-a8a7-c8b79a598d99",
19+
data: {
20+
title: {
21+
mainTitle: "foo bar"
22+
},
23+
"@type": ["http://id.loc.gov/ontologies/bibframe/AbbreviatedTitle"],
24+
group: "blue core",
2525
created: "2019-11-27T19:05:50.496Z",
2626
modified: "2019-11-27T19:05:50.496Z",
2727
},
@@ -31,13 +31,13 @@ describe("sinopia resource search", () => {
3131
_index: "sinopia_resources",
3232
_type: "sinopia",
3333
_id: "resource/cornell/a96f16c1-a15c-4f4f-8a25-7ed49ba1eebe",
34-
_score: 0.2876819,
35-
_source: {
36-
title: ["foo"],
37-
uri: "http://platform:8080/resource/cornell/a96f16c1-a15c-4f4f-8a25-7ed49ba1eebe",
38-
label: "foo",
39-
type: ["http://id.loc.gov/ontologies/bibframe/AbbreviatedTitle"],
40-
group: "cornell",
34+
uri: "http://platform:8080/resource/cornell/a96f16c1-a15c-4f4f-8a25-7ed49ba1eebe",
35+
data: {
36+
title: {
37+
mainTitle: "foo"
38+
},
39+
"@type": ["http://id.loc.gov/ontologies/bibframe/AbbreviatedTitle"],
40+
group: "blue core",
4141
created: "2019-11-27T19:05:52.496Z",
4242
modified: "2019-11-27T19:05:52.496Z",
4343
},
@@ -48,12 +48,13 @@ describe("sinopia resource search", () => {
4848
_type: "sinopia",
4949
_id: "resource/stanford/a96f16c1-a15c-4f4f-8a25-7ed49ba1eebe",
5050
_score: 0.2876822,
51-
_source: {
52-
title: ["baz"],
53-
uri: "http://platform:8080/resource/stanford/d7b0eb50-17bb-4258-83be-2cef2e9fc3ad",
54-
label: "baz",
55-
type: ["http://id.loc.gov/ontologies/bibframe/Title"],
56-
group: "stanford",
51+
uri: "http://platform:8080/resource/stanford/d7b0eb50-17bb-4258-83be-2cef2e9fc3ad",
52+
data: {
53+
title: {
54+
mainTitle: "baz"
55+
} ,
56+
"@type": ["http://id.loc.gov/ontologies/bibframe/Title"],
57+
group: "blue core",
5758
created: "2019-11-27T19:05:48.496Z",
5859
modified: "2019-11-27T19:05:48.496Z",
5960
},
@@ -68,11 +69,8 @@ describe("sinopia resource search", () => {
6869
skipped: 0,
6970
failed: 0,
7071
},
71-
hits: {
72-
total: { value: 3 },
73-
max_score: 0.2876821,
74-
hits: [fooBarHit, fooHit, bazHit],
75-
},
72+
results: [fooBarHit, fooHit, bazHit],
73+
total: 3,
7674
}
7775

7876
const successResultResorted = {
@@ -84,11 +82,12 @@ describe("sinopia resource search", () => {
8482
skipped: 0,
8583
failed: 0,
8684
},
87-
hits: {
88-
total: { value: 3 },
89-
max_score: 0.2876821,
90-
hits: [bazHit, fooBarHit, fooHit],
91-
},
85+
results: [bazHit, fooBarHit, fooHit],
86+
// hits: {
87+
// total: { value: 3 },
88+
// max_score: 0.2876821,
89+
// results: [bazHit, fooBarHit, fooHit],
90+
// },
9291
}
9392

9493
const successResultPage1 = {

__tests__/sinopiaApi.test.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ describe("fetchResource", () => {
175175
describe("postResource", () => {
176176
const state = createState({ hasResourceWithLiteral: true })
177177
const currentUser = selectUser(state)
178-
console.log(`state`, state)
179-
console.log(`currentUser`, currentUser)
180178

181179
describe("with a new resource", () => {
182180
const resource = selectFullSubject(state, "t9zVwg2zO")

src/Config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class Config {
6969
* This is the public endpont for the sinopia search.
7070
*/
7171
static get searchPath() {
72-
return "/api/search/sinopia_resources/_search"
72+
// return "/api/search/sinopia_resources/_search"
73+
return "/search"
7374
}
7475

7576
static get templateSearchPath() {

src/KeycloakContext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const KeycloakProvider = ({ children }) => {
2727
const [initialized, setInitialized] = useState(false)
2828

2929
useEffect(() => {
30+
if (keycloak.initialized) return
3031
keycloak.init().then((authenticated) => {
3132
setAuthenticated(authenticated)
3233
setInitialized(true)

src/actionCreators/authenticate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const authenticate = (keycloak) => async (dispatch, getState) => {
1616
}
1717
const userInfo = keycloak.tokenParsed
1818
dispatch(setUser(toUser(userInfo)))
19-
dispatch(loadUserData(userInfo.preferred_username))
19+
dispatch(loadUserData(userInfo.preferred_username, keycloak))
2020
return Promise.resolve(true)
2121
}
2222
dispatch(removeUser())

src/actionCreators/history.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,19 @@ export const loadTemplateHistory = (templateIds) => (dispatch) => {
3333
.catch((err) => console.error(err))
3434
}
3535

36-
export const loadSearchHistory = (searches) => (dispatch) => {
36+
export const loadSearchHistory = (searches, keycloak) => (dispatch) => {
3737
if (_.isEmpty(searches)) return
3838
searches.reverse().forEach((search) => {
3939
const authorityConfig = findAuthorityConfig(search.authorityUri)
4040
if (!authorityConfig) return
4141

4242
dispatch(
43-
addSearchHistory(search.authorityUri, authorityConfig.label, search.query)
43+
addSearchHistory(
44+
search.authorityUri,
45+
authorityConfig.label,
46+
search.query,
47+
keycloak
48+
)
4449
)
4550
})
4651
}

src/actionCreators/search.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,19 @@ import { isBfWorkInstanceItem } from "utilities/Bibframe"
1616
import { loadSearchRelationships } from "./relationships"
1717

1818
export const fetchSinopiaSearchResults =
19-
(query, options, errorKey) => (dispatch) => {
19+
(query, options, errorKey, keycloak) => (dispatch) => {
2020
dispatch(clearErrors(errorKey))
21-
return getSearchResultsWithFacets(query, options).then(
21+
return getSearchResultsWithFacets(query, options, keycloak).then(
2222
([response, facetResponse]) => {
23-
dispatch(addSearchHistory(sinopiaSearchUri, "Sinopia resources", query))
24-
dispatch(addApiSearchHistory(sinopiaSearchUri, query))
23+
dispatch(
24+
addSearchHistory(
25+
sinopiaSearchUri,
26+
"Sinopia resources",
27+
query,
28+
keycloak
29+
)
30+
)
31+
dispatch(addApiSearchHistory(sinopiaSearchUri, query, keycloak))
2532
dispatch(
2633
setSearchResults(
2734
"resource",

0 commit comments

Comments
 (0)