Skip to content

Commit

Permalink
Add AmiChart overriding logic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pbn4 committed Aug 25, 2021
1 parent 090279a commit 548b94e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
5 changes: 5 additions & 0 deletions backend/core/src/listings/listings.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Listing } from "./entities/listing.entity"
import { ListingsQueryParams, ListingFilterParams } from "./dto/listing.dto"
import { Compare } from "../shared/dto/filter.dto"
import { TranslationsService } from "../translations/translations.service"
import { AmiChart } from "../ami-charts/entities/ami-chart.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.
Expand Down Expand Up @@ -102,6 +103,10 @@ describe("ListingsService", () => {
provide: getRepositoryToken(Listing),
useValue: mockListingsRepo,
},
{
provide: getRepositoryToken(AmiChart),
useValue: jest.fn(),
},
{
provide: TranslationsService,
useValue: { translateListing: jest.fn() },
Expand Down
2 changes: 1 addition & 1 deletion backend/core/src/listings/views/view.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe("listing views", () => {
view.getViewQb()

expect(mockQueryBuilder.select).toHaveBeenCalledTimes(1)
expect(mockQueryBuilder.leftJoin).toHaveBeenCalledTimes(7)
expect(mockQueryBuilder.leftJoin).toHaveBeenCalledTimes(8)
})

it("should map unitSummary to listings", () => {
Expand Down
49 changes: 49 additions & 0 deletions backend/core/src/shared/units-transformations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { AmiChart } from "../ami-charts/entities/ami-chart.entity"
import { UnitAmiChartOverride } from "../units/entities/unit-ami-chart-override.entity"
import { mergeAmiChartWithOverrides } from "./units-transformations"

describe("Unit Transformations", () => {
it("Ami chart items are correctly overwritten", () => {
let amiChart: AmiChart = {
id: "id",
createdAt: new Date(),
updatedAt: new Date(),
name: "name",
items: [
{
percentOfAmi: 1,
householdSize: 1,
income: 1,
},
{
percentOfAmi: 2,
householdSize: 2,
income: 2,
},
{
percentOfAmi: 3,
householdSize: 3,
income: 3,
},
],
}

const amiChartOverride: UnitAmiChartOverride = {
id: "id",
createdAt: new Date(),
updatedAt: new Date(),
items: [
{
percentOfAmi: 2,
householdSize: 2,
income: 20,
},
],
}
amiChart = mergeAmiChartWithOverrides(amiChart, amiChartOverride)
expect(amiChart.items.length).toBe(3)
expect(amiChart.items[0].income).toBe(1)
expect(amiChart.items[1].income).toBe(20)
expect(amiChart.items[2].income).toBe(3)
})
})
4 changes: 2 additions & 2 deletions backend/core/src/shared/units-transformations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ const getAmiChartItemUniqueKey = (amiChartItem: AmiChartItem) => {
return amiChartItem.householdSize.toString() + "-" + amiChartItem.percentOfAmi.toString()
}

const mergeAmiChartWithOverrides = (amiChart: AmiChart, override: UnitAmiChartOverride) => {
export const mergeAmiChartWithOverrides = (amiChart: AmiChart, override: UnitAmiChartOverride) => {
const householdAmiPercentageOverrideMap: Map<string, AmiChartItem> = override.items.reduce(
(acc, amiChartItem) => {
acc[getAmiChartItemUniqueKey(amiChartItem)] = amiChartItem
acc.set(getAmiChartItemUniqueKey(amiChartItem), amiChartItem)
return acc
},
new Map()
Expand Down
1 change: 0 additions & 1 deletion backend/core/test/listings/listings.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ describe("Listings", () => {
const am: ApplicationMethodCreateDto = {
type: ApplicationMethodType.FileDownload,
paperApplications: [{ id: paperApplication.body.id }],
listing: listing,
}

const applicationMethod = await supertest(app.getHttpServer())
Expand Down

0 comments on commit 548b94e

Please sign in to comment.