Skip to content

Commit

Permalink
feat: Add mock interpreter for FileSystem effects
Browse files Browse the repository at this point in the history
  • Loading branch information
edahlseng committed Jul 26, 2019
1 parent 104c4e8 commit f83bc25
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 3 deletions.
29 changes: 29 additions & 0 deletions sources/fileSystem.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { taggedSum } from "daggy";
import fs from "fs";
import path from "path";
import { assoc } from "ramda";

import { interpreter, send } from "./eff";

Expand Down Expand Up @@ -37,3 +38,31 @@ export const interpretLocalFileSystem = (fileSystemRoot: string) =>
),
}),
});

export const interpretMockFileSystem = ({
fileSystemRoot,
startingFileSystem,
onUpdate,
}: {
fileSystemRoot: string,
startingFileSystem: { ... },
onUpdate?: ({ ... }) => void,
}) => {
let fileSystem = startingFileSystem;

return interpreter({
predicate: x => FileSystem.is(x),
handler: fileSystemEffect =>
fileSystemEffect.cata({
readFile: filePath => continuation =>
continuation(fileSystem[path.resolve(fileSystemRoot, filePath)]),
writeFile: (filePath, content) => continuation => {
fileSystem = assoc(path.resolve(fileSystemRoot, filePath))(content)(
fileSystem,
);
if (onUpdate) onUpdate({ ...fileSystem });
return continuation(null);
},
}),
});
};
57 changes: 54 additions & 3 deletions sources/fileSystem.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import fs from "fs";
import path from "path";

import { run } from "./eff";
import { readFile, writeFile, interpretLocalFileSystem } from "./fileSystem";
import {
readFile,
writeFile,
interpretLocalFileSystem,
interpretMockFileSystem,
} from "./fileSystem";
import { tempDirectory } from "./testUtils.js";

test.cb("Read file", t => {
test.cb("Local File System – Read file", t => {
const a = "Hello, World! I'm reading!";
const fileName = "./helloWorldReading.txt";

Expand All @@ -28,7 +33,7 @@ test.cb("Read file", t => {
});
});

test.cb("Write file", t => {
test.cb("Local File System – Write file", t => {
const a = "Hello, World! I'm writing!";
const fileName = "./helloWorldWriting.txt";

Expand All @@ -47,3 +52,49 @@ test.cb("Write file", t => {
})(application);
});
});

test.cb("Mock File System – Read file", t => {
const a = "Hello, World! I'm writing!";
const fileName = "./helloWorldWriting.txt";
const directory = "/directory";

const application = readFile(fileName);

let fileSystem = { "/directory/helloWorldWriting.txt": a };

run(
interpretMockFileSystem({
fileSystemRoot: directory,
startingFileSystem: fileSystem,
onUpdate: newFileSystem => {
fileSystem = newFileSystem;
},
}),
)(b => {
t.is(b, a);
t.end();
})(application);
});

test.cb("Mock File System – Write file", t => {
const a = "Hello, World! I'm writing!";
const fileName = "./helloWorldWriting.txt";
const directory = "/directory";

const application = writeFile(fileName, a);

let fileSystem: { [string]: string } = {};

run(
interpretMockFileSystem({
fileSystemRoot: directory,
startingFileSystem: fileSystem,
onUpdate: newFileSystem => {
fileSystem = newFileSystem;
},
}),
)(() => {
t.is(fileSystem["/directory/helloWorldWriting.txt"], a);
t.end();
})(application);
});

0 comments on commit f83bc25

Please sign in to comment.