feat: add simple challengeFile sorter

This commit is contained in:
Oliver Eyton-Williams
2020-06-01 18:28:22 +02:00
committed by Mrugesh Mohapatra
parent b0f18cacc7
commit f82886148c
3 changed files with 77 additions and 0 deletions

View File

@ -0,0 +1,38 @@
export const challengeFiles = {
indexcss: {
contents: 'some css',
error: null,
ext: 'css',
head: '',
history: ['index.css'],
key: 'indexcss',
name: 'index',
path: 'index.css',
seed: 'some css',
tail: ''
},
indexhtml: {
contents: 'some html',
error: null,
ext: 'html',
head: '',
history: ['index.html'],
key: 'indexhtml',
name: 'index',
path: 'index.html',
seed: 'some html',
tail: ''
},
indexjs: {
contents: 'some js',
error: null,
ext: 'js',
head: '',
history: ['index.js'],
key: 'indexjs',
name: 'index',
path: 'index.js',
seed: 'some js',
tail: ''
}
};

View File

@ -0,0 +1,13 @@
export function sortFiles(challengeFiles) {
const xs = Object.values(challengeFiles);
// TODO: refactor this to use an ext array ['html', 'js', 'css'] and loop over
// that.
xs.sort((a, b) => {
if (a.ext === 'html') return -1;
if (b.ext === 'html') return 1;
if (a.ext === 'js') return -1;
if (b.ext === 'js') return 1;
return 0;
});
return xs;
}

View File

@ -0,0 +1,26 @@
/* global expect */
const { sortFiles } = require('./sort-files');
const { challengeFiles } = require('./__fixtures__/challenges');
describe('sort-files', () => {
describe('sortFiles', () => {
it('should return an array', () => {
const sorted = sortFiles(challengeFiles);
expect(Array.isArray(sorted)).toBe(true);
});
it('should not modify the challenges', () => {
const sorted = sortFiles(challengeFiles);
const expected = Object.values(challengeFiles);
expect(sorted).toEqual(expect.arrayContaining(expected));
expect(sorted.length).toEqual(expected.length);
});
it('should sort the objects into html, js, css order', () => {
const sorted = sortFiles(challengeFiles);
const sortedKeys = sorted.map(({ key }) => key);
const expected = ['indexhtml', 'indexjs', 'indexcss'];
expect(sortedKeys).toStrictEqual(expected);
});
});
});