Skip to content

Commit

Permalink
tweak: formatting / credo
Browse files Browse the repository at this point in the history
  • Loading branch information
hannahpurcell committed Dec 19, 2024
1 parent 014d5df commit 2c9023f
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 49 deletions.
17 changes: 11 additions & 6 deletions assets/src/hooks/useDetours.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const subscribe = (
channel
.join()
.receive("ok", ({ data: data }: { data: SimpleDetour[] }) => {
const detoursMap = Object.fromEntries(data.map(v => [v.id, v]))
const detoursMap = Object.fromEntries(data.map((v) => [v.id, v]))
initializeChannel(detoursMap)
})

Expand Down Expand Up @@ -154,7 +154,7 @@ export const useDraftDetours = (socket: Socket | undefined) => {
channel = undefined
}
}
}, [socket])
}, [socket, topic])
return draftDetours
}

Expand Down Expand Up @@ -183,8 +183,11 @@ const subscribeByRoute = (
channel
.join()
.receive("ok", ({ data: data }: { data: SimpleDetour[] }) => {
const detoursMap = Object.fromEntries(data.map(v => [v.id, v]))
setDetours((detoursByRouteId) => ({...detoursByRouteId, [routeId]: detoursMap}))
const detoursMap = Object.fromEntries(data.map((v) => [v.id, v]))
setDetours((detoursByRouteId) => ({
...detoursByRouteId,
[routeId]: detoursMap,
}))
})

.receive("error", ({ reason }) => {
Expand All @@ -206,7 +209,9 @@ export const useActiveDetoursByRoute = (
routeIds: RouteId[]
): ByRouteId<DetoursMap> => {
const baseTopic = "detours:active:"
const [activeDetoursByRoute, setactiveDetoursByRoute] = useState<ByRouteId<DetoursMap>>({})
const [activeDetoursByRoute, setActiveDetoursByRoute] = useState<
ByRouteId<DetoursMap>
>({})
// eslint-disable-next-line react/hook-use-state
const [, setChannelsByRouteId] = useState<ByRouteId<Channel>>({})

Expand Down Expand Up @@ -235,7 +240,7 @@ export const useActiveDetoursByRoute = (
socket,
baseTopic,
routeId,
setactiveDetoursByRoute
setActiveDetoursByRoute
)
}
})
Expand Down
18 changes: 10 additions & 8 deletions assets/tests/factories/detourListFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export const detourListFactory = Factory.define<GroupedSimpleDetours>(() => {
}
})

export const simpleDetourFactory = Factory.define<SimpleDetour>(({ sequence }) => ({
id: sequence,
route: `${sequence}`,
direction: "Inbound",
name: `Headsign ${sequence}`,
intersection: `Street A${sequence} & Avenue B${sequence}`,
updatedAt: 1724866392,
}))
export const simpleDetourFactory = Factory.define<SimpleDetour>(
({ sequence }) => ({
id: sequence,
route: `${sequence}`,
direction: "Inbound",
name: `Headsign ${sequence}`,
intersection: `Street A${sequence} & Avenue B${sequence}`,
updatedAt: 1724866392,
})
)
95 changes: 61 additions & 34 deletions assets/tests/hooks/useDetours.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ 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 { useActiveDetours, useActiveDetoursByRoute, useDraftDetours, usePastDetours } from "../../src/hooks/useDetours"
import {
useActiveDetours,
useActiveDetoursByRoute,
useDraftDetours,
usePastDetours,
} from "../../src/hooks/useDetours"
import { SimpleDetour } from "../../src/models/detoursList"
import { RouteId } from "../../src/schedule"

Expand All @@ -16,23 +21,26 @@ const detours = [detourA, detourB, detourC]
describe("useActiveDetours", () => {
test("parses initial detours message from joining a channel", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })
mockSocket.channel.mockImplementation(() => mockChannel)
const { result } = renderHook(() => useActiveDetours(mockSocket))

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

test("parses an activated detour event", async () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })

let mockEvents: Record<string, undefined | ((data: {data: SimpleDetour}) => void)> = {
"activated": undefined
const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
> = {
activated: undefined,
}
mockChannel.on.mockImplementation((event, fn) => {
mockEvents[event] = fn
Expand All @@ -49,16 +57,19 @@ describe("useActiveDetours", () => {
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD
[detourD.id]: detourD,
})
})

test("parses a deactivated detour event", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })

