Skip to content

Commit 2bdc99e

Browse files
committed
Add settings tests
1 parent 8b98055 commit 2bdc99e

File tree

5 files changed

+173
-16
lines changed

5 files changed

+173
-16
lines changed

connectors/__tests__/custom-index-name.tests.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ const createCollectionConnector = require('../collection')
66
jest.mock('meilisearch')
77

88
const addDocumentsMock = jest.fn(() => 10)
9+
const updateSettingsMock = jest.fn(() => 10)
10+
911
const mockIndex = jest.fn(() => ({
1012
addDocuments: addDocumentsMock,
13+
updateSettings: updateSettingsMock,
1114
}))
1215

1316
MeiliSearch.mockImplementation(() => {

connectors/__tests__/entry-transformer.tests.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ const createCollectionConnector = require('../collection')
66
jest.mock('meilisearch')
77

88
const addDocumentsMock = jest.fn(() => 10)
9+
const updateSettingsMock = jest.fn(() => 10)
10+
911
const mockIndex = jest.fn(() => ({
1012
addDocuments: addDocumentsMock,
13+
updateSettings: updateSettingsMock,
1114
}))
1215

1316
MeiliSearch.mockImplementation(() => {
@@ -76,7 +79,6 @@ describe('Entry transformation', () => {
7679
})
7780

7881
afterEach(() => {
79-
jest.resetAllMocks()
8082
jest.clearAllMocks()
8183
jest.restoreAllMocks()
8284
})
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
const { MeiliSearch } = require('meilisearch')
2+
const createMeiliSearchConnector = require('../meilisearch')
3+
const createStoreConnector = require('../store')
4+
const createCollectionConnector = require('../collection')
5+
6+
jest.mock('meilisearch')
7+
8+
const addDocumentsMock = jest.fn(() => 10)
9+
const updateSettingsMock = jest.fn(() => 10)
10+
11+
const mockIndex = jest.fn(() => ({
12+
addDocuments: addDocumentsMock,
13+
updateSettings: updateSettingsMock,
14+
}))
15+
16+
MeiliSearch.mockImplementation(() => {
17+
return {
18+
getOrCreateIndex: () => {
19+
return mockIndex
20+
},
21+
index: mockIndex,
22+
}
23+
})
24+
25+
const storeClientMock = {
26+
set: jest.fn(() => 'test'),
27+
get: jest.fn(() => 'test'),
28+
}
29+
30+
const servicesMock = {
31+
restaurant: {
32+
count: jest.fn(() => {
33+
return 11
34+
}),
35+
find: jest.fn(() => {
36+
return [{ id: '1', collection: [{ name: 'one' }, { name: 'two' }] }]
37+
}),
38+
},
39+
}
40+
41+
const transformEntryMock = jest.fn(function ({ entry }) {
42+
const transformedEntry = {
43+
...entry,
44+
collection: entry.collection.map(cat => cat.name),
45+
}
46+
return transformedEntry
47+
})
48+
49+
const loggerMock = {
50+
warn: jest.fn(() => 'test'),
51+
}
52+
53+
describe('Test MeiliSearch settings', () => {
54+
let storeConnector
55+
beforeEach(async () => {
56+
jest.clearAllMocks()
57+
jest.restoreAllMocks()
58+
storeConnector = createStoreConnector({
59+
storeClient: storeClientMock,
60+
})
61+
})
62+
63+
test('Test not settings field in configuration', async () => {
64+
const modelMock = {
65+
restaurant: {
66+
meilisearch: {
67+
indexName: 'my_restaurant',
68+
transformEntry: transformEntryMock,
69+
},
70+
},
71+
}
72+
const collectionConnector = createCollectionConnector({
73+
logger: loggerMock,
74+
models: modelMock,
75+
services: servicesMock,
76+
})
77+
const meilisearchConnector = await createMeiliSearchConnector({
78+
collectionConnector,
79+
storeConnector,
80+
})
81+
const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName')
82+
const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings')
83+
84+
await meilisearchConnector.addCollectionInMeiliSearch('restaurant')
85+
86+
expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1)
87+
88+
expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant')
89+
expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant')
90+
expect(getSettingsSpy).toHaveBeenCalledWith('restaurant')
91+
expect(getSettingsSpy).toHaveReturnedWith({})
92+
})
93+
94+
test('Test a empty setting object in configuration', async () => {
95+
const modelMock = {
96+
restaurant: {
97+
meilisearch: {
98+
indexName: 'my_restaurant',
99+
transformEntry: transformEntryMock,
100+
settings: {},
101+
},
102+
},
103+
}
104+
const collectionConnector = createCollectionConnector({
105+
logger: loggerMock,
106+
models: modelMock,
107+
services: servicesMock,
108+
})
109+
const meilisearchConnector = await createMeiliSearchConnector({
110+
collectionConnector,
111+
storeConnector,
112+
})
113+
const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName')
114+
const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings')
115+
116+
await meilisearchConnector.addCollectionInMeiliSearch('restaurant')
117+
118+
expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1)
119+
120+
expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant')
121+
expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant')
122+
expect(getSettingsSpy).toHaveBeenCalledWith('restaurant')
123+
expect(getSettingsSpy).toHaveReturnedWith({})
124+
})
125+
126+
test('Test a setting object with one field in configuration', async () => {
127+
const modelMock = {
128+
restaurant: {
129+
meilisearch: {
130+
indexName: 'my_restaurant',
131+
transformEntry: transformEntryMock,
132+
settings: {
133+
searchableAttributes: ['*'],
134+
},
135+
},
136+
},
137+
}
138+
const collectionConnector = createCollectionConnector({
139+
logger: loggerMock,
140+
models: modelMock,
141+
services: servicesMock,
142+
})
143+
const meilisearchConnector = await createMeiliSearchConnector({
144+
collectionConnector,
145+
storeConnector,
146+
})
147+
const getIndexNameSpy = jest.spyOn(collectionConnector, 'getIndexName')
148+
const getSettingsSpy = jest.spyOn(collectionConnector, 'getSettings')
149+
150+
await meilisearchConnector.addCollectionInMeiliSearch('restaurant')
151+
152+
expect(servicesMock.restaurant.count).toHaveBeenCalledTimes(1)
153+
154+
expect(getIndexNameSpy).toHaveBeenCalledWith('restaurant')
155+
expect(getIndexNameSpy).toHaveReturnedWith('my_restaurant')
156+
expect(getSettingsSpy).toHaveBeenCalledWith('restaurant')
157+
expect(getSettingsSpy).toHaveReturnedWith({ searchableAttributes: ['*'] })
158+
})
159+
})

connectors/meilisearch/index.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@ module.exports = async ({ storeConnector, collectionConnector }) => {
264264
const client = MeiliSearch({ apiKey, host })
265265
const indexUid = collectionConnector.getIndexName(collection)
266266

267+
// Get MeiliSearch Index settings from model
268+
const settings = collectionConnector.getSettings(collection)
269+
await client.index(indexUid).updateSettings(settings)
270+
267271
// Callback function for batching action
268272
const addDocuments = async (entries, collection) => {
269273
if (entries.length === 0) {
@@ -279,20 +283,6 @@ module.exports = async ({ storeConnector, collectionConnector }) => {
279283
entries: transformedEntries,
280284
})
281285

282-
// Get MeiliSearch Index settings from model
283-
const settings = collectionConnector.getSettings(collection)
284-
285-
// Update MeiliSearch index settings if settings not empty
286-
if (settings && Object.keys(settings).length !== 0) {
287-
try {
288-
await client.index(indexUid).updateSettings(settings)
289-
} catch (error) {
290-
console.error(
291-
`[MEILISEARCH]: Failed updating MeiliSearch settings for collection: ${collection}. Please check your settings.`
292-
)
293-
}
294-
}
295-
296286
// Add documents in MeiliSearch
297287
const { updateId } = await client
298288
.index(indexUid)

playground/api/restaurant/models/restaurant.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ module.exports = {
1313
};
1414
return transformed;
1515
},
16-
indexName: "my_restaurant"
16+
indexName: "my_restaurant",
17+
settings: {
18+
"searchableAttributes": ["*"]
19+
}
1720
}
1821
}

0 commit comments

Comments
 (0)