-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(cms-base): improve getOptionsFromNode helper (#542)
- Loading branch information
Showing
9 changed files
with
229 additions
and
149 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,5 @@ | ||
--- | ||
"@shopware-pwa/cms-base": patch | ||
--- | ||
|
||
Potential problems with CmsElementText rendering, when Node object is incorrect |
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
107 changes: 107 additions & 0 deletions
107
packages/cms-base/helpers/html-to-vue/getOptionsFromNode.test.ts
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,107 @@ | ||
import { getOptionsFromNode } from "./getOptionsFromNode"; | ||
import { describe, expect, it, vi, beforeEach } from "vitest"; | ||
|
||
describe("getOptionsFromNode", () => { | ||
const consoleErrorSpy = vi.spyOn(console, "error"); | ||
const urlResolverMock = vi.fn(); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
consoleErrorSpy.mockImplementation(() => {}); | ||
urlResolverMock.mockImplementation((url) => "resolved-url" + url); | ||
}); | ||
|
||
it("should return empty object if node is undefined", () => { | ||
const options = getOptionsFromNode(undefined, urlResolverMock); | ||
|
||
expect(options).toEqual({ | ||
attrs: {}, | ||
}); | ||
}); | ||
|
||
it("should return options object with style, classNames, and align", () => { | ||
const node = { | ||
attrs: { | ||
style: "color: red", | ||
class: "my-class", | ||
align: "center", | ||
}, | ||
}; | ||
|
||
const options = getOptionsFromNode(node, urlResolverMock); | ||
|
||
expect(options).toEqual({ | ||
style: "color: red", | ||
class: "my-class", | ||
align: "center", | ||
attrs: {}, | ||
}); | ||
}); | ||
|
||
it("should return options object without style, classNames, and align if they are not present in node.attrs", () => { | ||
const node = { | ||
attrs: {}, | ||
}; | ||
|
||
const options = getOptionsFromNode(node, urlResolverMock); | ||
|
||
expect(options).toEqual({ | ||
attrs: {}, | ||
}); | ||
}); | ||
|
||
it("should return empty object when no attrs in node", () => { | ||
const node = { | ||
attrs: undefined, | ||
}; | ||
|
||
const options = getOptionsFromNode(node, urlResolverMock); | ||
|
||
expect(options).toEqual({ | ||
attrs: {}, | ||
}); | ||
}); | ||
|
||
it("should resolve URL if attrs.href exists", () => { | ||
const node = { | ||
attrs: { | ||
href: "/path/to/page", | ||
}, | ||
}; | ||
|
||
const options = getOptionsFromNode(node, urlResolverMock); | ||
|
||
expect(options.attrs.href).toEqual("resolved-url/path/to/page"); | ||
}); | ||
|
||
it('should add additional attrs to "attrs" object', () => { | ||
const node = { | ||
attrs: { | ||
href: "/path/to/page", | ||
"data-test": "test", | ||
}, | ||
}; | ||
|
||
const options = getOptionsFromNode(node, urlResolverMock); | ||
|
||
expect(options.attrs.href).toEqual("resolved-url/path/to/page"); | ||
expect(options.attrs["data-test"]).toEqual("test"); | ||
}); | ||
|
||
it("should show console error if something went wrong", () => { | ||
const node = { | ||
attrs: { | ||
href: "/path/to/page", | ||
}, | ||
}; | ||
|
||
const consoleErrorSpy = vi.spyOn(console, "error"); | ||
|
||
const options = getOptionsFromNode(node, undefined as any); | ||
|
||
expect(options).toEqual({ | ||
attrs: {}, | ||
}); | ||
expect(consoleErrorSpy).toBeCalled(); | ||
}); | ||
}); |
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
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 @@ | ||
import { defineConfig } from "vitest/config"; | ||
import { resolve } from "path"; | ||
|
||
export default defineConfig({ | ||
test: { | ||
environment: "happy-dom", | ||
coverage: { | ||
enabled: true, | ||
}, | ||
alias: { | ||
"#imports": resolve(__dirname, "./composables.d.ts"), | ||
"#shopware": resolve(__dirname, "./types/api-types.d.ts"), | ||
}, | ||
}, | ||
}); |
Oops, something went wrong.
f8266a0
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.
Successfully deployed to the following URLs:
shopware-frontends-docs – ./apps/docs/
shopware-frontends-docs-shopware-frontends.vercel.app
shopware-frontends-docs-git-main-shopware-frontends.vercel.app
shopware-frontends-docs.vercel.app
frontends.shopware.com