let mockEvents: Record<string, undefined | ((data: {data: SimpleDetour}) => void)> = {
"deactivated": undefined
const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
> = {
deactivated: undefined,
}
mockChannel.on.mockImplementation((event, fn) => {
mockEvents[event] = fn
Expand All @@ -81,23 +92,26 @@ describe("useActiveDetours", () => {
describe("usePastDetours", () => {
test("parses initial detours message", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })
mockSocket.channel.mockImplementation(() => mockChannel)
const { result } = renderHook(() => usePastDetours(mockSocket))

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

test("parses a deactivated detour event", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })

let mockEvents: Record<string, undefined | ((data: {data: SimpleDetour}) => void)> = {
"deactivated": undefined
const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
> = {
deactivated: undefined,
}
mockChannel.on.mockImplementation((event, fn) => {
mockEvents[event] = fn
Expand All @@ -114,31 +128,34 @@ describe("usePastDetours", () => {
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD
[detourD.id]: detourD,
})
})
})

describe("useDraftDetours", () => {
test("parses initial detours message", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })
mockSocket.channel.mockImplementation(() => mockChannel)
const { result } = renderHook(() => useDraftDetours(mockSocket))

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

test("parses a drafted detour event", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })

let mockEvents: Record<string, undefined | ((data: {data: SimpleDetour}) => void)> = {
"drafted": undefined
const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
> = {
drafted: undefined,
}
mockChannel.on.mockImplementation((event, fn) => {
mockEvents[event] = fn
Expand All @@ -155,16 +172,19 @@ describe("useDraftDetours", () => {
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC,
[detourD.id]: detourD
[detourD.id]: detourD,
})
})

test("parses an activated detour event", () => {
const mockSocket = makeMockSocket()
const mockChannel = makeMockChannel("ok", {data: detours})
const mockChannel = makeMockChannel("ok", { data: detours })

let mockEvents: Record<string, undefined | ((data: {data: SimpleDetour}) => void)> = {
"activated": undefined
const mockEvents: Record<
string,
undefined | ((data: { data: SimpleDetour }) => void)
> = {
activated: undefined,
}
mockChannel.on.mockImplementation((event, fn) => {
mockEvents[event] = fn
Expand Down Expand Up @@ -192,7 +212,9 @@ describe("useActiveDetoursByRoute", () => {
const mockChannel = makeMockChannel()
mockSocket.channel.mockImplementation(() => mockChannel)

const { rerender } = renderHook(() => useActiveDetoursByRoute(mockSocket, ["1", "2"]))
const { rerender } = renderHook(() =>
useActiveDetoursByRoute(mockSocket, ["1", "2"])
)

// Needs to be kicked to do the effects again after the socket initializes
rerender()
Expand All @@ -209,7 +231,8 @@ describe("useActiveDetoursByRoute", () => {
mockSocket.channel.mockImplementation(() => mockChannel)

const { rerender } = renderHook(
(selectedRouteIds: RouteId[]) => useActiveDetoursByRoute(mockSocket, selectedRouteIds),
(selectedRouteIds: RouteId[]) =>
useActiveDetoursByRoute(mockSocket, selectedRouteIds),
{ initialProps: ["1", "2"] }
)
rerender(["1"]) // Deselect the route
Expand All @@ -228,13 +251,15 @@ describe("useActiveDetoursByRoute", () => {
return mockChannel
})

const { result } = renderHook(() => useActiveDetoursByRoute(mockSocket, ["1"]))
const { result } = renderHook(() =>
useActiveDetoursByRoute(mockSocket, ["1"])
)

expect(result.current).toEqual({
"1": {
[detourA.id]: detourA,
[detourB.id]: detourB,
[detourC.id]: detourC
[detourC.id]: detourC,
},
})
})
Expand All @@ -250,11 +275,13 @@ describe("useActiveDetoursByRoute", () => {
return 1
})

const { result } = renderHook(() => useActiveDetoursByRoute(mockSocket, ["1"]))
const { result } = renderHook(() =>
useActiveDetoursByRoute(mockSocket, ["1"])
)

expect(result.current).toEqual({
"1": {
[detourD.id]: detourD
[detourD.id]: detourD,
},
})
})
Expand Down
2 changes: 1 addition & 1 deletion test/skate_web/channels/detours_channel_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ defmodule SkateWeb.DetoursChannelTest do
{:ok, _, socket} =
subscribe_and_join(socket, DetoursChannel, "detours:draft:" <> Integer.to_string(user_id))

detour = :detour_snapshot |> build()
detour = build(:detour_snapshot)

assert {:noreply, _socket} =
DetoursChannel.handle_info(
Expand Down

0 comments on commit 2c9023f

Please sign in to comment.