open-nomad/ui/tests/integration/components/lifecycle-chart-test.js

193 lines
5.3 KiB
JavaScript
Raw Normal View History

import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, settled } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { set } from '@ember/object';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
import { create } from 'ember-cli-page-object';
import LifecycleChart from 'nomad-ui/tests/pages/components/lifecycle-chart';
const Chart = create(LifecycleChart);
const tasks = [
{
lifecycleName: 'main',
name: 'main two: 3',
},
{
lifecycleName: 'main',
name: 'main one: 2',
},
{
lifecycleName: 'prestart-ephemeral',
name: 'prestart ephemeral: 0',
},
{
lifecycleName: 'prestart-sidecar',
name: 'prestart sidecar: 1',
},
{
lifecycleName: 'poststart-ephemeral',
name: 'poststart ephemeral: 5',
},
{
lifecycleName: 'poststart-sidecar',
name: 'poststart sidecar: 4',
},
{
lifecycleName: 'poststop',
name: 'poststop: 6',
},
];
2021-12-28 14:45:20 +00:00
module('Integration | Component | lifecycle-chart', function (hooks) {
setupRenderingTest(hooks);
2021-12-28 14:45:20 +00:00
test('it renders stateless phases and lifecycle- and name-sorted tasks', async function (assert) {
this.set('tasks', tasks);
await render(hbs`<LifecycleChart @tasks={{tasks}} />`);
assert.ok(Chart.isPresent);
assert.equal(Chart.phases[0].name, 'Prestart');
assert.equal(Chart.phases[1].name, 'Main');
assert.equal(Chart.phases[2].name, 'Poststart');
assert.equal(Chart.phases[3].name, 'Poststop');
2021-12-28 14:45:20 +00:00
Chart.phases.forEach((phase) => assert.notOk(phase.isActive));
assert.deepEqual(Chart.tasks.mapBy('name'), [
'prestart ephemeral: 0',
'prestart sidecar: 1',
'main one: 2',
'main two: 3',
'poststart sidecar: 4',
'poststart ephemeral: 5',
'poststop: 6',
]);
assert.deepEqual(Chart.tasks.mapBy('lifecycle'), [
'Prestart Task',
'Sidecar Task',
'Main Task',
'Main Task',
'Sidecar Task',
'Poststart Task',
'Poststop Task',
]);
assert.ok(Chart.tasks[0].isPrestartEphemeral);
assert.ok(Chart.tasks[1].isPrestartSidecar);
assert.ok(Chart.tasks[2].isMain);
assert.ok(Chart.tasks[4].isPoststartSidecar);
assert.ok(Chart.tasks[5].isPoststartEphemeral);
assert.ok(Chart.tasks[6].isPoststop);
2021-12-28 14:45:20 +00:00
Chart.tasks.forEach((task) => {
assert.notOk(task.isActive);
assert.notOk(task.isFinished);
});
await componentA11yAudit(this.element, assert);
});
2021-12-28 14:45:20 +00:00
test('it doesnt render when theres only one phase', async function (assert) {
this.set('tasks', [
{
lifecycleName: 'main',
},
]);
await render(hbs`<LifecycleChart @tasks={{tasks}} />`);
assert.notOk(Chart.isPresent);
});
2021-12-28 14:45:20 +00:00
test('it renders all phases when there are any non-main tasks', async function (assert) {
this.set('tasks', [tasks[0], tasks[6]]);
await render(hbs`<LifecycleChart @tasks={{tasks}} />`);
2020-09-01 17:02:10 +00:00
assert.equal(Chart.phases.length, 4);
});
2021-12-28 14:45:20 +00:00
test('it reflects phase and task states when states are passed in', async function (assert) {
this.set(
'taskStates',
2021-12-28 14:45:20 +00:00
tasks.map((task) => {
return { task };
})
);
await render(hbs`<LifecycleChart @taskStates={{taskStates}} />`);
assert.ok(Chart.isPresent);
2021-12-28 14:45:20 +00:00
Chart.phases.forEach((phase) => assert.notOk(phase.isActive));
2021-12-28 14:45:20 +00:00
Chart.tasks.forEach((task) => {
assert.notOk(task.isActive);
assert.notOk(task.isFinished);
});
// Change poststart-ephemeral to be running
this.set('taskStates.4.state', 'running');
await settled();
await componentA11yAudit(this.element, assert);
assert.ok(Chart.tasks[5].isActive);
assert.ok(Chart.phases[1].isActive);
assert.notOk(
Chart.phases[2].isActive,
'the poststart phase is nested within main and should never have the active class'
);
this.set('taskStates.4.finishedAt', new Date());
await settled();
assert.ok(Chart.tasks[5].isFinished);
});
[
{
testName: 'expected active phases',
runningTaskNames: ['prestart ephemeral', 'main one', 'poststop'],
activePhaseNames: ['Prestart', 'Main', 'Poststop'],
},
{
testName: 'sidecar task states dont affect phase active states',
runningTaskNames: ['prestart sidecar', 'poststart sidecar'],
activePhaseNames: [],
},
{
testName: 'poststart ephemeral task states affect main phase active state',
runningTaskNames: ['poststart ephemeral'],
activePhaseNames: ['Main'],
},
].forEach(async ({ testName, runningTaskNames, activePhaseNames }) => {
2021-12-28 14:45:20 +00:00
test(testName, async function (assert) {
this.set(
'taskStates',
tasks.map((task) => ({ task }))
);
await render(hbs`<LifecycleChart @taskStates={{taskStates}} />`);
2021-12-28 14:45:20 +00:00
runningTaskNames.forEach((taskName) => {
const taskState = this.taskStates.find((taskState) =>
taskState.task.name.includes(taskName)
);
set(taskState, 'state', 'running');
});
await settled();
2021-12-28 14:45:20 +00:00
Chart.phases.forEach((Phase) => {
if (activePhaseNames.includes(Phase.name)) {
assert.ok(Phase.isActive, `expected ${Phase.name} not to be active`);
} else {
assert.notOk(Phase.isActive, `expected ${Phase.name} phase not to be active`);
}
});
});
});
});