-
Notifications
You must be signed in to change notification settings - Fork 62
fix: block library missing title for grouped blocks #976
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+192
−3
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -135,9 +135,14 @@ function getLibraryMetadata(el) { | |
|
|
||
| function transformBlock(block) { | ||
| const prevSib = block.previousElementSibling; | ||
| const item = isHeading(prevSib) && prevSib.textContent | ||
| ? { name: prevSib.textContent } | ||
| : getBlockName(block.className || ''); | ||
| let item; | ||
| if (block.dataset.groupheading) { | ||
| item = { name: block.dataset.groupheading }; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this data-groupheading (one word) or data-group-heading? the dataset key needs to match exactly.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct: |
||
| } else if (isHeading(prevSib) && prevSib.textContent) { | ||
| item = { name: prevSib.textContent }; | ||
| } else { | ||
| item = getBlockName(block.className || ''); | ||
| } | ||
| item.dom = block.dataset?.isgroup ? processGroupBlock(block) : getBlockTableHtml(block); | ||
|
|
||
| const metaEl = block.nextElementSibling?.classList.contains('library-metadata') | ||
|
|
||
This file contains hidden or 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,3 @@ | ||
| // Mock NX /utils/daConfig.js utils for tests | ||
| export const fetchDaConfigs = async () => ({}); | ||
| export const getFirstSheet = () => null; |
181 changes: 181 additions & 0 deletions
181
test/unit/blocks/canvas/ew-panel-extensions/helpers.test.js
This file contains hidden or 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,181 @@ | ||
| import { expect } from '@esm-bundle/chai'; | ||
| import { setNx } from '../../../../../scripts/utils.js'; | ||
|
|
||
| setNx('/test/fixtures/nx', { hostname: 'example.com' }); | ||
|
|
||
| let getBlockVariants; | ||
|
|
||
| before(async () => { | ||
| const mod = await import('../../../../../blocks/canvas/ew-panel-extensions/helpers.js'); | ||
| getBlockVariants = mod.getBlockVariants; | ||
| }); | ||
|
|
||
| describe('EW panel helpers transformBlock', () => { | ||
| let savedFetch; | ||
| beforeEach(() => { savedFetch = window.fetch; }); | ||
| afterEach(() => { window.fetch = savedFetch; }); | ||
|
|
||
| function mockHtml(html) { | ||
| window.fetch = () => Promise.resolve(new Response(html, { status: 200 })); | ||
| } | ||
|
|
||
| it('Uses data-groupheading as the name for grouped blocks', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <h2>My Group</h2> | ||
| <div class="library-container-start"></div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-container-end"></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants).to.have.lengthOf(1); | ||
| expect(variants[0].name).to.equal('My Group'); | ||
| }); | ||
|
|
||
| it('Falls back to the preceding heading text when no groupheading', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <h2>Block Title</h2> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants).to.have.lengthOf(1); | ||
| expect(variants[0].name).to.equal('Block Title'); | ||
| }); | ||
|
|
||
| it('Falls back to class name when there is no groupheading and no preceding heading', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero wide"><div><div>content</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants).to.have.lengthOf(1); | ||
| expect(variants[0].name).to.equal('hero'); | ||
| expect(variants[0].variants).to.equal('wide'); | ||
| }); | ||
|
|
||
| it('Returns an empty array when the fetch fails', async () => { | ||
| window.fetch = () => Promise.resolve(new Response('error', { status: 500 })); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants).to.deep.equal([]); | ||
| }); | ||
|
|
||
| it('Returns a table as item.dom for a regular block', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].dom).to.be.instanceOf(window.HTMLTableElement); | ||
| }); | ||
|
|
||
| it('Returns a div as item.dom for a grouped block', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <h2>My Group</h2> | ||
| <div class="library-container-start"></div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-container-end"></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].dom).to.be.instanceOf(window.HTMLDivElement); | ||
| }); | ||
|
|
||
| it('Sets item.tags from searchtags in nextElementSibling library-metadata', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-metadata"><div><div>searchtags</div><div>hero, card</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].tags).to.equal('hero, card'); | ||
| }); | ||
|
|
||
| it('Sets item.description from description in nextElementSibling library-metadata', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-metadata"><div><div>description</div><div>A hero block</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].description).to.equal('A hero block'); | ||
| }); | ||
|
|
||
| it('Sets item.tags from searchtags in embedded library-metadata', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"> | ||
| <div><div>content</div></div> | ||
| <div class="library-metadata"><div><div>searchtags</div><div>hero, banner</div></div></div> | ||
| </div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].tags).to.equal('hero, banner'); | ||
| }); | ||
|
|
||
| it('Sets both tags and description when both present in library-metadata', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-metadata"> | ||
| <div><div>searchtags</div><div>hero, card</div></div> | ||
| <div><div>description</div><div>A hero block</div></div> | ||
| </div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].tags).to.equal('hero, card'); | ||
| expect(variants[0].description).to.equal('A hero block'); | ||
| }); | ||
|
|
||
| it('Does not set tags or description when no library-metadata is present', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].tags).to.be.undefined; | ||
| expect(variants[0].description).to.be.undefined; | ||
| }); | ||
|
|
||
| it('Sets tags from library-metadata appended after library-container-end in a group', async () => { | ||
| mockHtml(` | ||
| <body><div> | ||
| <h2>My Group</h2> | ||
| <div class="library-container-start"></div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <div class="library-container-end"></div> | ||
| <div class="library-metadata"><div><div>searchtags</div><div>group, hero</div></div></div> | ||
| </div></body> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].tags).to.equal('group, hero'); | ||
| }); | ||
|
|
||
| it('Group dom contains both a table for the block and cloned non-div siblings', async () => { | ||
| mockHtml(` | ||
| <main><div> | ||
| <h2>Hero with text</h2> | ||
| <div class="library-container-start"><div><div></div></div></div> | ||
| <div class="hero"><div><div>content</div></div></div> | ||
| <p>Lorem ipsum</p> | ||
| <div class="library-container-end"><div><div></div></div></div> | ||
| </div></main> | ||
| `); | ||
| const variants = await getBlockVariants('/mock-path'); | ||
| expect(variants[0].name).to.equal('Hero with text'); | ||
| const { dom } = variants[0]; | ||
| expect(dom).to.be.instanceOf(window.HTMLDivElement); | ||
| expect(dom.querySelector('table')).to.not.be.null; | ||
| expect(dom.querySelector('p')).to.not.be.null; | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
maybe worth adding a test for the three branches in transformBlock (groupheading attr, heading sibling, fallback to getBlockName). without it the same thing can regress silently.
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.
Yes I agree, added tests