From f60ef19e0dba4e06c04801ca5a7ce7eb1c1a24ce Mon Sep 17 00:00:00 2001 From: seveibar Date: Thu, 31 Oct 2024 10:49:59 -0700 Subject: [PATCH 1/2] introduce useChip, useResistor and useCapacitor --- lib/hooks/create-use-component.tsx | 10 ++++++---- lib/hooks/use-capacitor.tsx | 7 +++++++ lib/hooks/use-chip.tsx | 6 ++++++ lib/hooks/use-resistor.tsx | 7 +++++++ 4 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 lib/hooks/use-capacitor.tsx create mode 100644 lib/hooks/use-chip.tsx create mode 100644 lib/hooks/use-resistor.tsx diff --git a/lib/hooks/create-use-component.tsx b/lib/hooks/create-use-component.tsx index 04ad811..551b285 100644 --- a/lib/hooks/create-use-component.tsx +++ b/lib/hooks/create-use-component.tsx @@ -2,6 +2,11 @@ import React, { Component, type ComponentProps } from "react" import { z } from "zod" import { resistorProps, resistorPins } from "@tscircuit/props" +export type PinLabelSpec = + | readonly PinLabel[] + | readonly (readonly PinLabel[])[] + | { [key: string]: readonly PinLabel[] } + export type ComponentWithPins< Props, PinLabel extends string | never = never, @@ -21,10 +26,7 @@ export const createUseComponent = < PinLabel extends string | never = never, >( Component: React.ComponentType, - pins: - | readonly PinLabel[] - | readonly (readonly PinLabel[])[] - | { [key: string]: readonly PinLabel[] }, + pins: PinLabelSpec, ): ( | undefined = undefined>( name: string, props?: PropsFromHook, diff --git a/lib/hooks/use-capacitor.tsx b/lib/hooks/use-capacitor.tsx new file mode 100644 index 0000000..4306a0b --- /dev/null +++ b/lib/hooks/use-capacitor.tsx @@ -0,0 +1,7 @@ +import { capacitorPins, type CapacitorProps } from "@tscircuit/props" +import { createUseComponent } from "./create-use-component" + +export const useCapacitor = createUseComponent( + (props: CapacitorProps) => , + capacitorPins, +) diff --git a/lib/hooks/use-chip.tsx b/lib/hooks/use-chip.tsx new file mode 100644 index 0000000..6ebb505 --- /dev/null +++ b/lib/hooks/use-chip.tsx @@ -0,0 +1,6 @@ +import type { ChipProps } from "@tscircuit/props" +import { createUseComponent, type PinLabelSpec } from "./create-use-component" + +export const useChip = ( + pinLabels: PinLabelSpec, +) => createUseComponent((props: ChipProps) => , pinLabels) diff --git a/lib/hooks/use-resistor.tsx b/lib/hooks/use-resistor.tsx new file mode 100644 index 0000000..beed51c --- /dev/null +++ b/lib/hooks/use-resistor.tsx @@ -0,0 +1,7 @@ +import { resistorPins, type ResistorProps } from "@tscircuit/props" +import { createUseComponent } from "./create-use-component" + +export const useResistor = createUseComponent( + (props: ResistorProps) => , + resistorPins, +) From 1cbf4f6a933dfd795325f9f4c2d6d65f350a9f6b Mon Sep 17 00:00:00 2001 From: seveibar Date: Thu, 31 Oct 2024 10:57:53 -0700 Subject: [PATCH 2/2] fix useChip and introduce test for useChip, useResistor and useCapacitor --- bun.lockb | Bin 120861 -> 120861 bytes lib/hooks/use-chip.tsx | 8 +++-- tests/hooks/use-capacitor.test.tsx | 31 ++++++++++++++++++ tests/hooks/use-chip.test.tsx | 51 +++++++++++++++++++++++++++++ tests/hooks/use-resistor.test.tsx | 31 ++++++++++++++++++ 5 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 tests/hooks/use-capacitor.test.tsx create mode 100644 tests/hooks/use-chip.test.tsx create mode 100644 tests/hooks/use-resistor.test.tsx diff --git a/bun.lockb b/bun.lockb index 66394c004584e3e468c16101916ce0575eca8635..70f89093a424aabe461722523823af5ee7c8b51d 100755 GIT binary patch delta 39 vcmbQcfqm`<_J%Et2R+#t;|%qT^$ez;@MN6I!IYC&l3HBCuszj_aYruz790(~ delta 39 scmbQcfqm`<_J%Et2R+%D7{Flq2~WnU9E@=WdIoyt=G#-f7( - pinLabels: PinLabelSpec, -) => createUseComponent((props: ChipProps) => , pinLabels) + pinLabels: Record, +) => + createUseComponent( + (props: ChipProps) => , + pinLabels, + ) diff --git a/tests/hooks/use-capacitor.test.tsx b/tests/hooks/use-capacitor.test.tsx new file mode 100644 index 0000000..31811cc --- /dev/null +++ b/tests/hooks/use-capacitor.test.tsx @@ -0,0 +1,31 @@ +import { test, expect } from "bun:test" +import { useCapacitor } from "lib/hooks/use-capacitor" +import { Circuit } from "lib/Circuit" + +test("useCapacitor hook creates component with correct props and traces", () => { + const circuit = new Circuit() + + const C1 = useCapacitor("C1", { capacitance: "10uF", footprint: "0805" }) + const C2 = useCapacitor("C2", { capacitance: "100nF", footprint: "0603" }) + + circuit.add( + + + + , + ) + + circuit.render() + + // Check if capacitor components were created correctly + const capacitors = circuit.selectAll("capacitor") + expect(capacitors.length).toBe(2) + expect(capacitors[0].props.name).toBe("C1") + expect(capacitors[0].props.capacitance).toBe("10uF") + expect(capacitors[1].props.name).toBe("C2") + expect(capacitors[1].props.capacitance).toBe("100nF") + + // Check if traces were created correctly + const traces = circuit.selectAll("trace") + expect(traces.length).toBe(4) +}) diff --git a/tests/hooks/use-chip.test.tsx b/tests/hooks/use-chip.test.tsx new file mode 100644 index 0000000..50d6e0e --- /dev/null +++ b/tests/hooks/use-chip.test.tsx @@ -0,0 +1,51 @@ +import { test, expect } from "bun:test" +import { useChip } from "lib/hooks/use-chip" +import { Circuit } from "lib/Circuit" + +test("useChip hook creates component with correct props and traces", () => { + const circuit = new Circuit() + + const useAtmega = useChip({ + pin1: ["VCC"], + pin2: ["GND"], + pin3: ["TX"], + pin4: ["RX"], + } as const) + + const U1 = useAtmega("U1", { + footprint: "soic8", + manufacturerPartNumber: "ATMEGA328P", + }) + + circuit.add( + + + , + ) + + circuit.render() + + // Check if chip component was created correctly + const chip = circuit.selectOne("chip") + expect(chip).not.toBeNull() + expect(chip!.props.name).toBe("U1") + expect(chip!.props.footprint).toBe("soic8") + expect(chip!.props.manufacturerPartNumber).toBe("ATMEGA328P") + + // Check if traces were created correctly + const traces = circuit.selectAll("trace") + expect(traces.length).toBe(4) + + // Verify trace connections + const traceConnections = traces.map((t) => ({ + from: t.props.from, + to: t.props.to, + })) + expect(traceConnections).toContainEqual({ from: ".U1 > .VCC", to: "net.VCC" }) + expect(traceConnections).toContainEqual({ from: ".U1 > .GND", to: "net.GND" }) + expect(traceConnections).toContainEqual({ from: ".U1 > .TX", to: "net.TX" }) + expect(traceConnections).toContainEqual({ + from: ".U1 > .RX", + to: ".U1 > .TX", + }) +}) diff --git a/tests/hooks/use-resistor.test.tsx b/tests/hooks/use-resistor.test.tsx new file mode 100644 index 0000000..6568dbc --- /dev/null +++ b/tests/hooks/use-resistor.test.tsx @@ -0,0 +1,31 @@ +import { test, expect } from "bun:test" +import { useResistor } from "lib/hooks/use-resistor" +import { Circuit } from "lib/Circuit" + +test("useResistor hook creates component with correct props and traces", () => { + const circuit = new Circuit() + + const R1 = useResistor("R1", { resistance: "10k", footprint: "0402" }) + const R2 = useResistor("R2", { resistance: "20k", footprint: "0603" }) + + circuit.add( + + + + , + ) + + circuit.render() + + // Check if resistor components were created correctly + const resistors = circuit.selectAll("resistor") + expect(resistors.length).toBe(2) + expect(resistors[0].props.name).toBe("R1") + expect(resistors[0].props.resistance).toBe("10k") + expect(resistors[1].props.name).toBe("R2") + expect(resistors[1].props.resistance).toBe("20k") + + // Check if traces were created correctly + const traces = circuit.selectAll("trace") + expect(traces.length).toBe(4) +})