Skip to content

Commit

Permalink
Add randomness to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pmeinhardt committed Nov 3, 2021
1 parent 2e91d6d commit 9cc0ed5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 45 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,7 @@ jobs:
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm run test
run: |
export SEED=$(openssl rand -hex 16)
echo $SEED
npm run test
94 changes: 50 additions & 44 deletions index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import type {
DocNode as ADFDoc,
PanelType as ADFPanelType,
} from "@atlaskit/adf-schema";
import Chance from "chance";
import { u } from "unist-builder";

import convert from ".";

const seed = process.env.SEED;
const random = seed ? new Chance(seed) : new Chance();

function doc(content: ADFDoc["content"]): ADFDoc {
return { version: 1, type: "doc", content };
}
Expand Down Expand Up @@ -42,41 +46,42 @@ it("converts simple documents", () => {
] as const
).forEach(([description, mark, type]) => {
it(`converts ${description}`, () => {
const text = random.word();

expect(
convert(
doc([
{
type: "paragraph",
content: [
{ type: "text", text: "This is " },
{ type: "text", text: description, marks: [{ type: mark }] },
{ type: "text", text: "formatted: " },
{ type: "text", marks: [{ type: mark }], text },
],
},
])
)
).toEqual(
u("root", [
u("paragraph", [
u("text", "This is "),
u(type, [u("text", description)]),
]),
u("paragraph", [u("text", "formatted: "), u(type, [u("text", text)])]),
])
);
});
});

