UNPKG

tuture

Version:

Write tutorials from the future, with the power of Git and community.

158 lines (157 loc) 5.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const faker_1 = tslib_1.__importDefault(require("faker")); const lodash_clonedeep_1 = tslib_1.__importDefault(require("lodash.clonedeep")); const editure_constants_1 = require("editure-constants"); const utils_1 = require("../../utils"); const index_1 = require("../index"); const nodes_1 = require("../nodes"); function getFakeExplain() { const numOfParagraphs = faker_1.default.random.number({ min: 1, max: 5 }); return { type: 'explain', fixed: true, children: ' ' .repeat(numOfParagraphs) .split('') .map(() => ({ type: editure_constants_1.PARAGRAPH, children: [{ text: faker_1.default.lorem.paragraph() }], })), }; } function getFakeStepTitle(commit) { return { type: 'heading-two', fixed: true, id: utils_1.randHex(8), commit, children: [{ text: faker_1.default.lorem.sentence() }], }; } function getFakeDiffBlock(commit, file) { return { type: 'diff-block', file, commit, children: nodes_1.getEmptyChildren(), }; } function getFakeFile(commit) { const file = faker_1.default.lorem.word(); return { type: 'file', display: true, file, children: [ nodes_1.getEmptyExplain(), getFakeDiffBlock(commit, file), nodes_1.getEmptyExplain(), ], }; } function getFakeFiles(commit, num) { const numOfFiles = num || faker_1.default.random.number({ min: 1, max: 5 }); return ' ' .repeat(numOfFiles) .split('') .map(() => getFakeFile(commit)); } function getFakeStep() { const commit = utils_1.randHex(32); return { type: 'step', id: utils_1.randHex(8), articleId: utils_1.randHex(8), commit, children: [ getFakeStepTitle(commit), nodes_1.getEmptyExplain(), ...getFakeFiles(commit), nodes_1.getEmptyExplain(), ], }; } function getFakeSteps(num) { return ' ' .repeat(num) .split('') .map(() => getFakeStep()); } function populateSteps(steps) { return steps.map((step) => { const clonedStep = lodash_clonedeep_1.default(step); for (let i = 0; i < clonedStep.children.length; i++) { const child = clonedStep.children[i]; if (child.type === 'explain') { clonedStep.children[i] = getFakeExplain(); } else if (child.type === 'file') { child.children[0] = getFakeExplain(); child.children[2] = getFakeExplain(); } } return clonedStep; }); } describe('utils/index', () => { describe('isCommitEqual', () => { const hashA = utils_1.randHex(); const hashB = utils_1.randHex(); test('commits with exactly the same hash', () => { expect(index_1.isCommitEqual(hashA, hashA)).toBe(true); }); test('commits with different hashes', () => { expect(index_1.isCommitEqual(hashA, hashB)).toBe(false); }); test('same commit with different digits', () => { expect(index_1.isCommitEqual(hashA, hashA.slice(0, 8))).toBe(true); expect(index_1.isCommitEqual(hashB.slice(0, 8), hashB)).toBe(true); }); }); describe('mergeSteps', () => { test('add a new step', () => { const [step1, step2, step3] = getFakeSteps(3); const [pStep1, pStep2] = populateSteps([step1, step2]); const prevSteps = [pStep1, pStep2]; const newSteps = [step1, step2, step3]; const mergedSteps = index_1.mergeSteps(prevSteps, newSteps); expect(mergedSteps).toStrictEqual([pStep1, pStep2, step3]); }); test('add multiple new steps', () => { const [step1, step2, step3] = getFakeSteps(3); const [pStep1] = populateSteps([step1]); const prevSteps = [pStep1]; const newSteps = [step1, step2, step3]; const mergedSteps = index_1.mergeSteps(prevSteps, newSteps); expect(mergedSteps).toStrictEqual([pStep1, step2, step3]); }); test('last commit outdated', () => { const [step1, step2, step3] = getFakeSteps(3); const [pStep1, pStep2] = populateSteps([step1, step2]); const prevSteps = [pStep1, pStep2]; const newSteps = [step1, step3]; const mergedSteps = index_1.mergeSteps(prevSteps, newSteps); expect(mergedSteps).toStrictEqual([ pStep1, step3, Object.assign(Object.assign({}, pStep2), { outdated: true }), ]); }); test('all commits outdated', () => { const [step1, step2, step3, step4] = getFakeSteps(4); const [pStep1, pStep2] = populateSteps([step1, step2]); const prevSteps = [pStep1, pStep2]; const newSteps = [step3, step4]; const mergedSteps = index_1.mergeSteps(prevSteps, newSteps); expect(mergedSteps).toStrictEqual([ step3, step4, Object.assign(Object.assign({}, pStep1), { outdated: true }), Object.assign(Object.assign({}, pStep2), { outdated: true }), ]); }); }); describe('isInitialized', () => { }); });