-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: call resolveData when new item inserted
- Loading branch information
Showing
5 changed files
with
224 additions
and
42 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
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,123 @@ | ||
import { cleanup } from "@testing-library/react"; | ||
import { AppState, Config, Data } from "../../types/Config"; | ||
import { PuckAction } from "../../reducer"; | ||
import { defaultAppState } from "../../components/Puck/context"; | ||
import { insertComponent } from "../insert-component"; | ||
import { rootDroppableId } from "../root-droppable-id"; | ||
|
||
const item1 = { type: "MyComponent", props: { id: "MyComponent-1" } }; | ||
const item2 = { type: "MyComponent", props: { id: "MyComponent-2" } }; | ||
const item3 = { type: "MyComponent", props: { id: "MyComponent-3" } }; | ||
|
||
const data: Data = { | ||
root: { props: { title: "" } }, | ||
content: [item1], | ||
zones: { | ||
"MyComponent-1:zone": [item2], | ||
"MyComponent-2:zone": [item3], | ||
}, | ||
}; | ||
|
||
const state: AppState = { | ||
data, | ||
ui: defaultAppState.ui, | ||
}; | ||
|
||
const config: Config = { | ||
components: { | ||
MyComponent: { | ||
defaultProps: { prop: "example" }, | ||
resolveData: ({ props }) => { | ||
return { | ||
props: { | ||
...props, | ||
prop: "Hello, world", | ||
}, | ||
readOnly: { | ||
prop: true, | ||
}, | ||
}; | ||
}, | ||
render: () => <div />, | ||
}, | ||
}, | ||
}; | ||
|
||
describe("use-insert-component", () => { | ||
describe("insert-component", () => { | ||
let dispatchedEvents: PuckAction[] = []; | ||
let resolvedDataEvents: AppState[] = []; | ||
|
||
const ctx = { | ||
config, | ||
dispatch: (action: PuckAction) => { | ||
dispatchedEvents.push(action); | ||
}, | ||
resolveData: (appState: AppState) => { | ||
resolvedDataEvents.push(appState); | ||
}, | ||
state, | ||
}; | ||
|
||
afterEach(() => { | ||
cleanup(); | ||
dispatchedEvents = []; | ||
resolvedDataEvents = []; | ||
}); | ||
|
||
it("should dispatch the insert action", async () => { | ||
insertComponent("MyComponent", rootDroppableId, 0, ctx); | ||
|
||
expect(dispatchedEvents[0]).toEqual<PuckAction>({ | ||
type: "insert", | ||
componentType: "MyComponent", | ||
destinationZone: rootDroppableId, | ||
destinationIndex: 0, | ||
id: expect.stringContaining("MyComponent-"), | ||
recordHistory: false, | ||
}); | ||
}); | ||
|
||
it("should dispatch the setUi action, and select the item", async () => { | ||
insertComponent("MyComponent", rootDroppableId, 0, ctx); | ||
|
||
expect(dispatchedEvents[1]).toEqual<PuckAction>({ | ||
type: "setUi", | ||
ui: { | ||
itemSelector: { | ||
zone: rootDroppableId, | ||
index: 0, | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
it("should run any resolveData methods on the inserted item", async () => { | ||
insertComponent("MyComponent", rootDroppableId, 0, ctx); | ||
|
||
expect(resolvedDataEvents[0]).toEqual<AppState>({ | ||
...state, | ||
data: { | ||
...state.data, | ||
content: [ | ||
{ | ||
type: "MyComponent", | ||
props: { | ||
id: expect.stringContaining("MyComponent-"), | ||
prop: "example", | ||
}, | ||
}, | ||
...state.data.content, | ||
], | ||
}, | ||
ui: { | ||
...state.ui, | ||
itemSelector: { | ||
index: 0, | ||
zone: rootDroppableId, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); | ||
}); |
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,54 @@ | ||
import { insertAction, InsertAction, PuckAction } from "../reducer"; | ||
import { AppState, Config } from "../types/Config"; | ||
import { generateId } from "./generate-id"; | ||
|
||
// Makes testing easier without mocks | ||
export const insertComponent = ( | ||
componentType: string, | ||
zone: string, | ||
index: number, | ||
{ | ||
config, | ||
dispatch, | ||
resolveData, | ||
state, | ||
}: { | ||
config: Config; | ||
dispatch: (action: PuckAction) => void; | ||
resolveData: (newAppState: AppState) => void; | ||
state: AppState; | ||
} | ||
) => { | ||
// Reuse newData so ID retains parity between dispatch and resolver | ||
const id = generateId(componentType); | ||
|
||
const insertActionData: InsertAction = { | ||
type: "insert", | ||
componentType, | ||
destinationIndex: index, | ||
destinationZone: zone, | ||
id, | ||
}; | ||
|
||
const insertedData = insertAction(state.data, insertActionData, config); | ||
|
||
// Dispatch the insert, immediately | ||
dispatch({ | ||
...insertActionData, // Dispatch insert rather set, as user's may rely on this via onAction | ||
recordHistory: false, | ||
}); | ||
|
||
const itemSelector = { | ||
index, | ||
zone, | ||
}; | ||
|
||
// Select the item, immediately | ||
dispatch({ type: "setUi", ui: { itemSelector } }); | ||
|
||
// Run any resolvers, async | ||
resolveData({ | ||
data: insertedData, | ||
ui: { ...state.ui, itemSelector }, | ||
}); | ||
}; |
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