|
| 1 | +// TODO: Refactor to promises. |
| 2 | +import test from 'ava'; |
| 3 | +import * as path from 'path'; |
| 4 | +import * as sinon from 'sinon'; |
| 5 | + |
| 6 | +import { FileDriver } from './FileDriver'; |
| 7 | + |
| 8 | +test('should be a function', (t) => { |
| 9 | + t.is(typeof FileDriver, 'function'); |
| 10 | +}); |
| 11 | + |
| 12 | +test('should load files from the file system and extract data', (t) => { |
| 13 | + const files = [ |
| 14 | + `file/a.md`, |
| 15 | + `file/b.md`, |
| 16 | + ]; |
| 17 | + const glob = { |
| 18 | + sync: sinon.stub().returns(files), |
| 19 | + }; |
| 20 | + const fs = { |
| 21 | + readFileSync: sinon.stub().returns(`fake-content`), |
| 22 | + }; |
| 23 | + const extractData = sinon.spy(); |
| 24 | + const extractor = sinon.stub().returns({ extractData }); |
| 25 | + const driver = new FileDriver(glob, path, fs, extractor, `/some/cwd`); |
| 26 | + |
| 27 | + driver.getAll(`some-table`); |
| 28 | + |
| 29 | + t.true(glob.sync.calledOnce); |
| 30 | + |
| 31 | + t.true(fs.readFileSync.calledTwice); |
| 32 | + t.true(fs.readFileSync.calledWith(`file/a.md`)); |
| 33 | + t.true(fs.readFileSync.calledWith(`file/b.md`)); |
| 34 | + |
| 35 | + t.true(extractor.calledTwice); |
| 36 | + t.true(extractor.calledWith(`fake-content`)); |
| 37 | + |
| 38 | + t.true(extractData.calledTwice); |
| 39 | +}); |
| 40 | + |
| 41 | +test('should load file from the file system and extract data', (t) => { |
| 42 | + const glob = { sync: () => ([]) }; |
| 43 | + const fs = { |
| 44 | + readFileSync: sinon.stub().returns(`fake-content`), |
| 45 | + }; |
| 46 | + const extractData = sinon.spy(); |
| 47 | + const extractor = sinon.stub().returns({ extractData }); |
| 48 | + const cwd = `/some/cwd`; |
| 49 | + const table = `some-table`; |
| 50 | + const id = `some-id`; |
| 51 | + const driver = new FileDriver(glob, path, fs, extractor, cwd); |
| 52 | + |
| 53 | + driver.getById(id, table); |
| 54 | + |
| 55 | + t.true(fs.readFileSync.calledWith(`${cwd}/resources/${table}/${id}.md`)); |
| 56 | + t.true(extractor.calledWith(`fake-content`)); |
| 57 | +}); |
0 commit comments