Bring in data-directory, let's go async file reads (#16782)
* Bring in data-directory, let's go async file reads * Lint fixes * Update glossary.js
This commit is contained in:
15
tests/unit/data-directory/filename-to-key.js
Normal file
15
tests/unit/data-directory/filename-to-key.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const filenameToKey = require('../../../lib/filename-to-key')
|
||||
|
||||
describe('filename-to-key', () => {
|
||||
test('converts filenames to object keys', () => {
|
||||
expect(filenameToKey('foo/bar/baz.txt')).toBe('foo.bar.baz')
|
||||
})
|
||||
|
||||
test('ignores leading slash on filenames', () => {
|
||||
expect(filenameToKey('/foo/bar/baz.txt')).toBe('foo.bar.baz')
|
||||
})
|
||||
|
||||
test('supports MS Windows paths', () => {
|
||||
expect(filenameToKey('path\\to\\file.txt')).toBe('path.to.file')
|
||||
})
|
||||
})
|
||||
1
tests/unit/data-directory/fixtures/README.md
Normal file
1
tests/unit/data-directory/fixtures/README.md
Normal file
@@ -0,0 +1 @@
|
||||
I am a README. I am ignored by default.
|
||||
1
tests/unit/data-directory/fixtures/bar.yaml
Normal file
1
tests/unit/data-directory/fixtures/bar.yaml
Normal file
@@ -0,0 +1 @@
|
||||
another_markup_language: 'yes'
|
||||
1
tests/unit/data-directory/fixtures/foo.json
Normal file
1
tests/unit/data-directory/fixtures/foo.json
Normal file
@@ -0,0 +1 @@
|
||||
{"meaningOfLife": 42}
|
||||
1
tests/unit/data-directory/fixtures/nested/baz.md
Normal file
1
tests/unit/data-directory/fixtures/nested/baz.md
Normal file
@@ -0,0 +1 @@
|
||||
I am markdown!
|
||||
40
tests/unit/data-directory/index.js
Normal file
40
tests/unit/data-directory/index.js
Normal file
@@ -0,0 +1,40 @@
|
||||
const path = require('path')
|
||||
const dataDirectory = require('../../../lib/data-directory')
|
||||
const fixturesDir = path.join(__dirname, 'fixtures')
|
||||
|
||||
describe('data-directory', () => {
|
||||
test('works', async () => {
|
||||
const data = await dataDirectory(fixturesDir)
|
||||
const expected = {
|
||||
bar: { another_markup_language: 'yes' },
|
||||
foo: { meaningOfLife: 42 },
|
||||
nested: { baz: 'I am markdown!' }
|
||||
}
|
||||
expect(data).toEqual(expected)
|
||||
})
|
||||
|
||||
test('option: preprocess function', async () => {
|
||||
const preprocess = function (content) {
|
||||
return content.replace('markdown', 'MARKDOWN')
|
||||
}
|
||||
const data = await dataDirectory(fixturesDir, { preprocess })
|
||||
expect(data.nested.baz).toBe('I am MARKDOWN!')
|
||||
})
|
||||
|
||||
test('option: extensions array', async () => {
|
||||
const extensions = ['.yaml', 'markdown']
|
||||
const data = await dataDirectory(fixturesDir, { extensions })
|
||||
expect('bar' in data).toBe(true)
|
||||
expect('foo' in data).toBe(false) // JSON file should be ignored
|
||||
})
|
||||
|
||||
test('option: ignorePatterns', async () => {
|
||||
const ignorePatterns = []
|
||||
|
||||
// README is ignored by default
|
||||
expect('README' in await dataDirectory(fixturesDir)).toBe(false)
|
||||
|
||||
// README can be included by setting empty ignorePatterns array
|
||||
expect('README' in await dataDirectory(fixturesDir, { ignorePatterns })).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user