-
-
Notifications
You must be signed in to change notification settings - Fork 312
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 #2891 from sascha-karnatz/ingredient-update-turbo-…
…stream
- Loading branch information
Showing
6 changed files
with
96 additions
and
9 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,41 @@ | ||
import { reloadPreview } from "alchemy_admin/components/preview_window" | ||
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link" | ||
|
||
class Action extends HTMLElement { | ||
constructor() { | ||
super() | ||
|
||
// map action names with Javascript functions | ||
this.actions = { | ||
// add a intermediate closeCurrentDialog - action | ||
// this will be gone, if all dialogs are working with a promise and | ||
// we don't have to implicitly close the dialog | ||
closeCurrentDialog: Alchemy.closeCurrentDialog, | ||
reloadPreview, | ||
updateAnchorIcon: IngredientAnchorLink.updateIcon | ||
} | ||
} | ||
|
||
connectedCallback() { | ||
const func = this.actions[this.name] | ||
|
||
if (func) { | ||
func(...this.params) | ||
} else { | ||
console.error(`Unknown Alchemy action: ${this.name}`) | ||
} | ||
} | ||
|
||
get name() { | ||
return this.getAttribute("name") | ||
} | ||
|
||
get params() { | ||
if (this.hasAttribute("params")) { | ||
return JSON.parse(this.getAttribute("params")) | ||
} | ||
return [] | ||
} | ||
} | ||
|
||
customElements.define("alchemy-action", Action) |
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<alchemy-action name="closeCurrentDialog"></alchemy-action> | ||
<alchemy-action name="reloadPreview"></alchemy-action> | ||
<% if @ingredient.settings[:anchor] %> | ||
<alchemy-action name="updateAnchorIcon" params="<%= [@ingredient.id, @ingredient.dom_id.present?].to_json %>"></alchemy-action> | ||
<% end %> |
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,49 @@ | ||
import "alchemy_admin/components/action" | ||
import { renderComponent } from "./component.helper" | ||
import * as PreviewWindow from "alchemy_admin/components/preview_window" | ||
import IngredientAnchorLink from "alchemy_admin/ingredient_anchor_link" | ||
|
||
jest.mock("alchemy_admin/components/preview_window", () => { | ||
return { | ||
__esModule: true, | ||
reloadPreview: jest.fn() | ||
} | ||
}) | ||
|
||
jest.mock("alchemy_admin/ingredient_anchor_link", () => { | ||
return { | ||
__esModule: true, | ||
default: { | ||
updateIcon: jest.fn() | ||
} | ||
} | ||
}) | ||
|
||
describe("alchemy-action", () => { | ||
beforeEach(jest.clearAllMocks) | ||
|
||
it("call reloadPreview function", () => { | ||
renderComponent( | ||
"alchemy-action", | ||
`<alchemy-action name="reloadPreview"></alchemy-action>` | ||
) | ||
expect(PreviewWindow.reloadPreview).toBeCalled() | ||
}) | ||
|
||
it("call updateAnchorIcon function with parameter", () => { | ||
renderComponent( | ||
"alchemy-action", | ||
`<alchemy-action name="updateAnchorIcon" params="[123, true]"></alchemy-action>` | ||
) | ||
expect(IngredientAnchorLink.updateIcon).toBeCalledWith(123, true) | ||
}) | ||
|
||
it("call closeCurrentDialog function ", () => { | ||
window.Alchemy.closeCurrentDialog = jest.fn() | ||
renderComponent( | ||
"alchemy-action", | ||
`<alchemy-action name="closeCurrentDialog"></alchemy-action>` | ||
) | ||
expect(window.Alchemy.closeCurrentDialog).toBeCalled() | ||
}) | ||
}) |