Skip to content

Commit

Permalink
Merge pull request #9 from stofolus/separate-validation-from-mock-dat…
Browse files Browse the repository at this point in the history
…a-fetch

Separate validation from mock data fetch
  • Loading branch information
stofolus authored Apr 26, 2018
2 parents 4c95b99 + 0a069ab commit 8aa561f
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 42 deletions.
12 changes: 4 additions & 8 deletions lib/mockValidator.js → lib/fileMockDataReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ const basePath = path.join(__dirname, `../`, `mocks`);

let mockNumbers = [];

class Validator {
static isMock(pnr) {
this._populateMockData();
return mockNumbers.includes(Pnr.normalizePnr(pnr));
}

static _populateMockData() {
class FileMockDataReader {
static fetch() {
if (mockNumbers.length === 0) {
const files = fs.readdirSync(basePath);
files.forEach(file => {
Expand All @@ -24,7 +19,8 @@ class Validator {
mockNumbers = [...mockNumbers, ...mocks];
});
}
return mockNumbers;
}
}

module.exports = Validator;
module.exports = FileMockDataReader;
26 changes: 26 additions & 0 deletions lib/fileMockDataReader.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const FileMockDataReader = require("./fileMockDataReader");

describe("Validator", () => {
const fileContent = ["Testpersonnummer", "201710022383", "201801202381"].join(
"\r\n"
);

beforeEach(() => {
const mockFs = {
readdirSync: jest.fn(() => ["/some-path"]),
readFileSync: jest.fn(data => fileContent)
};
jest.mock("fs", () => mockFs);
});

describe("fetch()", () => {
test("returns a list of pnrs", () => {
expect(FileMockDataReader.fetch()).toEqual(
expect.arrayContaining(["201801202381", "201710022383"])
);
});
test("can filter out non-pnr lines from mock data", () => {
expect(FileMockDataReader.fetch()).not.toContain("Testpersonnummer");
});
});
});
6 changes: 4 additions & 2 deletions lib/finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const fs = require(`fs`);
const path = require(`path`);
const glob = require(`glob`);
const isTextOrBinary = require(`istextorbinary`);
const Validator = require(`./mockValidator`);
const Validator = require(`./validator`);
const Pnr = require(`./pnr`);
const FileMockDataReader = require("./fileMockDataReader");

const defaultPattern = `!(node_modules){,/**}`;
const validator = new Validator(FileMockDataReader.fetch());

class Finder {
static findPnrs(searchPath, pattern) {
Expand All @@ -32,7 +34,7 @@ class Finder {
let match;
const text = fileBuffer.toString();
while ((match = matcher.exec(text))) {
if (!Validator.isMock(match[0])) {
if (!validator.isMock(match[0])) {
const line = text.substr(0, match.index).split(`\n`).length;
results.push({ pnr: match[0], line });
}
Expand Down
9 changes: 6 additions & 3 deletions lib/finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ const mockFs = {
const mockIsTextOrBinary = {
isTextSync: jest.fn(data => !data.includes(`binary`))
};
const mockMockValidator = {
isMock: jest.fn(pnr => pnr === "195309112489")
const mockValidator = {
isMock: pnr => pnr === "195309112489"
};
jest.doMock("glob", () => mockGlob);
jest.mock("fs", () => mockFs);
jest.mock("istextorbinary", () => mockIsTextOrBinary);
jest.mock("./mockValidator", () => mockMockValidator);
jest.mock("./validator", () => {
return jest.fn().mockImplementation(() => mockValidator);
});
jest.mock("./fileMockDataReader");

const Finder = require("./finder");

Expand Down
29 changes: 0 additions & 29 deletions lib/mockValidator.test.js

This file was deleted.

13 changes: 13 additions & 0 deletions lib/validator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const Pnr = require(`./pnr`);

class Validator {
constructor(testPnrs) {
this.testPnrs = testPnrs;
}

isMock(pnr) {
return this.testPnrs.includes(Pnr.normalizePnr(pnr));
}
}

module.exports = Validator;
20 changes: 20 additions & 0 deletions lib/validator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const Validator = require("./validator");

describe("Validator", () => {
let validator;

beforeEach(() => {
validator = new Validator(["1", "2"]);
});

describe("isMock()", () => {
test("returns true if pnr is in mock data", () => {
expect(validator.isMock("1")).toBeTruthy();
expect(validator.isMock("2")).toBeTruthy();
});

test("returns false if pnr is not in mock data", () => {
expect(validator.isMock("3")).toBeFalsy();
});
});
});

0 comments on commit 8aa561f

Please sign in to comment.