-
Notifications
You must be signed in to change notification settings - Fork 64
test: add HTML-to-JSX tests #626
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ankit20012006
wants to merge
2
commits into
Bashamega:main
Choose a base branch
from
ankit20012006:html-jsx-tests
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import { htmlToJsx } from "html-to-jsx-transform"; | ||
|
|
||
| describe("htmlToJsx transformer", () => { | ||
| it("converts HTML attributes and nested content to JSX", () => { | ||
| const html = | ||
| '<section class="card"><h1>Title</h1><p id="summary">Summary</p></section>'; | ||
| const expected = | ||
| '<section className="card"><h1>Title</h1><p id="summary">Summary</p></section>'; | ||
|
|
||
| expect(htmlToJsx(html)).toBe(expected); | ||
| }); | ||
|
|
||
| it("handles boolean attributes and self-closing tags", () => { | ||
| expect(htmlToJsx('<input type="checkbox" checked>')).toBe( | ||
| '<input type="checkbox" checked={true} />', | ||
| ); | ||
| }); | ||
|
|
||
| it("preserves attribute values with mixed quotes and whitespace", () => { | ||
| const html = '<button disabled class="btn primary">Click</button>'; | ||
| expect(htmlToJsx(html)).toBe( | ||
| '<button disabled={true} className="btn primary">Click</button>', | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| import React from "react"; | ||
| import { render, screen, fireEvent, waitFor } from "@testing-library/react"; | ||
| import HTML_JSX from "@/app/convert/html-jsx/page"; | ||
|
|
||
| jest.mock("@monaco-editor/react", () => ({ | ||
| Editor: ({ language, value, onChange }) => ( | ||
| <textarea | ||
| data-testid={language === "html" ? "html-editor" : "jsx-editor"} | ||
| aria-label={language === "html" ? "HTML editor" : "JSX editor"} | ||
| value={value} | ||
| onChange={(event) => onChange?.(event.target.value)} | ||
| /> | ||
| ), | ||
| })); | ||
|
|
||
| jest.mock("@/components/navbar", () => ({ | ||
| NavBar: ({ title, isDarkMode, toggleTheme }) => ( | ||
| <div data-testid="mock-navbar"> | ||
| <h1>{title}</h1> | ||
| <button aria-label="Toggle dark mode" onClick={toggleTheme}> | ||
| Toggle Theme | ||
| </button> | ||
| <span>{isDarkMode ? "dark" : "light"}</span> | ||
| </div> | ||
| ), | ||
| })); | ||
|
|
||
| describe("HTML to JSX page", () => { | ||
| beforeEach(() => { | ||
| window.localStorage.clear(); | ||
| }); | ||
|
|
||
| it("renders the HTML and JSX panels", () => { | ||
| render(<HTML_JSX />); | ||
|
|
||
| expect(screen.getByText("HTML")).toBeInTheDocument(); | ||
| expect(screen.getByText("JSX")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("HTML editor")).toBeInTheDocument(); | ||
| expect(screen.getByLabelText("JSX editor")).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it("converts HTML input into JSX output", async () => { | ||
| render(<HTML_JSX />); | ||
|
|
||
| const htmlEditor = screen.getByLabelText("HTML editor"); | ||
| const jsxEditor = screen.getByLabelText("JSX editor"); | ||
|
|
||
| fireEvent.change(htmlEditor, { | ||
| target: { value: '<div class="foo">Hello</div>' }, | ||
| }); | ||
|
|
||
| await waitFor(() => { | ||
| expect(jsxEditor).toHaveValue( | ||
| 'function component() { return (<div className="foo">Hello</div>) }', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it("loads the saved theme from localStorage on mount", async () => { | ||
| window.localStorage.setItem("theme", JSON.stringify(true)); | ||
|
|
||
| render(<HTML_JSX />); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText("dark")).toBeInTheDocument(); | ||
| expect(screen.getByRole("main")).toHaveClass("bg-gray-900"); | ||
| }); | ||
| }); | ||
|
|
||
| it("toggles dark mode and saves the preference", async () => { | ||
| render(<HTML_JSX />); | ||
|
|
||
| const toggleButton = screen.getByRole("button", { | ||
| name: /toggle dark mode/i, | ||
| }); | ||
| fireEvent.click(toggleButton); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText("dark")).toBeInTheDocument(); | ||
| expect(window.localStorage.getItem("theme")).toBe("true"); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import React from "react"; | ||
| import { render, screen } from "@testing-library/react"; | ||
| import Preview from "@/app/resume-builder/Preview"; | ||
| import { defaultResumeData } from "@/app/resume-builder/defaultResumeData"; | ||
|
|
||
| describe("Preview", () => { | ||
| test("renders updated resume preview content from provided data", () => { | ||
| const sampleData = { | ||
| ...defaultResumeData, | ||
| name: "Jane Doe", | ||
| email: "jane@example.com", | ||
| phone: "1234567890", | ||
| workExperience: [ | ||
| { | ||
| title: "Software Engineer", | ||
| company: "Acme", | ||
| description: "Built apps.", | ||
| }, | ||
| ], | ||
| education: [ | ||
| { | ||
| degree: "B.Sc.", | ||
| institution: "University", | ||
| description: "Computer Science", | ||
| }, | ||
| ], | ||
| links: { | ||
| linkedIn: "https://linkedin.com/janedoe", | ||
| website: "https://janedoe.dev", | ||
| github: "https://github.com/janedoe", | ||
| }, | ||
| }; | ||
|
|
||
| render(<Preview isDarkMode={false} data={sampleData} />); | ||
|
|
||
| expect(screen.getByText("Jane Doe")).toBeInTheDocument(); | ||
| expect(screen.getAllByText("Software Engineer")[0]).toBeInTheDocument(); | ||
| expect(screen.getByText("Acme")).toBeInTheDocument(); | ||
| expect(screen.getByText("B.Sc.")).toBeInTheDocument(); | ||
| expect(screen.getByText("University")).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText((content) => content.includes("linkedin.com/janedoe")), | ||
| ).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByText((content) => content.includes("https://janedoe.dev")), | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import React from "react"; | ||
| import { render, screen, fireEvent } from "@testing-library/react"; | ||
| import ResumeForm from "@/app/resume-builder/ResumeForm"; | ||
| import { defaultResumeData } from "@/app/resume-builder/defaultResumeData"; | ||
|
|
||
| describe("ResumeForm", () => { | ||
| const onFormChange = jest.fn(); | ||
|
|
||
| beforeEach(() => { | ||
| onFormChange.mockClear(); | ||
| }); | ||
|
|
||
| test("renders core fields and updates parent state on text input change", () => { | ||
| render( | ||
| <ResumeForm | ||
| isDarkMode={false} | ||
| onFormChange={onFormChange} | ||
| initialData={defaultResumeData} | ||
| />, | ||
| ); | ||
|
|
||
| const nameInput = screen.getByPlaceholderText("John Doe"); | ||
| fireEvent.change(nameInput, { target: { value: "Jane Doe" } }); | ||
|
|
||
| expect(nameInput).toHaveValue("Jane Doe"); | ||
| expect(onFormChange).toHaveBeenCalledWith( | ||
| expect.objectContaining({ name: "Jane Doe" }), | ||
| ); | ||
| }); | ||
|
|
||
| test("updates links nested state and notifies parent", () => { | ||
| render( | ||
| <ResumeForm | ||
| isDarkMode={false} | ||
| onFormChange={onFormChange} | ||
| initialData={defaultResumeData} | ||
| />, | ||
| ); | ||
|
|
||
| const linkedinInput = screen.getByPlaceholderText( | ||
| "https://www.linkedin.com/in/johndev/", | ||
| ); | ||
| fireEvent.change(linkedinInput, { | ||
| target: { value: "https://linkedin.com/jane-doe" }, | ||
| }); | ||
|
|
||
| expect(linkedinInput).toHaveValue("https://linkedin.com/jane-doe"); | ||
| expect(onFormChange).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| links: expect.objectContaining({ | ||
| linkedIn: "https://linkedin.com/jane-doe", | ||
| }), | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| test("adds a new work experience row and triggers parent update", () => { | ||
| render( | ||
| <ResumeForm | ||
| isDarkMode={false} | ||
| onFormChange={onFormChange} | ||
| initialData={defaultResumeData} | ||
| />, | ||
| ); | ||
|
|
||
| const addButton = screen.getByRole("button", { | ||
| name: /Add Work Experience/i, | ||
| }); | ||
| fireEvent.click(addButton); | ||
|
|
||
| expect(onFormChange).toHaveBeenCalledWith( | ||
| expect.objectContaining({ | ||
| workExperience: expect.arrayContaining([ | ||
| expect.objectContaining({ title: "", company: "", description: "" }), | ||
| ]), | ||
| }), | ||
| ); | ||
| expect(onFormChange.mock.calls[0][0].workExperience).toHaveLength(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
Mapfor cache keys to avoid inherited-property collisions.At Line 27,
cacheRef.current[value]can read inherited object properties (e.g.,"toString"), returning non-JSX data and breaking conversion. This is user-input-driven and should be hardened.🔧 Proposed fix
Also applies to: 27-33
🤖 Prompt for AI Agents