test: add cases for fragments checking

This commit is contained in:
Théo LUDWIG 2024-01-12 00:33:05 +01:00
parent 68f35ddc0b
commit 0479652ffe
25 changed files with 183 additions and 98 deletions

View File

Before

Width:  |  Height:  |  Size: 95 B

After

Width:  |  Height:  |  Size: 95 B

View File

@ -0,0 +1,3 @@
# Awesome
<div id>Content</div>

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#)

View File

@ -0,0 +1,3 @@
# Invalid
![Image](../image.png#)

View File

@ -0,0 +1,3 @@
# Invalid
![Image](../image.png#non-existing-fragment)

View File

@ -0,0 +1,3 @@
# Awesome
<input name="name-should-be-ignored" id="id-after-name" />

View File

@ -0,0 +1,3 @@
# Invalid
[Invalid](./awesome.md#name-should-be-ignored)

View File

@ -0,0 +1,3 @@
# Awesome
<div data-id="not-an-id-should-be-ignored">Content</div>

View File

@ -0,0 +1,3 @@
# Invalid
[Invalid](./awesome.md#not-an-id-should-be-ignored)

View File

@ -0,0 +1,3 @@
# Awesome
<a name="existing-heading-anchor">Link</a>

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#non-existing-anchor-name-fragment)

View File

@ -0,0 +1,3 @@
# Awesome
<div id="existing-element-id-fragment">Content</div>

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#non-existing-element-id-fragment)

View File

@ -1,5 +1,3 @@
# Valid # Awesome
<a name="existing-heading-anchor" ></a>
## Existing Heading ## Existing Heading

View File

@ -1,5 +1,3 @@
# Invalid # Invalid
[Link fragment](./awesome.md#non-existing-heading) [Link fragment](./awesome.md#non-existing-heading)
[Link fragment](./awesome.md#non-existing-heading-anchor)

View File

@ -0,0 +1,3 @@
# Awesome
<a name="existing-heading-anchor">Link</a>

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#existing-heading-anchor)

View File

@ -0,0 +1,3 @@
# Awesome
<div id="existing-element-id-fragment">Content</div>

View File

@ -0,0 +1,3 @@
# Invalid
[Link fragment](./awesome.md#existing-element-id-fragment)

View File

@ -1,7 +1,5 @@
# Valid # Valid
<a name="existing-heading-anchor" ></a>
## Existing Heading ## Existing Heading
### Repeated Heading ### Repeated Heading

View File

@ -2,8 +2,6 @@
[Link fragment](./awesome.md#existing-heading) [Link fragment](./awesome.md#existing-heading)
[Link fragment](./awesome.md#existing-heading-anchor)
[Link fragment Repeated 0](./awesome.md#repeated-heading) [Link fragment Repeated 0](./awesome.md#repeated-heading)
[Link fragment Repeated 1](./awesome.md#repeated-heading-1) [Link fragment Repeated 1](./awesome.md#repeated-heading-1)

View File

@ -1,3 +1,3 @@
# Valid # Valid
![Image](./image.png) ![Image](../image.png)

View File

@ -0,0 +1,5 @@
# Valid
<div id="existing-element-id-fragment">Content</div>
[Link fragment](#non-existing-element-id-fragment)

View File

@ -23,96 +23,134 @@ const validateMarkdownLint = async (fixtureFile) => {
} }
test("ensure the rule validates correctly", async (t) => { test("ensure the rule validates correctly", async (t) => {
await t.test("should be valid", async (t) => {
await t.test("with an existing heading fragment", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("with an existing file", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-file.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("with an existing image", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/existing-image.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("should ignore absolute paths", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/ignore-absolute-paths.md",
)
assert.equal(lintResults?.length, 0)
})
await t.test("should ignore external links", async () => {
const lintResults = await validateMarkdownLint(
"test/fixtures/valid/ignore-external-links.md",
)
assert.equal(lintResults?.length, 0)
})
})
await t.test("should be invalid", async (t) => { await t.test("should be invalid", async (t) => {
await t.test("with a non-existing heading fragment", async () => { const testCases = [
const lintResults = await validateMarkdownLint( {
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md", name: "with an empty id fragment",
) fixturePath:
assert.equal(lintResults?.length, 2) "test/fixtures/invalid/empty-id-fragment/empty-id-fragment.md",
for (let i = 0; i < 2; i++) { error: '"./awesome.md#" should have a valid fragment identifier',
assert.deepEqual(lintResults?.[i]?.ruleNames, relativeLinksRule.names) },
{
name: "with a name fragment other than for an anchor",
fixturePath:
"test/fixtures/invalid/ignore-name-fragment-if-not-an-anchor/ignore-name-fragment-if-not-an-anchor.md",
error:
'"./awesome.md#name-should-be-ignored" should have a valid fragment identifier',
},
{
name: "with a non-existing id fragment (data-id !== id)",
fixturePath:
"test/fixtures/invalid/ignore-not-an-id-fragment/ignore-not-an-id-fragment.md",
error:
'"./awesome.md#not-an-id-should-be-ignored" should have a valid fragment identifier',
},
{
name: "with a non-existing anchor name fragment",
fixturePath:
"test/fixtures/invalid/non-existing-anchor-name-fragment/non-existing-anchor-name-fragment.md",
error:
'"./awesome.md#non-existing-anchor-name-fragment" should have a valid fragment identifier',
},
{
name: "with a non-existing element id fragment",
fixturePath:
"test/fixtures/invalid/non-existing-element-id-fragment/non-existing-element-id-fragment.md",
error:
'"./awesome.md#non-existing-element-id-fragment" should have a valid fragment identifier',
},
{
name: "with a non-existing heading fragment",
fixturePath:
"test/fixtures/invalid/non-existing-heading-fragment/non-existing-heading-fragment.md",
error:
'"./awesome.md#non-existing-heading" should have a valid fragment identifier',
},
{
name: "with a link to an image with a empty fragment",
fixturePath:
"test/fixtures/invalid/ignore-empty-fragment-checking-for-image.md",
error:
'"../image.png#" should not have a fragment identifier as it is an image',
},
{
name: "with a link to an image with a fragment",
fixturePath:
"test/fixtures/invalid/ignore-fragment-checking-for-image.md",
error:
'"../image.png#non-existing-fragment" should not have a fragment identifier as it is an image',
},
{
name: "with a non-existing file",
fixturePath: "test/fixtures/invalid/non-existing-file.md",
error: '"./index.test.js" should exist in the file system',
},
{
name: "with a non-existing image",
fixturePath: "test/fixtures/invalid/non-existing-image.md",
error: '"./image.png" should exist in the file system',
},
]
for (const { name, fixturePath, error } of testCases) {
await t.test(name, async () => {
const lintResults = await validateMarkdownLint(fixturePath)
assert.equal(lintResults?.length, 1)
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names)
assert.equal( assert.equal(
lintResults?.[i]?.ruleDescription, lintResults?.[0]?.ruleDescription,
relativeLinksRule.description, relativeLinksRule.description,
) )
} assert.equal(lintResults?.[0]?.errorDetail, error)
assert.equal( })
lintResults?.[0]?.errorDetail, }
'"./awesome.md#non-existing-heading" should have a valid fragment identifier', })
)
assert.equal(
lintResults?.[1]?.errorDetail,
'"./awesome.md#non-existing-heading-anchor" should have a valid fragment identifier',
)
})
await t.test("with a non-existing file", async () => { await t.test("should be valid", async (t) => {
const lintResults = await validateMarkdownLint( const testCases = [
"test/fixtures/invalid/non-existing-file.md", {
) name: "with an existing anchor name fragment",
assert.equal(lintResults?.length, 1) fixturePath:
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) "test/fixtures/valid/existing-anchor-name-fragment/existing-anchor-name-fragment.md",
assert.equal( },
lintResults?.[0]?.ruleDescription, {
relativeLinksRule.description, name: "with an existing element id fragment",
) fixturePath:
assert.equal( "test/fixtures/valid/existing-element-id-fragment/existing-element-id-fragment.md",
lintResults?.[0]?.errorDetail, },
'"./index.test.js" should exist in the file system', {
) name: "with an existing heading fragment",
}) fixturePath:
"test/fixtures/valid/existing-heading-fragment/existing-heading-fragment.md",
},
{
name: "with an existing file",
fixturePath: "test/fixtures/valid/existing-file.md",
},
{
name: "with an existing image",
fixturePath: "test/fixtures/valid/existing-image.md",
},
{
name: "should ignore absolute paths",
fixturePath: "test/fixtures/valid/ignore-absolute-paths.md",
},
{
name: "should ignore external links",
fixturePath: "test/fixtures/valid/ignore-external-links.md",
},
{
name: "should ignore checking fragment in own file",
fixturePath:
"test/fixtures/valid/ignore-fragment-checking-in-own-file.md",
},
]
await t.test("with a non-existing image", async () => { for (const { name, fixturePath } of testCases) {
const lintResults = await validateMarkdownLint( await t.test(name, async () => {
"test/fixtures/invalid/non-existing-image.md", const lintResults = await validateMarkdownLint(fixturePath)
) assert.equal(lintResults?.length, 0)
assert.equal(lintResults?.length, 1) })
assert.deepEqual(lintResults?.[0]?.ruleNames, relativeLinksRule.names) }
assert.equal(
lintResults?.[0]?.ruleDescription,
relativeLinksRule.description,
)
assert.equal(
lintResults?.[0]?.errorDetail,
'"./image.png" should exist in the file system',
)
})
}) })
}) })

View File

@ -38,14 +38,16 @@ test("utils", async (t) => {
await t.test("getMarkdownAnchorHTMLFragments", async () => { await t.test("getMarkdownAnchorHTMLFragments", async () => {
assert.deepStrictEqual( assert.deepStrictEqual(
getMarkdownAnchorHTMLFragments('<a name="anchorName" id="anchorId"></a>'), getMarkdownAnchorHTMLFragments(
'<a name="anchorName" id="anchorId">Link</a>',
),
["#anchorId"], ["#anchorId"],
) )
assert.deepStrictEqual( assert.deepStrictEqual(
getMarkdownAnchorHTMLFragments('<a name="anchorName"></a>'), getMarkdownAnchorHTMLFragments('<a name="anchorName">Link</a>'),
["#anchorName"], ["#anchorName"],
) )
assert.deepStrictEqual(getMarkdownAnchorHTMLFragments("<a></a>"), []) assert.deepStrictEqual(getMarkdownAnchorHTMLFragments("<a>Link</a>"), [])
assert.deepStrictEqual(getMarkdownAnchorHTMLFragments("<a>"), []) assert.deepStrictEqual(getMarkdownAnchorHTMLFragments("<a>"), [])
}) })
}) })