|
| 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 | +}) |
0 commit comments