Skip to content

Commit

Permalink
[F] finish and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgoff committed Mar 14, 2023
1 parent 9e88473 commit c5cf5f6
Show file tree
Hide file tree
Showing 9 changed files with 349 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface AstroDataset {

export type AstroType = "supernova" | "galaxy" | "galaxyFilter";

export type AccessorKey = "ra" | "dec";

export interface AstroObject {
object_id: string;
id: AstroType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "supernova | galaxy | galaxyFilter",
},
category: "Display",
},
},
className: {
...className,
table: {
...className.table,
category: "Styling",
},
},
className,
isSelected: {
control: "boolean",
description: "Determines if the point has been selected and is visible",
table: {
type: {
summary: "boolean",
},
category: "Display",
},
},
isActive: {
Expand All @@ -31,6 +39,7 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "boolean",
},
category: "Display",
},
},
x: {
Expand All @@ -40,6 +49,7 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "number",
},
category: "Display",
},
},
y: {
Expand All @@ -49,6 +59,7 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "number",
},
category: "Display",
},
},
radius: {
Expand All @@ -59,6 +70,7 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "number",
},
category: "Display",
},
},
color: {
Expand All @@ -68,6 +80,7 @@ const meta: ComponentMeta<typeof Point> = {
type: {
summary: "string",
},
category: "Styling",
},
},
},
Expand All @@ -78,9 +91,8 @@ const meta: ComponentMeta<typeof Point> = {
viewBox="0 0 100 100"
style={{
display: "inline-block",
width: "auto",
height: "100%",
maxHeight: "200px",
width: "100%",
height: "auto",
}}
>
<Story />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { render, screen } from "@testing-library/react";
import Point from ".";

const props = {
x: 50,
y: 50,
radius: 10,
isSelected: false,
isActive: false,
color: "#000",
};

describe("Point", () => {
it(`should populate cx, cy, and r attributes`, () => {
render(
<svg>
<Point {...props} id="galaxy" />
</svg>
);

const point = screen.getByRole("listitem");

expect(point).toHaveAttribute("cx", props.x.toString());
expect(point).toHaveAttribute("cy", props.y.toString());
expect(point).toHaveAttribute("r", props.radius.toString());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ const Point: FunctionComponent<PointProps> = ({
r={isActive ? activeRadius : baseRadius}
fill="transparent"
stroke={getStroke(isActive, isSelected, color)}
tabIndex={0}
role="listitem"
{...{ isSelected, className }}
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import styled, { css, keyframes } from "styled-components";

const growRadius = (r: number) =>
keyframes`from {r: ${r}} to {r: ${Math.max(10, r * 1.2)}}`;
import styled, { css } from "styled-components";

export const Point = styled.circle<{
isSelected: boolean;
Expand All @@ -11,7 +8,7 @@ export const Point = styled.circle<{
${({ isSelected }) =>
isSelected &&
css`
stroke-width: 4;
stroke-width: 3;
`}
&:focus {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { className } from "@/storybook/utilities/argTypes";
import { ComponentMeta, ComponentStoryObj } from "@storybook/react";

import Points from ".";
import { AstroDataset } from "../GalaxySelector";
import { getLinearScale } from "../utilities";

const width = 600;
const height = 600;
const domain = [0, 1200];
const data: AstroDataset = {
name: "All Three Filters",
id: "ZTF19abvhduf",
color: "#fed828",
distance: 124.63102831794073,
dec: 56.07525,
velocity: 8573.901530035577,
ra: 97.139708,
redshift: 0.029,
objects: [
{
object_id: "01",
id: "galaxy",
ra: 120,
dec: 780,
color: "#fed828",
},
{
object_id: "02",
id: "galaxyFilter",
ra: 590,
dec: 630,
color: "#fed828",
},
{
object_id: "03",
id: "supernova",
ra: 990,
dec: 440,
color: "#fed828",
},
],
};

const meta: ComponentMeta<typeof Points> = {
argTypes: {
className: {
...className,
table: {
...className.table,
category: "Styling",
},
},
data: {
control: "object",
description: "Array of objects to be rendered as points.",
table: {
type: {
summary: "AstroObject[]",
},
defaultValue: {
summary: "[]",
},
category: "Model",
},
},
activeId: {
control: "select",
options: ["supernova", "galaxy", "galaxyFilter", "none"],
description:
"ID of the currently active point that will be given active styling",
table: {
type: {
summary: "supernova | galaxy | galaxyFilter",
},
category: "Model",
},
},
selectedData: {
control: "object",
description:
"ID of the currently selected point that will be visible to the user.",
table: {
type: {
summary: "AstroObject[]",
},
category: "Model",
},
},
xValueAccessor: {
type: {
name: "string",
required: true,
},
control: "select",
options: ["ra", "dec"],
description: "Key to access the x value of data points.",
table: {
type: {
summary: "ra | dec",
},
category: "Placement",
},
},
yValueAccessor: {
type: {
name: "string",
required: true,
},
control: "select",
options: ["ra", "dec"],
description: "Key to access the y value of data points.",
table: {
type: {
summary: "ra | dec",
},
category: "Placement",
},
},
xScale: {
type: {
name: "function",
required: true,
},
description:
"Scaling function to place points on the appropriate x value given the data range.",
table: {
type: {
summary: "(value: number) => number",
},
category: "Placement",
},
},
yScale: {
type: {
name: "function",
required: true,
},
description:
"Scaling function to place points on the appropriate y value given the data range.",
table: {
type: {
summary: "(value: number) => number",
},
category: "Placement",
},
},
color: {
control: "color",
description:
"Override color to be used in place of data points defined colors.",
table: {
type: {
summary: "string",
},
category: "Styling",
},
},
},
component: Points,
decorators: [
(Story) => (
<svg
viewBox={`0 0 ${width} ${height}`}
style={{
display: "inline-block",
width: "100%",
height: "auto",
}}
>
<Story />
</svg>
),
],
};
export default meta;

const xScale = getLinearScale(domain, [0, width]);
const yScale = getLinearScale(domain, [height, 0]);

export const Primary: ComponentStoryObj<typeof Points> = {
args: {
data: data.objects,
selectedData: [data.objects[0]],
xScale,
yScale,
xValueAccessor: "ra",
yValueAccessor: "dec",
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { render, screen } from "@testing-library/react";
import Points from ".";
import { AstroDataset } from "../GalaxySelector";
import { getLinearScale } from "../utilities";

const width = 600;
const height = 600;
const domain = [0, 1200];
const data: AstroDataset = {
name: "All Three Filters",
id: "ZTF19abvhduf",
color: "#fed828",
distance: 124.63102831794073,
dec: 56.07525,
velocity: 8573.901530035577,
ra: 97.139708,
redshift: 0.029,
objects: [
{
object_id: "01",
id: "galaxy",
ra: 120,
dec: 780,
color: "#fed828",
},
{
object_id: "02",
id: "galaxyFilter",
ra: 590,
dec: 630,
color: "#fed828",
},
{
object_id: "03",
id: "supernova",
ra: 990,
dec: 440,
color: "#fed828",
},
],
};

const xScale = getLinearScale(domain, [0, width]);
const yScale = getLinearScale(domain, [height, 0]);

const props = {
data: data.objects,
xScale,
yScale,
};

describe("Points", () => {
it(`should render a point for each object in the dataset`, () => {
render(
<svg>
<Points {...props} xValueAccessor="ra" yValueAccessor="dec" />
</svg>
);

const points = screen.getAllByRole("listitem");

expect(points.length).toBe(props.data.length);
});
});
Loading

0 comments on commit c5cf5f6

Please sign in to comment.