From f82886148c2ebbe69cfa1ff880d30ce29849c639 Mon Sep 17 00:00:00 2001 From: Oliver Eyton-Williams Date: Mon, 1 Jun 2020 18:28:22 +0200 Subject: [PATCH] feat: add simple challengeFile sorter --- .../utils/__fixtures__/challenges.js | 38 +++++++++++++++++++ .../templates/Challenges/utils/sort-files.js | 13 +++++++ .../Challenges/utils/sort-files.test.js | 26 +++++++++++++ 3 files changed, 77 insertions(+) create mode 100644 client/src/templates/Challenges/utils/__fixtures__/challenges.js create mode 100644 client/src/templates/Challenges/utils/sort-files.js create mode 100644 client/src/templates/Challenges/utils/sort-files.test.js diff --git a/client/src/templates/Challenges/utils/__fixtures__/challenges.js b/client/src/templates/Challenges/utils/__fixtures__/challenges.js new file mode 100644 index 0000000000..98460e4129 --- /dev/null +++ b/client/src/templates/Challenges/utils/__fixtures__/challenges.js @@ -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: '' + } +}; diff --git a/client/src/templates/Challenges/utils/sort-files.js b/client/src/templates/Challenges/utils/sort-files.js new file mode 100644 index 0000000000..4fb9b9226c --- /dev/null +++ b/client/src/templates/Challenges/utils/sort-files.js @@ -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; +} diff --git a/client/src/templates/Challenges/utils/sort-files.test.js b/client/src/templates/Challenges/utils/sort-files.test.js new file mode 100644 index 0000000000..7a3175a235 --- /dev/null +++ b/client/src/templates/Challenges/utils/sort-files.test.js @@ -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); + }); + }); +});