Skip to content

Commit

Permalink
refactor(Common): Added validateTracks() in Common module, used in To…
Browse files Browse the repository at this point in the history
…pic, Project and Challenge modules
  • Loading branch information
mfdebian committed Nov 10, 2023
1 parent 9cd9762 commit 0b7829f
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
26 changes: 24 additions & 2 deletions lib/__tests__/common.spec.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { describe, it, expect, vi } from 'vitest';
import { resolveFixturePath } from './helpers.js';
import {
parseDirname,
splitMetaAndContent,
getMetaFromFile,
getTitle,
parseDirname,
splitMetaAndContent,
validateTracks,
} from '../common.js';

vi.mock('sharp');
Expand Down Expand Up @@ -40,6 +41,27 @@ describe('splitMetaAndContent', () => {
});
});

describe('validateTracks', () => {
it('should reject if tracks is not an array', () => {
[null, undefined, 420, 'foo', {}].forEach((variable) => {
expect(() => validateTracks(variable))
.toThrow('No tracks found. Expected at least one.');
});
});

it('should reject if tracks array is empty', () => {
const emptyTracks = [];
expect(() => validateTracks(emptyTracks))
.toThrow('No tracks found. Expected at least one.');
});

it('should reject if tracks contains unknown track', () => {
const badTracks = ['web-dev', 'ux', 'data', 'foo'];
expect(() => validateTracks(badTracks))
.toThrow('Found unknown track "foo". Expected "web-dev", "ux" or "data".');
});
});

describe('getMetaFromFile', () => {
it('should reject when project.yml is malformed', () => {
const dir = resolveFixturePath('01-a-project-with-malformed-yml');
Expand Down
5 changes: 2 additions & 3 deletions lib/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
parseDirname,
parseReadmes,
parser,
validateTracks,
} from './common.js';

const knownEnvs = ['cjs', 'dom', 'form'];
Expand Down Expand Up @@ -222,9 +223,7 @@ export const parseChallenge = async (dir, opts, pkg) => {

const { track, tracks } = meta;

if (!tracks) {
throw new Error('No tracks found. Expected at least one.');
}
validateTracks(tracks);

return {
slug,
Expand Down
12 changes: 12 additions & 0 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,18 @@ export const parseReadmes = async (dir, langs, type, fn) => {
};
};

export const validateTracks = (tracks) => {
if (!Array.isArray(tracks) || !tracks.length) {
throw new Error('No tracks found. Expected at least one.');
}

tracks.forEach((track) => {
if (!['web-dev', 'ux', 'data'].includes(track)) {
throw new Error(`Found unknown track "${track}". Expected "web-dev", "ux" or "data".`);
}
});
};

export const comparePrefixedDirs = (a, b) => (
parseInt(a.split('-')[0], 10) - parseInt(b.split('-')[0], 10)
);
Expand Down
5 changes: 2 additions & 3 deletions lib/project.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
parseReadmes,
getTitle,
parser,
validateTracks,
} from './common.js';

const getSummary = (rootNode) => {
Expand Down Expand Up @@ -160,9 +161,7 @@ export const parseProject = async (dir, opts, pkg) => {
throw new Error(`Invalid track "${track}". Expected "web-dev", "ux" or "data".`);
}

if (!tracks) {
throw new Error('No tracks found. Expected at least one.');
}
validateTracks(tracks);

const learningObjectives = await transformLearningObjectives(dir, opts, meta);

Expand Down
5 changes: 2 additions & 3 deletions lib/topic.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
parseDirname,
parseReadmes,
parser,
validateTracks,
} from './common.js';
import { parsePart } from './part.js';

Expand Down Expand Up @@ -172,9 +173,7 @@ export const parseTopic = async (dir, opts, pkg) => {
throw new Error(`Invalid track "${track}". Expected "web-dev", "ux" or "data".`);
}

if (!tracks) {
throw new Error('No tracks found. Expected at least one.');
}
validateTracks(tracks);

const units = await parseUnits(dir, langs, parsedLocales);
const { errors: duplicateSlugErrors } = units.reduce(
Expand Down

0 comments on commit 0b7829f

Please sign in to comment.