diff --git a/tools/challenge-md-parser/__snapshots__/solution-to-data.test.js.snap b/tools/challenge-md-parser/__snapshots__/solution-to-data.test.js.snap new file mode 100644 index 0000000000..7557a5413c --- /dev/null +++ b/tools/challenge-md-parser/__snapshots__/solution-to-data.test.js.snap @@ -0,0 +1,14 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`challengeSeed-to-data plugin should have an output to match the snapshot 1`] = ` +Object { + "solutions": Array [ + "function testFunction(arg) { + return arg; +} + +testFunction('hello'); +", + ], +} +`; diff --git a/tools/challenge-md-parser/solution-to-data.js b/tools/challenge-md-parser/solution-to-data.js new file mode 100644 index 0000000000..1b63733f0c --- /dev/null +++ b/tools/challenge-md-parser/solution-to-data.js @@ -0,0 +1,20 @@ +const visit = require('unist-util-visit'); +const { selectAll } = require('hast-util-select'); + +const { sectionFilter } = require('./utils'); + +function createPlugin() { + return function transformer(tree, file) { + function visitor(node) { + if (sectionFilter(node, 'solution')) { + const solutions = selectAll('code', node).map( + element => element.children[0].value + ); + file.data.solutions = solutions; + } + } + visit(tree, 'element', visitor); + }; +} + +module.exports = createPlugin; diff --git a/tools/challenge-md-parser/solution-to-data.test.js b/tools/challenge-md-parser/solution-to-data.test.js new file mode 100644 index 0000000000..a869d1c411 --- /dev/null +++ b/tools/challenge-md-parser/solution-to-data.test.js @@ -0,0 +1,37 @@ +/* global describe it expect beforeEach */ +const mockAST = require('./fixtures/challenge-html-ast.json'); +const solutionToData = require('./solution-to-data'); + +describe('challengeSeed-to-data plugin', () => { + const plugin = solutionToData(); + let file = { data: {} }; + + beforeEach(() => { + file = { data: {} }; + }); + + it('returns a function', () => { + expect(typeof plugin).toEqual('function'); + }); + + it('adds a `solutions` property to `file.data`', () => { + plugin(mockAST, file); + expect('solutions' in file.data).toBe(true); + }); + + it('ensures that the `solutions` property is an array', () => { + plugin(mockAST, file); + expect(Array.isArray(file.data.solutions)).toBe(true); + }); + + it('each entry in the `solutions` array is a string', () => { + plugin(mockAST, file); + + expect(file.data.solutions.every(el => typeof el === 'string')).toBe(true); + }); + + it('should have an output to match the snapshot', () => { + plugin(mockAST, file); + expect(file.data).toMatchSnapshot(); + }); +}); diff --git a/tools/challenge-md-parser/text-to-data.js b/tools/challenge-md-parser/text-to-data.js index 8ff7b8767a..9e186b20d0 100644 --- a/tools/challenge-md-parser/text-to-data.js +++ b/tools/challenge-md-parser/text-to-data.js @@ -1,7 +1,7 @@ const visit = require('unist-util-visit'); const toHTML = require('hast-util-to-html'); -const { sectionFilter } = require('./utils') +const { sectionFilter } = require('./utils'); function textToData(sectionIds) { if (!sectionIds || !Array.isArray(sectionIds) || sectionIds.length <= 0) {