Extract `dedent` to `util`, adding tests (#538)
Summary: There have been a couple of occasions on which we’ve considered using it, but didn’t want to require from `app/`. Test Plan: Unit tests added, with full coverage. wchargin-branch: extract-dedent
This commit is contained in:
parent
6b13ab64a0
commit
4058a3fd85
|
@ -7,7 +7,7 @@ import {match, RouterContext} from "react-router";
|
||||||
|
|
||||||
import {createRoutes} from "./createRoutes";
|
import {createRoutes} from "./createRoutes";
|
||||||
import {resolveTitleFromPath} from "./routeData";
|
import {resolveTitleFromPath} from "./routeData";
|
||||||
import dedent from "./dedent";
|
import dedent from "../util/dedent";
|
||||||
|
|
||||||
export default function render(
|
export default function render(
|
||||||
locals: {+path: string, +assets: {[string]: string}},
|
locals: {+path: string, +assets: {[string]: string}},
|
||||||
|
|
|
@ -0,0 +1,47 @@
|
||||||
|
// @flow
|
||||||
|
|
||||||
|
import dedent from "./dedent";
|
||||||
|
|
||||||
|
describe("util/dedent", () => {
|
||||||
|
it("dedents a simple example", () => {
|
||||||
|
const actual = dedent`\
|
||||||
|
hello
|
||||||
|
good
|
||||||
|
world
|
||||||
|
`;
|
||||||
|
const expected = "hello\n good\nworld\n";
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("interpolates components", () => {
|
||||||
|
const ell = "l";
|
||||||
|
const actual = dedent`\
|
||||||
|
he${ell}${ell}o
|
||||||
|
good
|
||||||
|
wor${ell}d
|
||||||
|
`;
|
||||||
|
const expected = "hello\n good\nworld\n";
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not strip leading whitespace in components", () => {
|
||||||
|
// See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e
|
||||||
|
const code = 'if (true) {\n console.log("hi");\n}';
|
||||||
|
const actual = dedent`\
|
||||||
|
<pre><code>${code}</code></pre>
|
||||||
|
`;
|
||||||
|
const expected = `<pre><code>${code}</code></pre>\n`;
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("does not strip trailing backslashes", () => {
|
||||||
|
// See: https://github.com/wchargin/wchargin.github.io/commit/06475d4cc44a0437c911dd2d4d6275be4381142e
|
||||||
|
const code = "printf '%s' \\\n wat";
|
||||||
|
const actual = dedent`\
|
||||||
|
$ cat foo.sh
|
||||||
|
${code}
|
||||||
|
`;
|
||||||
|
const expected = `$ cat foo.sh\n${code}\n`;
|
||||||
|
expect(actual).toEqual(expected);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue