freeCodeCamp/tools/challenge-md-parser/text-to-data.test.js
Josh Soref 004b99bf8f chore: fix typos in spelling (#38100)
* spelling: accidentally

* spelling: announce

* spelling: assembly

* spelling: avoid

* spelling: backend

* spelling: because

* spelling: claimed

* spelling: candidate

* spelling: certification

* spelling: certified

* spelling: challenge

* spelling: circular

* spelling: it isn't

* spelling: coins

* spelling: combination

* spelling: compliant

* spelling: containers

* spelling: concise

* spelling: deprecated

* spelling: development

* spelling: donor

* spelling: error

* spelling: everything

* spelling: exceed

* spelling: exist

* spelling: falsy

* spelling: faulty

* spelling: forward

* spelling: handle

* spelling: indicates

* spelling: initial

* spelling: integers

* spelling: issealed

* spelling: javascript

* spelling: length

* spelling: maximum

* spelling: minimum

* spelling: mutable

* spelling: notifier

* spelling: coordinate

* spelling: passport

* spelling: perform

* spelling: permuter

* spelling: placeholder

* spelling: progressively

* spelling: semantic

* spelling: submission

* spelling: submit

* spelling: translations

* spelling: turquoise

* spelling: visualization

* spelling: without

* spelling: registration

* spelling: representation
2020-02-08 23:59:10 +05:30

91 lines
3.0 KiB
JavaScript

/* global describe it expect */
const mockAST = require('./fixtures/challenge-html-ast.json');
const adjacentTagsAST = require('./fixtures/adjacent-tags-ast.json');
const textToData = require('./text-to-data');
describe('text-to-data', () => {
const expectedField = 'description';
const otherExpectedField = 'instructions';
const unexpectedField = 'does-not-exist';
let file = { data: {} };
beforeEach(() => {
file = { data: {} };
});
it('should take return a function', () => {
const plugin = textToData(['a-section-id']);
expect(typeof plugin).toEqual('function');
});
it('throws when no argument or the incorrect argument is supplied', () => {
expect.assertions(5);
const expectedError =
"textToData must have an array of section id's supplied";
expect(() => {
textToData();
}).toThrow(expectedError);
expect(() => {
textToData('');
}).toThrow(expectedError);
expect(() => {
textToData({});
}).toThrow(expectedError);
expect(() => {
textToData(1);
}).toThrow(expectedError);
expect(() => {
textToData([]);
}).toThrow(expectedError);
});
it("should only add a value for 'found' section id's", () => {
const plugin = textToData([expectedField, unexpectedField]);
plugin(mockAST, file);
expect(expectedField in file.data && !(unexpectedField in file.data)).toBe(
true
);
});
it('should add a string relating to the section id to `file.data`', () => {
const plugin = textToData([expectedField]);
plugin(mockAST, file);
const expectedText = 'Welcome to freeCodeCamp';
expect(file.data[expectedField].includes(expectedText)).toBe(true);
});
// eslint-disable-next-line max-len
it('should add an empty string relating to the section id without data to `file.data`', () => {
const plugin = textToData([otherExpectedField]);
plugin(mockAST, file);
expect(file.data[otherExpectedField]).toEqual('');
});
it('should preserve nested html', () => {
const plugin = textToData([expectedField]);
plugin(mockAST, file);
const expectedText = `<blockquote>
<p>Some text in a blockquote</p>
<p>Some text in a blockquote, with <code>code</code></p>
</blockquote>`;
expect(file.data[expectedField].includes(expectedText)).toBe(true);
});
// eslint-disable-next-line max-len
it('should not add paragraphs when html elements are separated by whitespace', () => {
const plugin = textToData([expectedField]);
plugin(adjacentTagsAST, file);
const expectedText1 = `<code>code</code> <tag>with more after a space</tag>`;
const expectedText2 = `another pair of <strong>elements</strong> <em>with a space</em>`;
expect(file.data[expectedField].includes(expectedText1)).toBe(true);
expect(file.data[expectedField].includes(expectedText2)).toBe(true);
});
it('should have an output to match the snapshot', () => {
const plugin = textToData([expectedField, otherExpectedField]);
plugin(mockAST, file);
expect(file.data).toMatchSnapshot();
});
});