Skip to content

Commit e9b0528

Browse files
feat: test service for injection
1 parent 9f1c0fe commit e9b0528

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
import { Inject } from '@nestjs/common'
2+
import { ELASTICSEARCH_CATALOG_NAME } from 'lib/constants'
3+
import { ClassConstructor, Document } from 'lib/types'
24
import { getCatalogInjectionToken } from 'nestjs/utils'
35

4-
export const InjectCatalog = (index: string) => Inject(getCatalogInjectionToken(index))
6+
export const InjectCatalog = <TDocument extends Document>(document: ClassConstructor<TDocument>) => {
7+
const index = Reflect.getMetadata(ELASTICSEARCH_CATALOG_NAME, document)
8+
9+
if (!index) {
10+
throw new Error('Failed to find registered catalog index')
11+
}
12+
13+
return Inject(getCatalogInjectionToken(index))
14+
}

src/test/features/2-querying-catalog-in-other-service.spec.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { setupNestApplication } from 'test/toolkit'
22
import { ElasticsearchModule } from 'nestjs/elasticsearch.module'
3-
import { HomeDocument, PropertyType } from 'test/module'
4-
import { getBoolQuery, getTermQuery } from 'lib/queries'
3+
import { HomeDocument, TestService } from 'test/module'
54
import { validateSync } from 'class-validator'
6-
import { getCatalogInjectionToken } from 'nestjs/utils'
7-
import { Catalog } from 'nestjs/injectables'
85

96
describe('Making a search', () => {
107
const { app } = setupNestApplication({
8+
providers: [TestService],
119
imports: [
1210
ElasticsearchModule.register({
1311
node: 'http://localhost:9200'
@@ -19,24 +17,14 @@ describe('Making a search', () => {
1917
})
2018

2119
it('has catalog available', () => {
22-
const catalog = app.get<Catalog<HomeDocument>>(getCatalogInjectionToken('homes'))
20+
const catalog = app.get(TestService)
2321

2422
expect(catalog).toBeDefined()
2523
})
2624

2725
it('makes a test search query', async () => {
28-
const catalog = app.get<Catalog<HomeDocument>>(getCatalogInjectionToken('homes'))
29-
const result = await catalog.search({
30-
size: 10,
31-
body: {
32-
query: getBoolQuery({
33-
must: [
34-
getTermQuery('propertyType.keyword', PropertyType.Flat)
35-
]
36-
})
37-
}
38-
})
39-
26+
const service = app.get(TestService)
27+
const result = await service.getHomeDocuments()
4028
const allDocumentsAreValid = result.documents.every(document => {
4129
const errors = validateSync(document)
4230

src/test/module/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './homes.catalog'
22
export * from './enums'
3+
export * from './test.service'

src/test/module/test.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Injectable } from '@nestjs/common'
2+
import { InjectCatalog } from 'lib/decorators'
3+
import { getBoolQuery, getTermQuery } from 'lib/queries'
4+
import { Catalog } from 'nestjs/injectables'
5+
import { PropertyType } from './enums'
6+
import { HomeDocument } from './homes.catalog'
7+
8+
@Injectable()
9+
export class TestService {
10+
@InjectCatalog(HomeDocument)
11+
private readonly homes: Catalog<HomeDocument>
12+
13+
getHomeDocuments() {
14+
return this.homes.search({
15+
size: 10,
16+
body: {
17+
query: getBoolQuery({
18+
must: [
19+
getTermQuery('propertyType.keyword', PropertyType.Flat)
20+
]
21+
})
22+
}
23+
})
24+
}
25+
}

0 commit comments

Comments
 (0)