Skip to content

Commit

Permalink
feat(comments): add new object (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
ecarreras authored Oct 9, 2024
1 parent d45a20f commit 755f09a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import Field from "./Field";

class Comments extends Field {
/**
* Height of the comments component
*/
_height: number | undefined = undefined;

get height(): number | undefined {
return this._height;
}

set height(value: number | undefined) {
this._height = value;
}

constructor(props: any) {
super(props);
if (props.height) {
const parsedHeight = parseInt(props.height);
this._height = isNaN(parsedHeight) ? undefined : parsedHeight;
}
}
}

export default Comments;
4 changes: 4 additions & 0 deletions src/WidgetFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import CodeEditor from "./CodeEditor";
import Avatar from "./Avatar";
import Time from "./Time";
import Alert from "./Alert";
import Comments from "./Comments";

class WidgetFactory {
/**
Expand Down Expand Up @@ -168,6 +169,9 @@ class WidgetFactory {
case "alert":
this._widgetClass = Alert;
break;
case "comments_timeline":
this._widgetClass = Comments;
break;

default:
break;
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import CodeEditor from "./CodeEditor";
import Time from "./Time";
import HTMLPreview from "./HTMLPreview";
import Alert from "./Alert";
import Comments from "./Comments";

import {
Graph,
Expand Down Expand Up @@ -134,4 +135,5 @@ export {
Alert,
YAxisOpts,
MinMaxValues,
Comments,
};
32 changes: 32 additions & 0 deletions src/spec/Comments.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Write specs for the Comments component here with vitest
import { describe, it, expect } from "vitest";
import Comments from "../Comments";

describe("Comments", () => {
it("sets height when provided in props", () => {
const comments = new Comments({ height: "150" });
expect(comments.height).toBe(150);
});

it("defaults height to undefined when not provided in props", () => {
const comments = new Comments({});
expect(comments.height).toBeUndefined();
});

it("sets height to undefined when provided height is not a number", () => {
const comments = new Comments({ height: "invalid" });
expect(comments.height).toBeUndefined();
});

it("updates height when set with a valid number", () => {
const comments = new Comments({});
comments.height = 200;
expect(comments.height).toBe(200);
});

it("updates height to undefined when set with an invalid value", () => {
const comments = new Comments({});
comments.height = undefined;
expect(comments.height).toBeUndefined();
});
});
8 changes: 8 additions & 0 deletions src/spec/WidgetFactory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ButtonGroup,
Time,
HTMLPreview,
Comments,
} from "..";

describe("A WidgetFactory", () => {
Expand Down Expand Up @@ -119,4 +120,11 @@ describe("A WidgetFactory", () => {
expect(widget).toBeInstanceOf(HTMLPreview);
expect(widget.type).toBe("html_preview");
});
it("should be albe to Comments type", () => {
const widgetFactory = new WidgetFactory();
const props = {};
const widget = widgetFactory.createWidget("comments_timeline", props);
expect(widget).toBeInstanceOf(Comments);
expect(widget.type).toBe("comments_timeline");
});
});

0 comments on commit 755f09a

Please sign in to comment.