forked from bloom-housing/bloom
-
Notifications
You must be signed in to change notification settings - Fork 1
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
Add initial filtering capability and tests to listings service #18
Merged
Merged
Changes from 14 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
7d26cd1
initial figuring out how listings work
avaleske eb67706
initial figuring out how listings work
avaleske db2f81f
merging main into this branch
avaleske 19072d9
Merge branch 'main' into feature/avaleske-initial-filters
avaleske f5e5665
add some more debugging
avaleske a9a1cd9
Merge branch 'main' into feature/avaleske-initial-filters
avaleske b05ea15
adds where clause to listings request
avaleske 62b4401
Gets basic filtering for the neighborhood to work
avaleske ff4004d
Re-add jsonpath code
avaleske 121e68e
Add @expose decorator to jsonpath param
avaleske 8cbfc59
Add listings controller test scaffolding
avaleske cec9a0b
Adds test scaffolding for Listings service
avaleske 18e3274
Add tests for listings service, update listings service to use inject…
avaleske ef17dc2
Remove test strings from tsx files
avaleske a350346
Merge branch 'main' into feature/avaleske-initial-filters
avaleske 4b12cdf
updates comment on listings service where clause
avaleske File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Test, TestingModule } from "@nestjs/testing" | ||
import { ListingsController } from "./listings.controller" | ||
import { PassportModule } from "@nestjs/passport" | ||
import { ListingsService } from "./listings.service" | ||
import { AuthzService } from "../auth/authz.service" | ||
import { CacheModule } from "@nestjs/common" | ||
|
||
// Cypress brings in Chai types for the global expect, but we want to use jest | ||
// expect here so we need to re-declare it. | ||
// see: https://github.com/cypress-io/cypress/issues/1319#issuecomment-593500345 | ||
declare const expect: jest.Expect | ||
|
||
describe("Listings Controller", () => { | ||
let controller: ListingsController | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
imports: [ | ||
PassportModule, | ||
CacheModule.register({ | ||
ttl: 60, | ||
max: 2, | ||
}), | ||
], | ||
providers: [ | ||
{ provide: AuthzService, useValue: {} }, | ||
{ provide: ListingsService, useValue: {} }, | ||
], | ||
controllers: [ListingsController], | ||
}).compile() | ||
|
||
controller = module.get<ListingsController>(ListingsController) | ||
}) | ||
|
||
it("should be defined", () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { Test, TestingModule } from "@nestjs/testing" | ||
import { ListingsService } from "./listings.service" | ||
import { getRepositoryToken } from "@nestjs/typeorm" | ||
import { Listing } from "./entities/listing.entity" | ||
|
||
// Cypress brings in Chai types for the global expect, but we want to use jest | ||
// expect here so we need to re-declare it. | ||
// see: https://github.com/cypress-io/cypress/issues/1319#issuecomment-593500345 | ||
declare const expect: jest.Expect | ||
|
||
let service: ListingsService | ||
const mockListings = [{ id: "asdf1" }, { id: "asdf2" }] | ||
const mockQueryBuilder = { | ||
leftJoinAndSelect: jest.fn().mockReturnThis(), | ||
orderBy: jest.fn().mockReturnThis(), | ||
andWhere: jest.fn().mockReturnThis(), | ||
getMany: jest.fn().mockReturnValue(mockListings), | ||
} | ||
const mockListingsRepo = { createQueryBuilder: jest.fn().mockReturnValue(mockQueryBuilder) } | ||
|
||
describe("ListingsService", () => { | ||
beforeEach(async () => { | ||
process.env.APP_SECRET = "SECRET" | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [ | ||
ListingsService, | ||
{ | ||
provide: getRepositoryToken(Listing), | ||
useValue: mockListingsRepo, | ||
}, | ||
], | ||
}).compile() | ||
|
||
service = module.get(ListingsService) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it("should be defined", () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
describe("getListingsList", () => { | ||
it("should not add a WHERE clause if no filters are applied", async () => { | ||
const listings = await service.list({}) | ||
|
||
expect(listings).toEqual(mockListings) | ||
expect(mockQueryBuilder.andWhere).toHaveBeenCalledTimes(0) | ||
}) | ||
|
||
it("should add a WHERE clause if the neighborhood filter is applied", async () => { | ||
const expectedNeighborhood = "Fox Creek" | ||
|
||
const listings = await service.list({ neighborhood: expectedNeighborhood }) | ||
|
||
expect(listings).toEqual(mockListings) | ||
expect(mockQueryBuilder.andWhere).toHaveBeenCalledWith( | ||
"property.neighborhood = :neighborhood", | ||
{ | ||
neighborhood: expectedNeighborhood, | ||
} | ||
) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the switch here and elsewhere to use this.repository instead of Listing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Listing
is using the entity object directly, butthis.repository
is the injected entity. That lets me swap it with a mock in the tests.