Skip to content

Commit

Permalink
sp-element-title: add unit tests / observe text attribute / tidy stor…
Browse files Browse the repository at this point in the history
…ybook
  • Loading branch information
ko-noguchi committed Oct 26, 2024
1 parent 70953c2 commit a1c609c
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 19 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions src/components/elementTitle/sp-element-title.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ export class SpElementTitle extends HTMLElement {
this.#headingElement.textContent = value;
}

static get observedAttributes() {
return ["text"];
}

constructor() {
super();
this.attachShadow({ mode: "open" });
Expand All @@ -32,6 +36,12 @@ export class SpElementTitle extends HTMLElement {
this.shadowRoot.appendChild(this.#createContainer());
}

attributeChangedCallback(name: string, oldValue: string, newValue: string) {
if (name === "text" && oldValue !== newValue) {
this.text = newValue;
}
}

#createContainer() {
const container = document.createElement("div");
container.classList.add("container");
Expand Down
24 changes: 7 additions & 17 deletions stories/elementTitle/sp-element-title.story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,19 @@ const meta: Meta = {
argTypes: {
text: { type: "string" },
},
args: {
text: "Element Title",
},
};
export default meta;

type Story = StoryObj;

export const Basic: Story = {
args: {
text: "Element Title",
},
};
export const Basic: Story = {};

export const WithTextLinks: Story = {
args: {
text: "Element Title",
},
render: (args) => html`
<sp-element-title .text=${args.text}>
<sp-element-title text=${args.text}>
<a
href="#"
slot="text-links"
Expand All @@ -43,23 +39,17 @@ export const WithTextLinks: Story = {
};

export const WithButtons: Story = {
args: {
text: "Element Title",
},
render: (args) => html`
<sp-element-title .text=${args.text}>
<sp-element-title text=${args.text}>
<sp-button text="Button1" slot="buttons"></sp-button>
<sp-button text="Button2" slot="buttons"></sp-button>
</sp-element-title>
`,
};

export const WithFullContents: Story = {
args: {
text: "Element Title",
},
render: (args) => html`
<sp-element-title .text=${args.text}>
<sp-element-title text=${args.text}>
<a
href="#"
slot="text-links"
Expand Down
37 changes: 37 additions & 0 deletions tests/elementTitle/sp-element-title.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { describe, expect, test } from "vitest";
import { getByShadowRole } from "shadow-dom-testing-library";
import { SpElementTitle } from "../../src/components/elementTitle/sp-element-title";
import "../../src/components/elementTitle/sp-element-title";

function getSpElementTitle() {
return document.querySelector("sp-element-title") as SpElementTitle;
}

function getHeadingElement() {
return getByShadowRole(document.body, "heading");
}

describe("sp-element-title", () => {
describe("text属性", () => {
test("text属性を設定すると、そのテキストが見出しとして表示される", async () => {
document.body.innerHTML =
"<sp-element-title text='サンプルタイトル'></sp-element-title>";

const headingElement = getHeadingElement();

expect(headingElement.textContent).toBe("サンプルタイトル");
});

test("text属性を更新すると、更新後のテキストが表示される", async () => {
document.body.innerHTML =
"<sp-element-title text='初期タイトル'></sp-element-title>";

const spElementTitle = getSpElementTitle();
const headingElement = getHeadingElement();

spElementTitle.setAttribute("text", "更新後タイトル");

expect(headingElement.textContent).toBe("更新後タイトル");
});
});
});

0 comments on commit a1c609c

Please sign in to comment.