-
Notifications
You must be signed in to change notification settings - Fork 1
chore: #3 add basic tests #20
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b7d5adb
chore: Add basic tests
KaiPrince a6ae91c
Add test setup functions
KaiPrince 9a4a3d9
Use setup helpers
KaiPrince 92b447e
Add Default options test group
KaiPrince f9b7aa1
Add test for trimParagraphTagsInMd: false
KaiPrince 76ca785
Override config path in tests
KaiPrince c2936f8
duplicate demo to test_fixtures folder
KaiPrince 52a1e52
rename folder to test-fixtures
KaiPrince 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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| const template = document.createElement('template'); | ||
|
|
||
| template.innerHTML = ` | ||
| <p>Hello from the greeting component!</p> | ||
| `; | ||
|
|
||
| export default class GreetingComponent extends HTMLElement { | ||
| constructor() { | ||
| super(); | ||
| this.attachShadow({ mode: 'open' }); | ||
| } | ||
|
|
||
| async connectedCallback() { | ||
| this.shadowRoot.appendChild(template.content.cloneNode(true)); | ||
| } | ||
| } | ||
|
|
||
| customElements.define('x-greeting', GreetingComponent); |
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 @@ | ||
| ## 11ty + WCC Demo | ||
|
|
||
| <x-greeting></x-greeting> |
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 |
|---|---|---|
| @@ -1,22 +1,80 @@ | ||
| import Eleventy from '@11ty/eleventy'; | ||
| import { wccPlugin } from '../src/index.js'; | ||
| import assert from 'node:assert/strict'; | ||
| import { describe, it } from 'node:test'; | ||
| import { describe, it, before } from 'node:test'; | ||
|
|
||
| describe('WCC plugin', () => { | ||
| it('removes wrapping p tags', async () => { | ||
| const elev = new Eleventy('demo', 'test/output', { | ||
| config: function (eleventyConfig) { | ||
| eleventyConfig.addPlugin(wccPlugin.configFunction); | ||
| } | ||
| describe('Default options', () => { | ||
| let indexMdFile; | ||
|
|
||
| before(async () => { | ||
| const templates = await setUpTemplates(); | ||
| ({ indexMdFile } = templates); | ||
| }); | ||
|
|
||
| it('prints the header', () => { | ||
| const expected = '<h2>11ty + WCC Demo</h2>'; | ||
|
|
||
| assert.ok(indexMdFile.content.includes(expected), 'Header not found'); | ||
| }); | ||
|
|
||
| it('prints the greeting component', () => { | ||
| const contentNormalized = indexMdFile.content | ||
| .replaceAll(/\s+/g, ' ') | ||
| .trim(); | ||
| const expected = | ||
| '<x-greeting><template shadowrootmode="open"> <p>Hello from the greeting component!</p> </template></x-greeting>'; | ||
|
|
||
| assert.ok( | ||
| contentNormalized.includes(expected), | ||
| 'x-greeting component not found' | ||
| ); | ||
| }); | ||
|
|
||
| it('removes wrapping p tags', () => { | ||
| const regexp = new RegExp('<p><x-greeting>(.|\\s)*</x-greeting></p>'); | ||
|
|
||
| assert.doesNotMatch(indexMdFile.content, regexp); | ||
| }); | ||
| }); | ||
|
|
||
| await elev.init(); | ||
|
|
||
| const elevOutput = await elev.toJSON(); | ||
| const result = elevOutput[0].content; | ||
| describe('trimParagraphTagsInMd: false', () => { | ||
| it('leaves wrapping p tags', async () => { | ||
| const eleventy = setUpEleventy({ trimParagraphTagsInMd: false }); | ||
| const { indexMdFile } = await setUpTemplates(eleventy); | ||
| const regexp = new RegExp('<p><x-greeting>(.|\\s)*</x-greeting></p>'); | ||
|
|
||
| const regexp = new RegExp('<p><x-greeting>(.|\\s)*</x-greeting></p>'); | ||
| assert.doesNotMatch(result, regexp); | ||
| assert.match(indexMdFile.content, regexp); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| function setUpEleventy(pluginOptions = {}) { | ||
| return new Eleventy('test-fixtures', 'test/output', { | ||
| configPath: null, | ||
| config: function (eleventyConfig) { | ||
| eleventyConfig.addPlugin(wccPlugin, { | ||
| definitions: [ | ||
| new URL('../test-fixtures/components/greeting.js', import.meta.url) | ||
| ], | ||
| ...pluginOptions | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| async function setUpTemplates(eleventy = null) { | ||
| eleventy ??= setUpEleventy(); | ||
|
|
||
| await eleventy.init(); | ||
| const elevOutput = await eleventy.toJSON(); | ||
|
|
||
| const indexMdFile = elevOutput.find( | ||
| (template) => template.inputPath === './test-fixtures/index.md' | ||
| ); | ||
|
|
||
| assert.ok(indexMdFile, 'Precondition failed: demo/index.md not found'); | ||
|
|
||
| return { indexMdFile }; | ||
| } | ||
|
|
||
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.
This prevents the project's root eleventy config file from overriding the test configs.