2022-03-02 16:12:20 +01:00
|
|
|
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
|
2022-01-25 11:34:16 +01:00
|
|
|
import fs from 'fs';
|
|
|
|
import ObjectID from 'bson-objectid';
|
|
|
|
import glob from 'glob';
|
2022-03-02 16:12:20 +01:00
|
|
|
import * as matter from 'gray-matter';
|
2022-01-25 11:34:16 +01:00
|
|
|
import mock from 'mock-fs';
|
2021-07-06 19:22:12 -05:00
|
|
|
|
|
|
|
// NOTE:
|
|
|
|
// Use `console.log()` before mocking the filesystem or use
|
|
|
|
// `process.stdout.write()` instead. There are issues when using `mock-fs` and
|
|
|
|
// `require`.
|
|
|
|
|
|
|
|
jest.mock('bson-objectid', () => {
|
2022-01-25 11:34:16 +01:00
|
|
|
return jest.fn(() => ({ toString: () => mockChallengeId }));
|
2021-07-06 19:22:12 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
jest.mock('./helpers/get-step-template', () => {
|
|
|
|
return {
|
|
|
|
getStepTemplate: jest.fn(() => 'Mock template...')
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
const mockChallengeId = '60d35cf3fe32df2ce8e31b03';
|
2022-01-25 11:34:16 +01:00
|
|
|
import { getStepTemplate } from './helpers/get-step-template';
|
2022-03-02 16:12:20 +01:00
|
|
|
import { createStepFile, insertStepIntoMeta, updateStepTitles } from './utils';
|
2021-07-06 19:22:12 -05:00
|
|
|
|
|
|
|
describe('Challenge utils helper scripts', () => {
|
|
|
|
describe('createStepFile util', () => {
|
|
|
|
it('should create next step and return its identifier', () => {
|
|
|
|
mock({
|
|
|
|
'project/': {
|
2021-10-21 10:07:52 -07:00
|
|
|
'step-001.md': 'Lorem ipsum...',
|
|
|
|
'step-002.md': 'Lorem ipsum...'
|
2021-07-06 19:22:12 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const step = createStepFile({
|
|
|
|
projectPath: 'project/',
|
|
|
|
stepNum: 3
|
|
|
|
});
|
|
|
|
|
2022-01-25 11:34:16 +01:00
|
|
|
expect(step.toString()).toEqual(mockChallengeId);
|
2021-07-06 19:22:12 -05:00
|
|
|
expect(ObjectID).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// Internal tasks
|
|
|
|
// - Should generate a template for the step that is being created
|
|
|
|
expect(getStepTemplate).toHaveBeenCalledTimes(1);
|
|
|
|
|
|
|
|
// - Should write a file with a given name and template
|
|
|
|
const files = glob.sync(`project/*.md`);
|
|
|
|
|
|
|
|
expect(files).toEqual([
|
2022-03-02 16:12:20 +01:00
|
|
|
`project/${mockChallengeId}.md`,
|
2021-10-21 10:07:52 -07:00
|
|
|
`project/step-001.md`,
|
2022-03-02 16:12:20 +01:00
|
|
|
`project/step-002.md`
|
2021-07-06 19:22:12 -05:00
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
describe('insertStepIntoMeta util', () => {
|
|
|
|
it('should update the meta with a new file id and name', () => {
|
2021-07-06 19:22:12 -05:00
|
|
|
mock({
|
|
|
|
'_meta/project/': {
|
2022-03-02 16:12:20 +01:00
|
|
|
'meta.json': `{"id": "mock-id",
|
|
|
|
"challengeOrder": [
|
|
|
|
[
|
|
|
|
"id-1",
|
|
|
|
"Step 1"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"id-2",
|
|
|
|
"Step 2"
|
|
|
|
],
|
|
|
|
[
|
|
|
|
"id-3",
|
|
|
|
"Step 3"
|
|
|
|
]
|
|
|
|
]}`
|
2021-07-06 19:22:12 -05:00
|
|
|
}
|
|
|
|
});
|
2022-03-02 16:12:20 +01:00
|
|
|
process.env.CALLING_DIR = 'english/superblock/project';
|
|
|
|
|
|
|
|
insertStepIntoMeta({ stepNum: 3, stepId: new ObjectID(mockChallengeId) });
|
|
|
|
|
|
|
|
const meta = JSON.parse(
|
|
|
|
fs.readFileSync('_meta/project/meta.json', 'utf8')
|
|
|
|
);
|
|
|
|
expect(meta).toEqual({
|
|
|
|
id: 'mock-id',
|
|
|
|
challengeOrder: [
|
|
|
|
['id-1', 'Step 1'],
|
|
|
|
['id-2', 'Step 2'],
|
|
|
|
[mockChallengeId, 'Step 3'],
|
|
|
|
['id-3', 'Step 4']
|
|
|
|
]
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
describe('updateStepTitles util', () => {
|
|
|
|
it('should apply meta.challengeOrder to step files', () => {
|
|
|
|
mock({
|
|
|
|
'_meta/project/': {
|
|
|
|
'meta.json':
|
|
|
|
'{"id": "mock-id", "challengeOrder": [["id-1", "Step 1"], ["id-3", "Step 2"], ["id-2", "Step 3"]]}'
|
|
|
|
},
|
|
|
|
'english/superblock/project/': {
|
|
|
|
'id-1.md': `---
|
|
|
|
id: id-1
|
|
|
|
title: Step 2
|
|
|
|
challengeType: a
|
|
|
|
dashedName: step-2
|
|
|
|
---
|
|
|
|
`,
|
|
|
|
'id-2.md': `---
|
|
|
|
id: id-2
|
|
|
|
title: Step 1
|
|
|
|
challengeType: b
|
|
|
|
dashedName: step-1
|
|
|
|
---
|
|
|
|
`,
|
|
|
|
'id-3.md': `---
|
|
|
|
id: id-3
|
|
|
|
title: Step 3
|
|
|
|
challengeType: c
|
|
|
|
dashedName: step-3
|
|
|
|
---
|
|
|
|
`
|
|
|
|
}
|
|
|
|
});
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
process.env.CALLING_DIR = 'english/superblock/project';
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
updateStepTitles();
|
2021-07-06 19:22:12 -05:00
|
|
|
|
2022-03-02 16:12:20 +01:00
|
|
|
expect(matter.read('english/superblock/project/id-1.md').data).toEqual({
|
|
|
|
id: 'id-1',
|
|
|
|
title: 'Step 1',
|
|
|
|
challengeType: 'a',
|
|
|
|
dashedName: 'step-1'
|
|
|
|
});
|
|
|
|
expect(matter.read('english/superblock/project/id-2.md').data).toEqual({
|
|
|
|
id: 'id-2',
|
|
|
|
title: 'Step 3',
|
|
|
|
challengeType: 'b',
|
|
|
|
dashedName: 'step-3'
|
|
|
|
});
|
|
|
|
expect(matter.read('english/superblock/project/id-3.md').data).toEqual({
|
|
|
|
id: 'id-3',
|
|
|
|
title: 'Step 2',
|
|
|
|
challengeType: 'c',
|
|
|
|
dashedName: 'step-2'
|
|
|
|
});
|
2021-07-06 19:22:12 -05:00
|
|
|
});
|
|
|
|
});
|
2022-01-25 11:34:16 +01:00
|
|
|
afterEach(() => {
|
|
|
|
mock.restore();
|
2022-03-02 16:12:20 +01:00
|
|
|
delete process.env.CALLING_DIR;
|
2022-01-25 11:34:16 +01:00
|
|
|
});
|
2021-07-06 19:22:12 -05:00
|
|
|
});
|