-
-
Notifications
You must be signed in to change notification settings - Fork 748
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* improve unit tests setup, refactor and move some unit tests. Also add a callback for nodes * add pull request template * add unit tests for frame, improve handling of frame
- Loading branch information
1 parent
f61c311
commit 663958e
Showing
18 changed files
with
541 additions
and
247 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,15 @@ | ||
# New | ||
<!-- Provide a description of the changes you’ve made in this pr --> | ||
|
||
|
||
# Testing | ||
<!-- Please select all the testing methods that apply to this pr --> | ||
|
||
- [ ] Unit | ||
- [ ] Integration | ||
- [ ] Localhost | ||
|
||
|
||
# Screenshots | ||
<!-- If you have made client facing changes please provide screenshots --> | ||
|
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,3 +1,4 @@ | ||
beforeEach(() => { | ||
console.error = jest.fn(); | ||
}); | ||
const Enzyme = require("enzyme"); | ||
const Adapter = require("enzyme-adapter-react-16"); | ||
|
||
Enzyme.configure({ adapter: new Adapter() }); |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useEffect } from "react"; | ||
|
||
import { Options } from "../interfaces"; | ||
import { Events } from "../events"; | ||
|
||
import { useEditorStore } from "./store"; | ||
import { EditorContext } from "./EditorContext"; | ||
|
||
export const withDefaults = (options: Partial<Options> = {}) => ({ | ||
onStateChange: () => null, | ||
onRender: ({ render }) => render, | ||
resolver: {}, | ||
nodes: null, | ||
enabled: true, | ||
indicator: { | ||
error: "red", | ||
success: "rgb(98, 196, 98)" | ||
}, | ||
...options | ||
}); | ||
|
||
/** | ||
* A React Component that provides the Editor context | ||
*/ | ||
export const Editor: React.FC<Partial<Options>> = ({ | ||
children, | ||
...options | ||
}) => { | ||
const context = useEditorStore(withDefaults(options)); | ||
|
||
useEffect(() => { | ||
if (context && options) | ||
context.actions.setOptions(editorOptions => { | ||
editorOptions = options; | ||
}); | ||
}, [context, options]); | ||
|
||
const json = context && context.query.serialize(); | ||
const { onStateChange } = options; | ||
// because `useEffect` doesnt allow for deep comparison, we use this trick. | ||
// TODO: improve to actually use a deep comparison. | ||
useEffect(() => { | ||
onStateChange && json && onStateChange(JSON.parse(json)); | ||
}, [onStateChange, json]); | ||
|
||
return context ? ( | ||
<EditorContext.Provider value={context}> | ||
<Events>{children}</Events> | ||
</EditorContext.Provider> | ||
) : null; | ||
}; |
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,42 +1 @@ | ||
import React, { useEffect } from "react"; | ||
import { Options } from "../interfaces"; | ||
import { useEditorStore } from "../editor/store"; | ||
import { EditorContext } from "./EditorContext"; | ||
import { Events } from "../events"; | ||
|
||
export const createEditorStoreOptions = (options: Partial<Options> = {}) => { | ||
return { | ||
onRender: ({ render }) => render, | ||
resolver: {}, | ||
nodes: null, | ||
enabled: true, | ||
indicator: { | ||
error: "red", | ||
success: "rgb(98, 196, 98)" | ||
}, | ||
...options | ||
}; | ||
}; | ||
|
||
/** | ||
* A React Component that provides the Editor context | ||
*/ | ||
export const Editor: React.FC<Partial<Options>> = ({ | ||
children, | ||
...options | ||
}) => { | ||
const context = useEditorStore(createEditorStoreOptions(options)); | ||
|
||
useEffect(() => { | ||
if (context && options) | ||
context.actions.setOptions(editorOptions => { | ||
editorOptions = options; | ||
}); | ||
}, [context, options]); | ||
|
||
return context ? ( | ||
<EditorContext.Provider value={context}> | ||
<Events>{children}</Events> | ||
</EditorContext.Provider> | ||
) : null; | ||
}; | ||
export * from "./Editor"; |
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import React from "react"; | ||
import { shallow } from "enzyme"; | ||
import { act } from "react-dom/test-utils"; | ||
|
||
import { EditorContext } from "../EditorContext"; | ||
import { Editor } from "../editor"; | ||
import { Events } from "../../events"; | ||
import { useEditorStore } from "../store"; | ||
|
||
jest.mock("../store"); | ||
const mockStore = useEditorStore as jest.Mock<any>; | ||
|
||
describe("<Editor />", () => { | ||
const children = <h1>a children</h1>; | ||
let actions; | ||
let component; | ||
let query; | ||
let onStateChange; | ||
|
||
beforeEach(() => { | ||
React.useEffect = f => f(); | ||
|
||
query = { serialize: jest.fn().mockImplementation(() => "{}") }; | ||
onStateChange = jest.fn(); | ||
mockStore.mockImplementation(value => ({ ...value, query, actions })); | ||
act(() => { | ||
component = shallow( | ||
<Editor onStateChange={onStateChange}>{children}</Editor> | ||
); | ||
}); | ||
}); | ||
it("should render the children with events", () => { | ||
expect(component.contains(<Events>{children}</Events>)).toBe(true); | ||
}); | ||
it("should render the EditorContext.Provider", () => { | ||
expect(component.find(EditorContext.Provider)).toHaveLength(1); | ||
}); | ||
it("should have called serialize", () => { | ||
expect(query.serialize).toHaveBeenCalled(); | ||
}); | ||
}); |
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,50 @@ | ||
import React from "react"; | ||
import { useEditor } from "../useEditor"; | ||
|
||
import { useInternalEditor } from "../../editor/useInternalEditor"; | ||
|
||
jest.mock("../../editor/useInternalEditor"); | ||
const internalEditorMock = useInternalEditor as jest.Mock<any>; | ||
|
||
describe("useEditor", () => { | ||
const otherActions = { one: "one" }; | ||
const actions = { | ||
setDOM: "setDOM", | ||
setNodeEvent: "setNodeEvent", | ||
replaceNodes: "replaceNodes", | ||
reset: "reset", | ||
...otherActions | ||
}; | ||
const otherQueries = { another: "query" }; | ||
const query = { deserialize: "deserialize", ...otherQueries }; | ||
const state = { | ||
aRandomValue: "aRandomValue", | ||
connectors: "one", | ||
actions, | ||
query, | ||
store: {} | ||
}; | ||
let collect; | ||
let editor; | ||
|
||
beforeEach(() => { | ||
React.useMemo = f => f(); | ||
|
||
internalEditorMock.mockImplementation(() => state); | ||
collect = jest.fn(); | ||
editor = useEditor(collect); | ||
}); | ||
it("should have called internal state with collect", () => { | ||
expect(useInternalEditor).toHaveBeenCalledWith(collect); | ||
}); | ||
it("should return the correct editor", () => { | ||
expect(editor).toEqual( | ||
expect.objectContaining({ | ||
actions: { ...otherActions, selectNode: expect.any(Function) }, | ||
connectors: state.connectors, | ||
query: otherQueries, | ||
aRandomValue: state.aRandomValue | ||
}) | ||
); | ||
}); | ||
}); |
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,6 +1,6 @@ | ||
export * from "./nodes"; | ||
export * from "./render"; | ||
export * from "./interfaces"; | ||
export * from "./hooks"; | ||
export * from "./editor"; | ||
export * from "./events"; | ||
export * from "./hooks"; | ||
export * from "./interfaces"; | ||
export * from "./nodes"; | ||
export * from "./render"; |
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
Oops, something went wrong.