Commit 9e42590a authored by David Sveningsson's avatar David Sveningsson

feat(transform): adding test utils

parent 7440b3ea
Pipeline #96481700 passed with stages
in 8 minutes and 56 seconds
import fs from "fs";
import { TransformContext } from ".";
import { Source } from "../context";
import { transformFile, transformSource, transformString } from "./test-utils";
jest.mock("fs");
it("transformFile() should read file and apply transformer", () => {
const transformer = jest.fn((source: Source) => [source]);
const readFileSync = jest
.spyOn(fs, "readFileSync")
.mockImplementation(() => "mocked file data");
const result = transformFile(transformer, "foo.html");
expect(readFileSync).toHaveBeenCalledWith("foo.html", "utf-8");
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 1,
"data": "mocked file data",
"filename": "foo.html",
"line": 1,
},
]
`);
});
it("transformString() should apply transformer", () => {
const transformer = jest.fn((source: Source) => [source]);
const result = transformString(transformer, "inline data");
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 1,
"data": "inline data",
"filename": "inline",
"line": 1,
},
]
`);
});
it("transformSource() should apply transformer", () => {
const source: Source = {
filename: "bar.html",
line: 1,
column: 2,
data: "source data",
};
const transformer = jest.fn((source: Source) => [source]);
const result = transformSource(transformer, source);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 2,
"data": "source data",
"filename": "bar.html",
"line": 1,
},
]
`);
});
it("transformSource() should support chaining", () => {
const source: Source = {
filename: "bar.html",
line: 1,
column: 2,
data: "source data",
};
const transformer = jest.fn(function(this: TransformContext, source: Source) {
return this.chain(source, "chained.html");
});
const result = transformSource(transformer, source);
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 2,
"data": "source data",
"filename": "bar.html",
"line": 1,
},
]
`);
});
it("transformSource() should support custom chaining", () => {
const source: Source = {
filename: "bar.html",
line: 1,
column: 2,
data: "source data",
};
const chain = jest.fn((source: Source) => [source]);
const transformer = jest.fn(function(this: TransformContext, source: Source) {
return this.chain(source, "chained.html");
});
const result = transformSource(transformer, source, chain);
expect(chain).toHaveBeenCalledWith(source, "chained.html");
expect(result).toMatchInlineSnapshot(`
Array [
Object {
"column": 2,
"data": "source data",
"filename": "bar.html",
"line": 1,
},
]
`);
});
import fs from "fs";
import { TransformContext, Transformer } from ".";
import { Source } from "../context";
/**
* Helper function to call a transformer function in test-cases.
*
* @param fn - Transformer function to call.
* @param filename - Filename to read data from. Must be readable.
* @param chain - If set this function is called when chaining transformers. Default is pass-thru.
*/
export function transformFile(
fn: Transformer,
filename: string,
chain?: (source: Source, filename: string) => Iterable<Source>
): Source[] {
const data = fs.readFileSync(filename, "utf-8");
const source: Source = {
filename,
line: 1,
column: 1,
data,
};
return transformSource(fn, source, chain);
}
/**
* Helper function to call a transformer function in test-cases.
*
* @param fn - Transformer function to call.
* @param data - String to transform.
* @param chain - If set this function is called when chaining transformers. Default is pass-thru.
*/
export function transformString(
fn: Transformer,
data: string,
chain?: (source: Source, filename: string) => Iterable<Source>
): Source[] {
const source: Source = {
filename: "inline",
line: 1,
column: 1,
data,
};
return transformSource(fn, source, chain);
}
/**
* Helper function to call a transformer function in test-cases.
*
* @param fn - Transformer function to call.
* @param data - Source to transform.
* @param chain - If set this function is called when chaining transformers. Default is pass-thru.
*/
export function transformSource(
fn: Transformer,
source: Source,
chain?: (source: Source, filename: string) => Iterable<Source>
): Source[] {
const defaultChain = (source: Source): Iterable<Source> => [source];
const context: TransformContext = {
chain: chain || defaultChain,
};
return Array.from(fn.call(context, source));
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment