|
| 1 | +import { describe, expect, it, test } from "vitest"; |
| 2 | + |
| 3 | +import { findExistingBadges } from "./findExistingBadges.js"; |
| 4 | + |
| 5 | +describe("findExistingBadges", () => { |
| 6 | + describe("no result cases", () => { |
| 7 | + test.each([ |
| 8 | + "", |
| 9 | + "abc123", |
| 10 | + "# Test Title", |
| 11 | + "[]", |
| 12 | + "[][]", |
| 13 | + "[]()", |
| 14 | + "[][]()()", |
| 15 | + `<img />`, |
| 16 | + ])("%j", (input) => { |
| 17 | + expect(findExistingBadges(input)).toEqual([]); |
| 18 | + }); |
| 19 | + }); |
| 20 | + |
| 21 | + describe("single result cases", () => { |
| 22 | + test.each([ |
| 23 | + `[](https://github.com/JoshuaKGoldberg/console-fail-test/actions/workflows/compile.yml)`, |
| 24 | + `[](https://prettier.io)`, |
| 25 | + ``, |
| 26 | + `[](http://badge.fury.io/js/console-fail-test)`, |
| 27 | + `[](https://npmjs.org/package/console-fail-test)`, |
| 28 | + "<a>badge</a>", |
| 29 | + "<a >badge</a>", |
| 30 | + "<a \t>badge</a>", |
| 31 | + "<a href='abc'>badge</a>", |
| 32 | + ` <a href="#contributors" target="_blank"> |
| 33 | + <!-- prettier-ignore-start --> |
| 34 | + <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> |
| 35 | + <img alt="All Contributors: 1" src="https://img.shields.io/badge/all_contributors-1-21bb42.svg" /> |
| 36 | + <!-- ALL-CONTRIBUTORS-BADGE:END --> |
| 37 | + <!-- prettier-ignore-end --> |
| 38 | + </a>`, |
| 39 | + ` <a href="https://codecov.io/gh/JoshuaKGoldberg/all-contributors-auto-action" target="_blank"> |
| 40 | + <img alt="Codecov Test Coverage" src="https://codecov.io/gh/JoshuaKGoldberg/all-contributors-auto-action/branch/main/graph/badge.svg"/> |
| 41 | + </a>`, |
| 42 | + ` <a href="https://github.com/JoshuaKGoldberg/all-contributors-auto-action/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"> |
| 43 | + <img alt="Contributor Covenant" src="https://img.shields.io/badge/code_of_conduct-enforced-21bb42" /> |
| 44 | + </a>`, |
| 45 | + ` |
| 46 | + <a href="https://github.com/JoshuaKGoldberg/all-contributors-auto-action/blob/main/LICENSE.md" target="_blank"> |
| 47 | + <img alt="License: MIT" src="https://img.shields.io/github/license/JoshuaKGoldberg/all-contributors-auto-action?color=21bb42"> |
| 48 | + </a>`, |
| 49 | + ` |
| 50 | + <a href="https://github.com/sponsors/JoshuaKGoldberg" target="_blank"> |
| 51 | + <img alt="Sponsor: On GitHub" src="https://img.shields.io/badge/sponsor-on_github-21bb42.svg" /> |
| 52 | + </a>`, |
| 53 | + `<img alt="Style: Prettier" src="https://img.shields.io/badge/style-prettier-21bb42.svg" />`, |
| 54 | + `<img alt="TypeScript: Strict" src="https://img.shields.io/badge/typescript-strict-21bb42.svg" />`, |
| 55 | + ])("%s", (contents) => { |
| 56 | + expect(findExistingBadges(contents)).toEqual([contents.trim()]); |
| 57 | + }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("doesn't collect badges after a ##", () => { |
| 61 | + expect( |
| 62 | + findExistingBadges(` |
| 63 | + <img alt="test badge a" src="test-a.jpg" /> |
| 64 | + |
| 65 | + ## Usage |
| 66 | +
|
| 67 | + <img alt="test badge b" src="test-b.jpg" /> |
| 68 | + `), |
| 69 | + ).toEqual([`<img alt="test badge a" src="test-a.jpg" />`]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("doesn't collect badges after an h2", () => { |
| 73 | + expect( |
| 74 | + findExistingBadges(` |
| 75 | + <img alt="test badge a" src="test-a.jpg" /> |
| 76 | + |
| 77 | + <h2 align="left">Usage</h2> |
| 78 | +
|
| 79 | + <img alt="test badge b" src="test-b.jpg" /> |
| 80 | + `), |
| 81 | + ).toEqual([`<img alt="test badge a" src="test-a.jpg" />`]); |
| 82 | + }); |
| 83 | + |
| 84 | + test("real-world usage", () => { |
| 85 | + expect( |
| 86 | + findExistingBadges(` |
| 87 | +<p align="center"> |
| 88 | + <a href="#contributors" target="_blank"> |
| 89 | +<!-- prettier-ignore-start --> |
| 90 | +<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> |
| 91 | +<img alt="All Contributors: 1" src="https://img.shields.io/badge/all_contributors-1-21bb42.svg" /> |
| 92 | +<!-- ALL-CONTRIBUTORS-BADGE:END --> |
| 93 | +<!-- prettier-ignore-end --> |
| 94 | + </a> |
| 95 | + <a href="https://codecov.io/gh/JoshuaKGoldberg/all-contributors-auto-action" target="_blank"> |
| 96 | + <img alt="Codecov Test Coverage" src="https://codecov.io/gh/JoshuaKGoldberg/all-contributors-auto-action/branch/main/graph/badge.svg?token=eVIFY4MhfQ"/> |
| 97 | + </a> |
| 98 | + <a href="https://github.com/JoshuaKGoldberg/all-contributors-auto-action/blob/main/.github/CODE_OF_CONDUCT.md" target="_blank"> |
| 99 | + <img alt="Contributor Covenant" src="https://img.shields.io/badge/code_of_conduct-enforced-21bb42" /> |
| 100 | + </a> |
| 101 | + <a href="https://github.com/JoshuaKGoldberg/all-contributors-auto-action/blob/main/LICENSE.md" target="_blank"> |
| 102 | + <img alt="License: MIT" src="https://img.shields.io/github/license/JoshuaKGoldberg/all-contributors-auto-action?color=21bb42"> |
| 103 | + </a> |
| 104 | + <a href="https://github.com/sponsors/JoshuaKGoldberg" target="_blank"> |
| 105 | + <img alt="Sponsor: On GitHub" src="https://img.shields.io/badge/sponsor-on_github-21bb42.svg" /> |
| 106 | + </a> |
| 107 | + <img alt="Style: Prettier" src="https://img.shields.io/badge/style-prettier-21bb42.svg" /> |
| 108 | + <img alt="TypeScript: Strict" src="https://img.shields.io/badge/typescript-strict-21bb42.svg" /> |
| 109 | +</p> |
| 110 | + `), |
| 111 | + ).toMatchInlineSnapshot(` |
| 112 | + [ |
| 113 | + "<a href=\\"#contributors\\" target=\\"_blank\\"> |
| 114 | + <!-- prettier-ignore-start --> |
| 115 | + <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section --> |
| 116 | + <img alt=\\"All Contributors: 1\\" src=\\"https://img.shields.io/badge/all_contributors-1-21bb42.svg\\" /> |
| 117 | + <!-- ALL-CONTRIBUTORS-BADGE:END --> |
| 118 | + <!-- prettier-ignore-end --> |
| 119 | + </a>", |
| 120 | + "<a href=\\"https://github.com/JoshuaKGoldberg/all-contributors-auto-action/blob/main/LICENSE.md\\" target=\\"_blank\\"> |
| 121 | + <img alt=\\"License: MIT\\" src=\\"https://img.shields.io/github/license/JoshuaKGoldberg/all-contributors-auto-action?color=21bb42\\"> |
| 122 | + </a>", |
| 123 | + "<img alt=\\"Codecov Test Coverage\\" src=\\"https://codecov.io/gh/JoshuaKGoldberg/all-contributors-auto-action/branch/main/graph/badge.svg?token=eVIFY4MhfQ\\"/>", |
| 124 | + "<img alt=\\"Sponsor: On GitHub\\" src=\\"https://img.shields.io/badge/sponsor-on_github-21bb42.svg\\" />", |
| 125 | + "<img alt=\\"TypeScript: Strict\\" src=\\"https://img.shields.io/badge/typescript-strict-21bb42.svg\\" />", |
| 126 | + ] |
| 127 | + `); |
| 128 | + }); |
| 129 | +}); |
0 commit comments