refactor: sortFiles -> toSortedArray

This commit is contained in:
Oliver Eyton-Williams 2020-07-08 16:23:09 +02:00 committed by Mrugesh Mohapatra
parent 1ee5e24d0f
commit 52cb6adf02
4 changed files with 10 additions and 10 deletions

View File

@ -16,7 +16,7 @@ import {
} from '../redux';
import { userSelector, isDonationModalOpenSelector } from '../../../redux';
import { Loader } from '../../../components/helpers';
import { sortFiles } from '../../../../../utils/sort-files';
import { toSortedArray } from '../../../../../utils/sort-files';
import './editor.css';
@ -164,7 +164,7 @@ class Editor extends Component {
// NOTE: the ARIA state is controlled by fileKey, so changes to it must
// trigger a re-render. Hence state:
this.state = {
fileKey: sortFiles(challengeFiles)[0].key
fileKey: toSortedArray(challengeFiles)[0].key
};
this.options = {

View File

@ -42,7 +42,7 @@ const {
} = require('../../client/utils/challengeTypes');
const { dasherize } = require('../../utils/slugs');
const { sortFiles } = require('../../utils/sort-files');
const { toSortedArray } = require('../../utils/sort-files');
const { testedLang } = require('../utils');
@ -423,7 +423,7 @@ async function createTestRunner(challenge, solution, buildChallenge) {
});
} else if (solution) {
// fallback for single solution
const sortedFiles = sortFiles(files);
const sortedFiles = toSortedArray(files);
files[sortedFiles[0].key].contents = solution;
} else {

View File

@ -1,4 +1,4 @@
exports.sortFiles = function sortFiles(challengeFiles) {
exports.toSortedArray = function toSortedArray(challengeFiles) {
const xs = Object.values(challengeFiles);
// TODO: refactor this to use an ext array ['html', 'js', 'css'] and loop over
// that.

View File

@ -1,23 +1,23 @@
/* global expect */
const { sortFiles } = require('./sort-files');
const { toSortedArray } = require('./sort-files');
const { challengeFiles } = require('./__fixtures__/challenges');
describe('sort-files', () => {
describe('sortFiles', () => {
describe('toSortedArray', () => {
it('should return an array', () => {
const sorted = sortFiles(challengeFiles);
const sorted = toSortedArray(challengeFiles);
expect(Array.isArray(sorted)).toBe(true);
});
it('should not modify the challenges', () => {
const sorted = sortFiles(challengeFiles);
const sorted = toSortedArray(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 sorted = toSortedArray(challengeFiles);
const sortedKeys = sorted.map(({ key }) => key);
const expected = ['indexhtml', 'indexjsx', 'indexjs', 'indexcss'];
expect(sortedKeys).toStrictEqual(expected);