From e84604fcdbc2ce51c18cf8d20d041f0736a8a000 Mon Sep 17 00:00:00 2001 From: Michael Lange Date: Wed, 31 Jul 2019 01:41:00 -0700 Subject: [PATCH] Update factory-based fs tests to sort properly --- ui/mirage/config.js | 18 ++++++++++-------- ui/tests/acceptance/task-fs-test.js | 29 ++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/ui/mirage/config.js b/ui/mirage/config.js index 3a91c530d..ff177ef0e 100644 --- a/ui/mirage/config.js +++ b/ui/mirage/config.js @@ -11,6 +11,15 @@ export function findLeader(schema) { return `${agent.address}:${agent.tags.port}`; } +export function filesForPath(allocFiles, filterPath) { + return allocFiles.where( + file => + (!filterPath || file.path.startsWith(filterPath)) && + file.path.length > filterPath.length && + !file.path.substr(filterPath.length + 1).includes('/') + ); +} + export default function() { this.timing = 0; // delay for each request, automatically set to 0 during testing @@ -307,14 +316,7 @@ export default function() { const clientAllocationFSLsHandler = function({ allocFiles }, { queryParams }) { // Ignore the task name at the beginning of the path const filterPath = queryParams.path.substr(queryParams.path.indexOf('/') + 1); - - const files = allocFiles.where( - file => - (!filterPath || file.path.startsWith(filterPath)) && - file.path.length > filterPath.length && - !file.path.substr(filterPath.length + 1).includes('/') - ); - + const files = filesForPath(allocFiles, filterPath); return this.serialize(files); }; diff --git a/ui/tests/acceptance/task-fs-test.js b/ui/tests/acceptance/task-fs-test.js index a7f8e40f0..6d41f191f 100644 --- a/ui/tests/acceptance/task-fs-test.js +++ b/ui/tests/acceptance/task-fs-test.js @@ -6,9 +6,9 @@ import moment from 'moment'; import setupMirage from 'ember-cli-mirage/test-support/setup-mirage'; import Response from 'ember-cli-mirage/response'; -import moment from 'moment'; import { formatBytes } from 'nomad-ui/helpers/format-bytes'; +import { filesForPath } from 'nomad-ui/mirage/config'; import FS from 'nomad-ui/tests/pages/allocations/task/fs'; @@ -16,6 +16,20 @@ let allocation; let task; let files; +const fileSort = (prop, files) => { + let dir = []; + let file = []; + files.forEach(f => { + if (f.isDir) { + dir.push(f); + } else { + file.push(f); + } + }); + + return dir.sortBy(prop).concat(file.sortBy(prop)); +}; + module('Acceptance | task fs', function(hooks) { setupApplicationTest(hooks); setupMirage(hooks); @@ -89,6 +103,8 @@ module('Acceptance | task fs', function(hooks) { test('navigating allocation filesystem', async function(assert) { await FS.visitPath({ id: allocation.id, name: task.name, path: '/' }); + const sortedFiles = fileSort('name', filesForPath(this.server.schema.allocFiles, '').models); + assert.ok(FS.fileViewer.isHidden); assert.equal(FS.directoryEntries.length, 4); @@ -100,7 +116,7 @@ module('Acceptance | task fs', function(hooks) { assert.equal(FS.breadcrumbs[0].text, 'task-name'); FS.directoryEntries[0].as(directory => { - const fileRecord = files[0]; + const fileRecord = sortedFiles[0]; assert.equal(directory.name, fileRecord.name, 'directories should come first'); assert.ok(directory.isDirectory); assert.equal(directory.size, '', 'directory sizes are hidden'); @@ -109,7 +125,7 @@ module('Acceptance | task fs', function(hooks) { }); FS.directoryEntries[2].as(file => { - const fileRecord = files[4]; + const fileRecord = sortedFiles[2]; assert.equal(file.name, fileRecord.name); assert.ok(file.isFile); assert.equal(file.size, formatBytes([fileRecord.size])); @@ -281,9 +297,12 @@ module('Acceptance | task fs', function(hooks) { test('viewing a file', async function(assert) { await FS.visitPath({ id: allocation.id, name: task.name, path: '/' }); - await FS.directoryEntries[2].visit(); - const fileRecord = files[4]; + const sortedFiles = fileSort('name', filesForPath(this.server.schema.allocFiles, '').models); + const fileRecord = sortedFiles.find(f => !f.isDir); + const fileIndex = sortedFiles.indexOf(fileRecord); + + await FS.directoryEntries[fileIndex].visit(); assert.equal(FS.breadcrumbsText, `${task.name} ${fileRecord.name}`);