Skip to content

Commit

Permalink
fix: Tests needed updated with detour parsing, but also caught a bug
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell committed Jan 6, 2025
1 parent 18605d4 commit 071dcb2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 52 deletions.
15 changes: 9 additions & 6 deletions assets/src/hooks/useDetours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ const subscribe = (
const channel = socket.channel(topic)

handleDrafted &&
channel.on("drafted", ({ data: data }) =>
channel.on("drafted", ({ data: data }: { data: SimpleDetourData }) =>
handleDrafted(simpleDetourFromData(data))
)
handleActivated &&
channel.on("activated", ({ data: data }) =>
channel.on("activated", ({ data: data }: { data: SimpleDetourData }) =>
handleActivated(simpleDetourFromData(data))
)
handleDeactivated &&
channel.on("deactivated", ({ data: data }) =>
channel.on("deactivated", ({ data: data }: { data: SimpleDetourData }) =>
handleDeactivated(simpleDetourFromData(data))
)
channel.on("auth_expired", reload)
Expand Down Expand Up @@ -178,13 +178,16 @@ const subscribeByRoute = (
): Channel => {
const channel = socket.channel(topic + routeId)

channel.on("activated", ({ data: data }) => {
channel.on("activated", ({ data: data }: { data: SimpleDetourData }) => {
setDetours((activeDetours) => ({
...activeDetours,
[routeId]: { ...activeDetours[routeId], [data.id]: data },
[routeId]: {
...activeDetours[routeId],
[data.id]: simpleDetourFromData(data),
},
}))
})
channel.on("deactivated", ({ data: data }) => {
channel.on("deactivated", ({ data: data }: { data: SimpleDetourData }) => {
setDetours((activeDetours) => {
delete activeDetours[routeId][data.id]
return activeDetours
Expand Down
19 changes: 13 additions & 6 deletions assets/tests/factories/detourListFactory.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,34 @@
import { Factory } from "fishery"
import {
GroupedSimpleDetours,
SimpleDetour,
SimpleDetourData,
simpleDetourFromData,
} from "../../src/models/detoursList"

export const detourListFactory = Factory.define<GroupedSimpleDetours>(() => {
return {
active: [
simpleDetourFactory.build(),
simpleDetourFactory.build({ direction: "Outbound" }),
simpleDetourFromData(simpleDetourDataFactory.build()),
simpleDetourFromData(
simpleDetourDataFactory.build({ direction: "Outbound" })
),
],
draft: undefined,
past: [simpleDetourFactory.build({ name: "Headsign Z" })],
past: [
simpleDetourFromData(
simpleDetourDataFactory.build({ name: "Headsign Z" })
),
],
}
})

export const simpleDetourFactory = Factory.define<SimpleDetour>(
export const simpleDetourDataFactory = Factory.define<SimpleDetourData>(
({ sequence }) => ({
id: sequence,
route: `${sequence}`,
direction: "Inbound",
name: `Headsign ${sequence}`,
intersection: `Street A${sequence} & Avenue B${sequence}`,
updatedAt: 1724866392,
updated_at: 1724866392,
})
)
88 changes: 48 additions & 40 deletions assets/tests/hooks/useDetours.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
import { describe, expect, test } from "@jest/globals"
import { makeMockChannel, makeMockSocket } from "../testHelpers/socketHelpers"
import { act, renderHook } from "@testing-library/react"
import { simpleDetourFactory } from "../factories/detourListFactory"
import { simpleDetourDataFactory } from "../factories/detourListFactory"
import {
useActiveDetours,
useActiveDetoursByRoute,
useDraftDetours,
usePastDetours,
} from "../../src/hooks/useDetours"
import { SimpleDetour } from "../../src/models/detoursList"
import {
SimpleDetourData,
simpleDetourFromData,
} from "../../src/models/detoursList"
import { RouteId } from "../../src/schedule"

const detourA = simpleDetourFactory.build()
const detourB = simpleDetourFactory.build()
const detourC = simpleDetourFactory.build()
const detourD = simpleDetourFactory.build()
const detourA = simpleDetourDataFactory.build()
const detourB = simpleDetourDataFactory.build()
const detourC = simpleDetourDataFactory.build()
const detourD = simpleDetourDataFactory.build()

const parsedDetourA = simpleDetourFromData(detourA)
const parsedDetourB = simpleDetourFromData(detourB)
const parsedDetourC = simpleDetourFromData(detourC)
const parsedDetourD = simpleDetourFromData(detourD)

const detours = [detourA, detourB, detourC]

Expand All @@ -26,9 +34,9 @@ describe("useActiveDetours", () => {
const { result } = renderHook(() => useActiveDetours(mockSocket))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
})
})

Expand All @@ -38,7 +46,7 @@ describe("useActiveDetours", () => {

const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
undefined | ((data: { data: SimpleDetourData }) => void)
> = {
activated: undefined,
}
Expand All @@ -54,10 +62,10 @@ describe("useActiveDetours", () => {
act(() => mockEvents["activated"]?.({ data: detourD }))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
[detourD.id]: parsedDetourD,
})
})

Expand All @@ -67,7 +75,7 @@ describe("useActiveDetours", () => {

const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
undefined | ((data: { data: SimpleDetourData }) => void)
> = {
deactivated: undefined,
}
Expand All @@ -83,8 +91,8 @@ describe("useActiveDetours", () => {
act(() => mockEvents["deactivated"]?.({ data: detourA }))

expect(result.current).toStrictEqual({
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
})
})
})
Expand All @@ -97,9 +105,9 @@ describe("usePastDetours", () => {
const { result } = renderHook(() => usePastDetours(mockSocket))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
})
})

Expand All @@ -109,7 +117,7 @@ describe("usePastDetours", () => {

const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
undefined | ((data: { data: SimpleDetourData }) => void)
> = {
deactivated: undefined,
}
Expand All @@ -125,10 +133,10 @@ describe("usePastDetours", () => {
act(() => mockEvents["deactivated"]?.({ data: detourD }))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
[detourD.id]: parsedDetourD,
})
})
})
Expand All @@ -141,9 +149,9 @@ describe("useDraftDetours", () => {
const { result } = renderHook(() => useDraftDetours(mockSocket))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
})
})

Expand All @@ -153,7 +161,7 @@ describe("useDraftDetours", () => {

const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
undefined | ((data: { data: SimpleDetourData }) => void)
> = {
drafted: undefined,
}
Expand All @@ -169,10 +177,10 @@ describe("useDraftDetours", () => {
act(() => mockEvents["drafted"]?.({ data: detourD }))

expect(result.current).toStrictEqual({
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
[detourD.id]: parsedDetourD,
})
})

Expand All @@ -182,7 +190,7 @@ describe("useDraftDetours", () => {

const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
undefined | ((data: { data: SimpleDetourData }) => void)
> = {
activated: undefined,
}
Expand All @@ -198,8 +206,8 @@ describe("useDraftDetours", () => {
act(() => mockEvents["activated"]?.({ data: detourA }))

expect(result.current).toStrictEqual({
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
})
})
})
Expand Down Expand Up @@ -257,9 +265,9 @@ describe("useActiveDetoursByRoute", () => {

expect(result.current).toEqual({
"1": {
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourA.id]: parsedDetourA,
[detourB.id]: parsedDetourB,
[detourC.id]: parsedDetourC,
},
})
})
Expand All @@ -281,7 +289,7 @@ describe("useActiveDetoursByRoute", () => {

expect(result.current).toEqual({
"1": {
[detourD.id]: detourD,
[detourD.id]: parsedDetourD,
},
})
})
Expand Down

0 comments on commit 071dcb2

Please sign in to comment.