Skip to content

Commit

Permalink
fix AggregateError on renderer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edno committed Aug 28, 2021
1 parent ef26a0f commit 390de07
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
25 changes: 16 additions & 9 deletions src/lib/renderer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const fs = require("fs-extra");
const fs = require("fs").promises;
const path = require("path");

const fsExtra = require("fs-extra");
const moment = require("moment");

const { toSlug, startCase, pathUrl } = require("./utils");
const { prettifyJavascript } = require("./prettier");

Expand All @@ -16,7 +19,7 @@ module.exports = class Renderer {
}

emptyOutputDir() {
fs.emptyDirSync(this.outputDir);
fsExtra.emptyDirSync(this.outputDir);
}

async renderRootTypes(name, type) {
Expand All @@ -32,10 +35,10 @@ module.exports = class Renderer {
}, {});
}

await fs.ensureDir(dirPath);
await fsExtra.ensureDir(dirPath);

const filePath = path.join(dirPath, "_category_.yml");
await fs.outputFile(filePath, `label: '${startCase(name)}'\n`, "utf8");
await fsExtra.outputFile(filePath, `label: '${startCase(name)}'\n`, "utf8");

pages = await Promise.all(
Object.keys(type).map(async (name) => {
Expand All @@ -51,7 +54,7 @@ module.exports = class Renderer {
const fileName = toSlug(name);
const filePath = path.join(path.normalize(dirPath), `${fileName}.mdx`);
const content = this.printer.printType(fileName, type);
await fs.outputFile(filePath, content, "utf8");
await fsExtra.outputFile(filePath, content, "utf8");
const pagePath = path.relative(this.outputDir, filePath);
const page = pagePath.match(
/(?<category>[A-z][A-z0-9-]*)[\\/]+(?<pageId>[A-z][A-z0-9-]*).mdx?$/,
Expand Down Expand Up @@ -83,22 +86,26 @@ module.exports = class Renderer {
);

const filePath = path.join(this.outputDir, SIDEBAR);
await fs.outputFile(filePath, content, "utf8");
await fsExtra.outputFile(filePath, content, "utf8");

return path.relative("./", filePath);
}

async renderHomepage(homepageLocation) {
const homePage = path.basename(homepageLocation);
const destLocation = path.join(this.outputDir, homePage);
await fs.copy(homepageLocation, destLocation);
const slug = pathUrl.resolve("/", this.baseURL);

await fsExtra.copy(homepageLocation, destLocation);

const template = await fs.readFile(destLocation, "utf8");

const data = template
.replace(/##baseURL##/gm, pathUrl.resolve("/", this.baseURL))
.replace(/##baseURL##/gm, slug)
.replace(
/##generated-date-time##/gm,
moment().format("MMMM DD, YYYY [at] h:mm:ss A"),
);
await fs.outputFile(destLocation, data, "utf8");
await fsExtra.outputFile(destLocation, data, "utf8");
}
};
24 changes: 12 additions & 12 deletions tests/unit/lib/renderer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const Renderer = require("@/lib/renderer");
jest.mock("@/lib/printer");
const Printer = require("@/lib/printer");

const FOLDER = "output";
const OUTPUT = "output";
const HOMEPAGE = "generated.md";
const SIDEBAR = "sidebar.json";

Expand All @@ -23,14 +23,14 @@ describe("lib", () => {
node_modules: mock.load(
path.resolve(__dirname, "../../../node_modules"),
),
output: {},
[OUTPUT]: {},
assets: {
[HOMEPAGE]: mock.load(require.resolve(`@assets/${HOMEPAGE}`)),
[SIDEBAR]: mock.load(require.resolve(`@assets/${SIDEBAR}`)),
},
});
printerInstance = new Printer("SCHEMA", baseURL, "root");
rendererInstance = new Renderer(printerInstance, FOLDER, baseURL);
rendererInstance = new Renderer(printerInstance, OUTPUT, baseURL);
});

afterEach(() => {
Expand All @@ -43,7 +43,7 @@ describe("lib", () => {
jest
.spyOn(printerInstance, "printType")
.mockReturnValue("Lorem ipsum");
const output = `${FOLDER}/foobar`;
const output = `${OUTPUT}/foobar`;

const meta = await rendererInstance.renderTypeEntities(
output,
Expand All @@ -52,8 +52,8 @@ describe("lib", () => {
);
expect(meta).toEqual({ category: "Foobar", slug: "foobar/foo-bar" });

const folder = dirTree(FOLDER);
expect(folder).toMatchSnapshot();
const outputFolder = dirTree(OUTPUT);
expect(outputFolder).toMatchSnapshot();
});
});

Expand All @@ -63,8 +63,8 @@ describe("lib", () => {

await rendererInstance.renderSidebar();

const folder = dirTree(FOLDER);
expect(folder).toMatchSnapshot();
const outputFolder = dirTree(OUTPUT);
expect(outputFolder).toMatchSnapshot();
});
});

Expand All @@ -73,8 +73,8 @@ describe("lib", () => {
expect.assertions(1);
await rendererInstance.renderHomepage(`assets/${HOMEPAGE}`);

const folder = dirTree(FOLDER);
expect(folder).toMatchSnapshot({
const outputFolder = dirTree(OUTPUT);
expect(outputFolder).toMatchSnapshot({
children: [{ size: expect.any(Number) }],
size: expect.any(Number),
});
Expand All @@ -92,8 +92,8 @@ describe("lib", () => {
{ name: "bar" },
]);

const folder = dirTree(FOLDER);
expect(folder).toMatchSnapshot();
const outputFolder = dirTree(OUTPUT);
expect(outputFolder).toMatchSnapshot();
});
});
});
Expand Down

0 comments on commit 390de07

Please sign in to comment.