Skip to content

Commit

Permalink
Testfixes for namespace facet on jobs list
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Jul 2, 2024
1 parent a01cde7 commit 39552cd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 64 deletions.
4 changes: 2 additions & 2 deletions ui/app/controllers/jobs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,14 +449,14 @@ export default class JobsIndexController extends Controller {
@computed('namespaceFacet.{filter,options}')
get filteredNamespaceOptions() {
return this.namespaceFacet.options.filter((ns) =>
ns.key.toLowerCase().includes(this.namespaceFacet.filter)
ns.key.toLowerCase().includes(this.namespaceFacet.filter.toLowerCase())
);
}

@computed('nodePoolFacet.{filter,options}')
get filteredNodePoolOptions() {
return this.nodePoolFacet.options.filter((np) =>
np.key.toLowerCase().includes(this.nodePoolFacet.filter)
np.key.toLowerCase().includes(this.nodePoolFacet.filter.toLowerCase())
);
}

Expand Down
107 changes: 45 additions & 62 deletions ui/tests/acceptance/jobs-list-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
click,
triggerKeyEvent,
typeIn,
visit,
} from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
Expand Down Expand Up @@ -216,7 +217,7 @@ module('Acceptance | jobs list', function (hooks) {
assert.equal(JobsList.jobs.length, 2, 'All jobs by default');

const firstNamespace = server.db.namespaces[0];
await JobsList.visit({ namespace: firstNamespace.id });
await JobsList.visit({ filter: `Namespace == ${firstNamespace.id}` });
assert.equal(JobsList.jobs.length, 1, 'One job in the default namespace');
assert.equal(
JobsList.jobs.objectAt(0).name,
Expand All @@ -225,7 +226,7 @@ module('Acceptance | jobs list', function (hooks) {
);

const secondNamespace = server.db.namespaces[1];
await JobsList.visit({ namespace: secondNamespace.id });
await JobsList.visit({ filter: `Namespace == ${secondNamespace.id}` });

assert.equal(
JobsList.jobs.length,
Expand Down Expand Up @@ -323,11 +324,11 @@ module('Acceptance | jobs list', function (hooks) {
);
});

testSingleSelectFacet('Namespace', {
testFacet('Namespace', {
facet: JobsList.facets.namespace,
paramName: 'namespace',
expectedOptions: ['All', 'default', 'namespace-2'],
optionToSelect: 'namespace-2',
expectedOptions: ['default', 'namespace-2'],
dynamicStrings: true,
async beforeEach() {
server.create('namespace', { id: 'default' });
server.create('namespace', { id: 'namespace-2' });
Expand All @@ -336,7 +337,7 @@ module('Acceptance | jobs list', function (hooks) {
await JobsList.visit();
},
filter(job, selection) {
return job.namespaceId === selection;
return selection.includes(job.namespaceId);
},
});

Expand Down Expand Up @@ -1581,7 +1582,7 @@ module('Acceptance | jobs list', function (hooks) {
modifyIndex: 9,
});

// By default, start on "All" namespace
// By default, start without a namespace filter applied
await JobsList.visit();
assert
.dom('.job-row')
Expand All @@ -1603,7 +1604,7 @@ module('Acceptance | jobs list', function (hooks) {

// Toggle ns-2 namespace
await JobsList.facets.namespace.toggle();
await JobsList.facets.namespace.options[2].toggle();
await JobsList.facets.namespace.options[1].toggle();

assert
.dom('.job-row')
Expand All @@ -1618,8 +1619,8 @@ module('Acceptance | jobs list', function (hooks) {
);

// Switch to default namespace
await JobsList.facets.namespace.toggle();
await JobsList.facets.namespace.options[1].toggle();
await JobsList.facets.namespace.options[1].toggle(); //ns-2 off
await JobsList.facets.namespace.options[0].toggle(); //default on

assert
.dom('.job-row')
Expand All @@ -1639,6 +1640,27 @@ module('Acceptance | jobs list', function (hooks) {
'10 jobs in "Default" namespace, so second page is not available'
);

// Turn both on
await JobsList.facets.namespace.options[1].toggle(); //ns-2 on, default was already on

assert
.dom('.job-row')
.exists(
{ count: 10 },
'Both-on should show 10 jobs in the default namespace.'
);
assert
.dom('[data-test-job-row="ns-2-job"]')
.doesNotExist(
'The job in the ns-2 namespace should not appear on the first page.'
);

assert
.dom('[data-test-pager="next"]')
.isNotDisabled(
'11 jobs with both namespaces filtered, so second page is available'
);

localStorage.removeItem('nomadPageSize');
});
test('Namespace filter options can be filtered', async function (assert) {
Expand Down Expand Up @@ -1693,8 +1715,8 @@ module('Acceptance | jobs list', function (hooks) {
assert.dom('[data-test-namespace-filter-searchbox]').exists();
// and it should be focused
assert.dom('[data-test-namespace-filter-searchbox]').isFocused();
// and there should be 7 things there
assert.dom('[data-test-dropdown-option]').exists({ count: 7 });
// and there should be 6 things there
assert.dom('[data-test-dropdown-option]').exists({ count: 6 });
await typeIn('[data-test-namespace-filter-searchbox]', 'Bonderman');
assert.dom('[data-test-dropdown-option]').exists({ count: 1 });
document.querySelector('[data-test-namespace-filter-searchbox]').value =
Expand Down Expand Up @@ -1724,10 +1746,13 @@ module('Acceptance | jobs list', function (hooks) {
'Namespace filter should not appear with only one namespace.'
);

let system = this.owner.lookup('service:system');
system.shouldShowNamespaces = true;
server.create('namespace', {
id: 'Bonderman',
name: 'Bonderman',
});

await settled();
await visit('/clients'); // go to another page to force a full refresh
await JobsList.visit();

assert
.dom('[data-test-facet="Namespace"]')
Expand Down Expand Up @@ -1988,7 +2013,7 @@ async function facetOptions(assert, beforeEach, facet, expectedOptions) {

function testFacet(
label,
{ facet, paramName, beforeEach, filter, expectedOptions }
{ facet, paramName, beforeEach, filter, expectedOptions, dynamicStrings }
) {
test(`the ${label} facet has the correct options`, async function (assert) {
await facetOptions(assert, beforeEach, facet, expectedOptions);
Expand Down Expand Up @@ -2061,57 +2086,15 @@ function testFacet(
selection.forEach((selection) => {
let capitalizedParamName =
paramName.charAt(0).toUpperCase() + paramName.slice(1);
// allowing for the possibility of "-" or other characters in the string, we wrap the filter parameter in quotes for namespaces and node pools
assert.ok(
currentURL().includes(
encodeURIComponent(`${capitalizedParamName} == ${selection}`)
dynamicStrings
? encodeURIComponent(`${capitalizedParamName} == "${selection}"`)
: encodeURIComponent(`${capitalizedParamName} == ${selection}`)
),
`URL has the correct query param key and value for ${selection}`
);
});
});
}

function testSingleSelectFacet(
label,
{ facet, paramName, beforeEach, filter, expectedOptions, optionToSelect }
) {
test(`the ${label} facet has the correct options`, async function (assert) {
await facetOptions(assert, beforeEach, facet, expectedOptions);
});

test(`the ${label} facet filters the jobs list by ${label}`, async function (assert) {
await beforeEach();
await facet.toggle();

const option = facet.options.findOneBy('label', optionToSelect);
const selection = option.label;
await option.toggle();

const expectedJobs = server.db.jobs
.filter((job) => filter(job, selection))
.sortBy('modifyIndex')
.reverse();

JobsList.jobs.forEach((job, index) => {
assert.equal(
job.id,
expectedJobs[index].id,
`Job at ${index} is ${expectedJobs[index].id}`
);
});
});

test(`selecting an option in the ${label} facet updates the ${paramName} query param`, async function (assert) {
await beforeEach();
await facet.toggle();

const option = facet.options.objectAt(1);
const selection = option.label;
await option.toggle();

assert.ok(
currentURL().includes(`${paramName}=${selection}`),
'URL has the correct query param key and value'
);
});
}

0 comments on commit 39552cd

Please sign in to comment.