From 1cbf4f6a933dfd795325f9f4c2d6d65f350a9f6b Mon Sep 17 00:00:00 2001 From: seveibar Date: Thu, 31 Oct 2024 10:57:53 -0700 Subject: [PATCH] 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) +})