it("converts strong emphasized text", () => {
const text = random.word();

expect(
convert(
doc([
{
type: "paragraph",
content: [
{ type: "text", text: "This is " },
{ type: "text", text: "strong & emphasized: " },
{
type: "text",
text: "strong & emphasized",
marks: [{ type: "em" }, { type: "strong" }],
text,
},
],
},
Expand All @@ -85,14 +90,16 @@ it("converts strong emphasized text", () => {
).toEqual(
u("root", [
u("paragraph", [
u("text", "This is "),
u("strong", [u("emphasis", [u("text", "strong & emphasized")])]),
u("text", "strong & emphasized: "),
u("strong", [u("emphasis", [u("text", text)])]),
]),
])
);
});

it("converts inline code", () => {
const text = random.hash();

expect(
convert(
doc([
Expand All @@ -102,17 +109,15 @@ it("converts inline code", () => {
{ type: "text", text: "This is " },
{
type: "text",
text: "code",
marks: [{ type: "code" }],
text,
},
],
},
])
)
).toEqual(
u("root", [
u("paragraph", [u("text", "This is "), u("inlineCode", "code")]),
])
u("root", [u("paragraph", [u("text", "This is "), u("inlineCode", text)])])
);
});

Expand Down Expand Up @@ -151,7 +156,8 @@ it("converts inline code", () => {
});

it("converts links", () => {
const url = "https://github.com/bitcrowd/mdast-util-from-adf";
const text = random.word();
const url = random.url();

expect(
convert(
Expand All @@ -161,33 +167,33 @@ it("converts links", () => {
content: [
{
type: "text",
text: "GitHub",
marks: [{ type: "link", attrs: { href: url } }],
text,
},
],
},
])
)
).toEqual(
u("root", [u("paragraph", [u("link", { url }, [u("text", "GitHub")])])])
u("root", [u("paragraph", [u("link", { url }, [u("text", text)])])])
);
});

[1, 2, 3, 4, 5, 6].forEach((level) => {
it(`converts headings (${level})`, () => {
const text = random.sentence({ words: 3 });

expect(
convert(
doc([
{
type: "heading",
attrs: { level },
content: [{ type: "text", text: "Heading" }],
content: [{ type: "text", text }],
},
])
)
).toEqual(
u("root", [u("heading", { depth: level }, [u("text", "Heading")])])
);
).toEqual(u("root", [u("heading", { depth: level }, [u("text", text)])]));
});
});

Expand Down Expand Up @@ -359,20 +365,18 @@ it("converts lists (decision)", () => {
});

it("converts block quotes", () => {
const quote = "I think; therefore I am.";
const text = random.sentence();

expect(
convert(
doc([
{
type: "blockquote",
content: [
{ type: "paragraph", content: [{ type: "text", text: quote }] },
],
content: [{ type: "paragraph", content: [{ type: "text", text }] }],
},
])
)
).toEqual(u("root", [u("blockquote", [u("paragraph", [u("text", quote)])])]));
).toEqual(u("root", [u("blockquote", [u("paragraph", [u("text", text)])])]));
});

it("converts dividers", () => {
Expand Down Expand Up @@ -423,6 +427,8 @@ it("converts emoji", () => {
});

it("converts mentions", () => {
const name = random.first();

expect(
convert(
doc([
Expand All @@ -433,15 +439,15 @@ it("converts mentions", () => {
type: "mention",
attrs: {
id: "557058:aafbc62f-3aa7-444a-8c18-42168d05183d",
text: "Alison",
text: name,
accessLevel: "",
},
},
],
},
])
)
).toEqual(u("root", [u("paragraph", [u("text", "@Alison")])]));
).toEqual(u("root", [u("paragraph", [u("text", `@${name}`)])]));
});

it("converts dates", () => {
Expand All @@ -466,6 +472,8 @@ it("converts dates", () => {
] as const
).forEach(([description, type]) => {
it(`converts ${description}`, () => {
const id = random.guid();

expect(
convert(
doc([
Expand All @@ -476,7 +484,7 @@ it("converts dates", () => {
{
type: "media",
attrs: {
id: "4b3fc7ea-3a65-4dda-8ae4-0c0576a6b9d3",
id,
type: "file",
collection: "",
width: 1632,
Expand All @@ -487,33 +495,27 @@ it("converts dates", () => {
},
])
)
).toEqual(
u("root", [
u("html", "<!-- media: file 4b3fc7ea-3a65-4dda-8ae4-0c0576a6b9d3 -->"),
])
);
).toEqual(u("root", [u("html", `<!-- media: file ${id} -->`)]));
});
});

it("converts cards (block)", () => {
const url = "https://github.com/bitcrowd/mdast-util-from-adf";
const url = random.url();

expect(
convert(
doc([
{
type: "blockCard",
attrs: {
url,
},
attrs: { url },
},
])
)
).toEqual(u("root", [u("paragraph", [u("text", url)])]));
});

it("converts cards (inline)", () => {
const url = "https://github.com/bitcrowd/mdast-util-from-adf";
const url = random.url();

expect(
convert(
Expand All @@ -528,7 +530,7 @@ it("converts cards (inline)", () => {
});

it("converts cards (embed)", () => {
const url = "https://github.com/bitcrowd/mdast-util-from-adf";
const url = random.url();

expect(
convert(
Expand All @@ -545,6 +547,8 @@ it("converts cards (embed)", () => {
(["info", "note", "success", "warning", "error"] as ADFPanelType[]).forEach(
(type) => {
it(`converts panels (${type})`, () => {
const text = random.string();

expect(
convert(
doc([
Expand All @@ -554,18 +558,20 @@ it("converts cards (embed)", () => {
content: [
{
type: "paragraph",
content: [{ type: "text", text: "Some panel text" }],
content: [{ type: "text", text }],
},
],
},
])
)
).toEqual(u("root", [u("paragraph", [u("text", "Some panel text")])]));
).toEqual(u("root", [u("paragraph", [u("text", text)])]));
});
}
);

it("converts layout containers", () => {
const content = random.string();

expect(
convert(
doc([
Expand All @@ -578,13 +584,13 @@ it("converts layout containers", () => {
content: [
{
type: "paragraph",
content: [{ type: "text", text: "Content" }],
content: [{ type: "text", text: content }],
},
],
},
],
},
])
)
).toEqual(u("root", [u("paragraph", [u("text", "Content")])]));
).toEqual(u("root", [u("paragraph", [u("text", content)])]));
});
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9cc0ed5

Please sign in to comment.