Skip to content

Commit

Permalink
Merge pull request #243 from tscircuit/use-led
Browse files Browse the repository at this point in the history
Add useLed hook
  • Loading branch information
seveibar authored Nov 7, 2024
2 parents 6118ee5 + 59e84c2 commit 0701423
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/hooks/use-led.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ledPins, type LedProps } from "@tscircuit/props"
import { createUseComponent } from "./create-use-component"

export const useLed = createUseComponent(
(props: LedProps) => <led {...props} />,
ledPins,
)
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from "./hooks/create-use-component"
export * from "./hooks/use-capacitor"
export * from "./hooks/use-chip"
export * from "./hooks/use-diode"
export * from "./hooks/use-led"
export * from "./hooks/use-resistor"

import "./register-catalogue"
Expand Down
31 changes: 31 additions & 0 deletions tests/hooks/use-led.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { test, expect } from "bun:test"
import { useLed } from "lib/hooks/use-led"
import { Circuit } from "lib/Circuit"

test("useLed hook creates component with correct props and traces", () => {
const circuit = new Circuit()

const LED1 = useLed("LED1", { footprint: "1206" })
const LED2 = useLed("LED2", { footprint: "0603" })

circuit.add(
<board width="10mm" height="10mm">
<LED1 anode="net.VCC" cathode="net.GND" />
<LED2 pos={LED1.anode} neg="net.GND" />
</board>,
)

circuit.render()

// Check if LED components were created correctly
const leds = circuit.selectAll("led")
expect(leds.length).toBe(2)
expect(leds[0].props.name).toBe("LED1")
expect(leds[0].props.footprint).toBe("1206")
expect(leds[1].props.name).toBe("LED2")
expect(leds[1].props.footprint).toBe("0603")

// Check if traces were created correctly
const traces = circuit.selectAll("trace")
expect(traces.length).toBe(4)
})

0 comments on commit 0701423

Please sign in to comment.