refactor: move client-only utils inside client (#51120)

This commit is contained in:
Oliver Eyton-Williams
2023-08-02 14:51:10 +02:00
committed by GitHub
parent 4e1e339b04
commit dc8dd9c1f7
13 changed files with 32 additions and 38 deletions
+60
View File
@@ -0,0 +1,60 @@
import { ChallengeFile } from "../../src/redux/prop-types";
export const challengeFiles: ChallengeFile[] = [
{
id: '1',
contents: 'some css',
error: null,
ext: 'css',
head: '',
history: ['styles.css'],
fileKey: 'stylescss',
name: 'styles',
seed: 'some css',
tail: '',
editableRegionBoundaries: [],
usesMultifileEditor: true,
},
{
id: '2',
contents: 'some html',
error: null,
ext: 'html',
head: '',
history: ['index.html'],
fileKey: 'indexhtml',
name: 'index',
seed: 'some html',
tail: '',
editableRegionBoundaries: [],
usesMultifileEditor: true,
},
{
id: '3',
contents: 'some js',
error: null,
ext: 'js',
head: '',
history: ['script.js'],
fileKey: 'scriptjs',
name: 'script',
seed: 'some js',
tail: '',
editableRegionBoundaries: [],
usesMultifileEditor: true,
},
{
id: '4',
contents: 'some jsx',
error: null,
ext: 'jsx',
head: '',
history: ['index.jsx'],
fileKey: 'indexjsx',
name: 'index',
seed: 'some jsx',
tail: '',
editableRegionBoundaries: [],
usesMultifileEditor: true,
}
]
@@ -1,5 +1,5 @@
const path = require('path');
const { sortChallengeFiles } = require('../../../utils/sort-challengefiles');
const { sortChallengeFiles } = require('../sort-challengefiles');
const {
challengeTypes,
viewTypes
+24
View File
@@ -1,3 +1,27 @@
const idToPath = new Map(
Object.entries({
'561add10cb82ac38a17523bc': 'back-end-development-and-apis',
'5a553ca864b52e1d8bceea14': 'data-visualization',
'561acd10cb82ac38a17513bc': 'front-end-development-libraries',
'5e611829481575a52dc59c0e': 'quality-assurance-v7',
'5e6021435ac9d0ecd8b94b00': 'information-security-v7',
'561abd10cb81ac38a17513bc': 'javascript-algorithms-and-data-structures',
'561add10cb82ac38a17513bc': 'responsive-web-design',
'660add10cb82ac38a17513be': 'legacy-back-end',
'561add10cb82ac39a17513bc': 'legacy-data-visualization',
'561add10cb82ac38a17513be': 'legacy-front-end',
'561add10cb82ac38a17213bc': 'information-security-and-quality-assurance',
'561add10cb82ac38a17213bd': 'full-stack',
'5e44431b903586ffb414c951': 'scientific-computing-with-python-v7',
'5e46fc95ac417301a38fb934': 'data-analysis-with-python-v7',
'5e46fc95ac417301a38fb935': 'machine-learning-with-python-v7'
})
);
export const getCertIds = (): IterableIterator<string> => idToPath.keys();
export const getPathFromID = (id: string): string | undefined =>
idToPath.get(id);
export function isBrowser(): boolean {
return typeof window !== 'undefined';
}
+15
View File
@@ -0,0 +1,15 @@
exports.sortChallengeFiles = function sortChallengeFiles(challengeFiles) {
const xs = challengeFiles.slice();
xs.sort((a, b) => {
if (a.history[0] === 'index.html') return -1;
if (b.history[0] === 'index.html') return 1;
if (a.history[0] === 'styles.css') return -1;
if (b.history[0] === 'styles.css') return 1;
if (a.history[0] === 'index.jsx') return -1;
if (b.history[0] === 'index.jsx') return 1;
if (a.history[0] === 'script.js') return -1;
if (b.history[0] === 'script.js') return 1;
return 0;
});
return xs;
};
+24
View File
@@ -0,0 +1,24 @@
const { challengeFiles } = require('./__fixtures__/challenges');
const { sortChallengeFiles } = require('./sort-challengefiles');
describe('sort-files', () => {
describe('sortChallengeFiles', () => {
it('should return an array', () => {
const sorted = sortChallengeFiles(challengeFiles);
expect(Array.isArray(sorted)).toBe(true);
});
it('should not modify the challenges', () => {
const sorted = sortChallengeFiles(challengeFiles);
const expected = challengeFiles;
expect(sorted).toEqual(expect.arrayContaining(expected));
expect(sorted.length).toEqual(expected.length);
});
it('should sort the objects into html, css, jsx, js order', () => {
const sorted = sortChallengeFiles(challengeFiles);
const sortedKeys = sorted.map(({ fileKey }) => fileKey);
const expected = ['indexhtml', 'stylescss', 'indexjsx', 'scriptjs'];
expect(sortedKeys).toStrictEqual(expected);
});
});
});