-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from oss-slu/24-jest-addnotescreen
24 jest addnotescreen
- Loading branch information
Showing
7 changed files
with
141 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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,37 @@ | ||
import Layout from "../layout"; | ||
import "@testing-library/jest-dom"; | ||
import { fireEvent, render, screen } from "@testing-library/react"; | ||
import NoteComponent from "../lib/components/noteComponent"; | ||
import { RichUtils } from 'draft-js'; | ||
|
||
jest.mock('draft-js', () => ({ | ||
...jest.requireActual('draft-js'), | ||
RichUtils: { | ||
...jest.requireActual('draft-js').RichUtils, | ||
toggleInlineStyle: jest.fn(jest.requireActual('draft-js').RichUtils.toggleInlineStyle), | ||
}, | ||
})); | ||
|
||
describe("NoteComponent Component", () => { | ||
|
||
it("renders the note component without crashing", () => { | ||
render(<NoteComponent />); | ||
expect(screen.getByText("Draft.js Testing Environment")).toBeInTheDocument(); | ||
}); | ||
|
||
describe("Button interactions", () => { | ||
it("toggles bold style when Bold button is clicked", () => { | ||
render(<NoteComponent />); | ||
const boldButton = screen.getByText("Bold"); | ||
fireEvent.click(boldButton); | ||
expect(RichUtils.toggleInlineStyle).toHaveBeenCalledWith(expect.anything(), 'BOLD'); | ||
}); | ||
|
||
it("toggles italic style when Italic button is clicked", () => { | ||
render(<NoteComponent />); | ||
const italicButton = screen.getByText("Italic"); | ||
fireEvent.click(italicButton); | ||
expect(RichUtils.toggleInlineStyle).toHaveBeenCalledWith(expect.anything(), 'ITALIC'); | ||
}); | ||
}); | ||
}); |
This file contains 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,90 @@ | ||
"use client"; | ||
import { Editor, EditorState, RichUtils } from "draft-js"; | ||
import "draft-js/dist/Draft.css"; | ||
import { useState, useEffect } from "react"; | ||
import { stateToHTML } from 'draft-js-export-html'; | ||
|
||
export default function NoteComponent() { | ||
const [editorState, setEditorState] = useState(EditorState.createEmpty()); | ||
const [plainText, setPlainText] = useState(""); | ||
const [rawHTML, setRawHTML] = useState(""); | ||
const [isClient, setIsClient] = useState(false); | ||
|
||
useEffect(() => { | ||
setIsClient(true); | ||
}, []); | ||
|
||
useEffect(() => { | ||
setPlainText(editorState.getCurrentContent().getPlainText()); | ||
}, [editorState]) | ||
|
||
useEffect(() => { | ||
// Updates the rawHTML component as the editorState changes | ||
const html = stateToHTML(editorState.getCurrentContent()); | ||
setRawHTML(html); | ||
}, [editorState]); | ||
|
||
const handleKeyCommand = (command: any) => { | ||
const newState = RichUtils.handleKeyCommand(editorState, command); | ||
if (newState) { | ||
setEditorState(newState); | ||
return "handled"; | ||
} | ||
return "not-handled"; | ||
}; | ||
|
||
const toggleBold = () => { | ||
setEditorState(RichUtils.toggleInlineStyle(editorState, "BOLD")); | ||
}; | ||
|
||
const toggleItalic = () => { | ||
setEditorState(RichUtils.toggleInlineStyle(editorState, "ITALIC")); | ||
}; | ||
|
||
const toggleUnderline = () => { | ||
setEditorState(RichUtils.toggleInlineStyle(editorState, "UNDERLINE")); | ||
}; | ||
|
||
return ( | ||
<main className="flex min-h-screen flex-col items-center justify-between p-24"> | ||
<div className=""> | ||
<h1 className="text-blue-500 text-xl">Draft.js Testing Environment</h1> | ||
<div className="border border-black p-4 rounded-lg"> | ||
<button onClick={toggleBold} className="border border-black p-2 m-1 rounded-md text-black"> | ||
Bold | ||
</button> | ||
<button onClick={toggleItalic} className="border border-black p-2 m-1 rounded-md text-black"> | ||
Italic | ||
</button> | ||
<button onClick={toggleUnderline} className="border border-black p-2 m-1 rounded-md text-black"> | ||
Underline | ||
</button> | ||
</div> | ||
<div style={editorStyles}> | ||
{isClient && ( | ||
<Editor | ||
editorState={editorState} | ||
onChange={setEditorState} | ||
handleKeyCommand={handleKeyCommand} | ||
editorKey="editor" | ||
placeholder="Start writing your notes here . . ." | ||
spellCheck={true} | ||
ariaLabel="Text editor" | ||
ariaMultiline={true} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
const editorStyles = { | ||
border: "1px solid black", | ||
padding: "10px", | ||
borderRadius: "4px", | ||
minHeight: "300px", | ||
width: "800px", | ||
color: "black", | ||
backgroundColor: "white", | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import '@testing-library/jest-dom/extend-expect'; | ||
import '@testing-library/jest-dom'; | ||
import fetchMock from 'jest-fetch-mock'; | ||
|
||
fetchMock.enableMocks(); |