Files
threshold-ui/ui/core/tests/acceptance/targets-test.js
T

316 lines
10 KiB
JavaScript
Raw Normal View History

2020-08-21 13:22:00 -04:00
import { module, test } from 'qunit';
2020-09-21 11:57:33 -04:00
import { visit, currentURL, click, find, findAll, fillIn } from '@ember/test-helpers';
2020-08-21 13:22:00 -04:00
import { setupApplicationTest } from 'ember-qunit';
import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
2020-09-20 16:04:20 -04:00
import a11yAudit from 'ember-a11y-testing/test-support/audit';
import { Response } from 'miragejs';
import {
authenticateSession,
// These are left here intentionally for future reference.
//currentSession,
//invalidateSession,
2020-09-20 16:04:20 -04:00
} from 'ember-simple-auth/test-support';
2020-08-21 13:22:00 -04:00
module('Acceptance | targets', function (hooks) {
setupApplicationTest(hooks);
setupMirage(hooks);
2020-09-20 16:04:20 -04:00
const instances = {
scopes: {
global: null,
org: null,
project: null,
},
target: null,
hostSets: null,
};
const urls = {
orgScope: null,
projects: null,
project: null,
targets: null,
newTarget: null,
target: null,
targetHostSets: null,
targetAddHostSets: null,
};
2020-08-21 13:22:00 -04:00
hooks.beforeEach(function () {
// Setup Mirage mock resources for this test
2020-09-20 16:04:20 -04:00
authenticateSession({});
instances.scopes.global = this.server.create('scope', { id: 'global' });
instances.scopes.org = this.server.create('scope', {
type: 'org',
scope: { id: 'global', type: 'global' },
});
instances.scopes.project = this.server.create('scope', {
type: 'project',
scope: { id: instances.scopes.org.id, type: 'org' },
});
2020-09-21 11:57:33 -04:00
instances.hostCatalog = this.server.create('host-catalog', {
scope: instances.scopes.project
}, 'withChildren');
2020-09-20 16:04:20 -04:00
instances.target = this.server.create('target', {
scope: instances.scopes.project,
2020-09-21 11:57:33 -04:00
hostSets: instances.hostCatalog.hostSets
2020-09-20 16:04:20 -04:00
});
2020-08-21 13:22:00 -04:00
// Generate route URLs for resources
2020-09-20 16:04:20 -04:00
urls.orgScope = `/scopes/${instances.scopes.org.id}`;
urls.projects = `${urls.orgScope}/projects`;
urls.project = `${urls.projects}/${instances.scopes.project.id}`;
urls.targets = `${urls.project}/targets`;
urls.newTarget = `${urls.targets}/new`;
urls.target = `${urls.targets}/${instances.target.id}`;
urls.targetHostSets = `${urls.target}/host-sets`;
urls.targetAddHostSets = `${urls.target}/add-host-sets`;
});
hooks.afterEach(async function() {
const notification = find('.rose-notification');
if(notification) {
await click('.rose-notification-dismiss');
}
2020-08-21 13:22:00 -04:00
});
test('visiting targets', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(1);
await visit(urls.targets);
await a11yAudit();
assert.equal(currentURL(), urls.targets);
2020-08-21 13:22:00 -04:00
});
test('can navigate to a target form', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(1);
await visit(urls.targets);
await click('main tbody .rose-table-header-cell:nth-child(1) a');
await a11yAudit();
assert.equal(currentURL(), urls.target);
2020-08-21 13:22:00 -04:00
});
test('can update a target and save changes', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(2);
await visit(urls.target);
await click('form [type="button"]', 'Activate edit mode');
2020-09-20 16:04:20 -04:00
await fillIn('[name="name"]', 'update name');
await fillIn('[name="default_port"]', '1234');
await click('form [type="submit"]:not(:disabled)');
assert.equal(this.server.db.targets[0].name, 'update name');
assert.equal(this.server.db.targets[0].attributes.default_port, '1234');
2020-08-21 13:22:00 -04:00
});
test('can update a target and cancel changes', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(2);
await visit(urls.target);
await click('form [type="button"]', 'Activate edit mode');
2020-09-20 16:04:20 -04:00
await fillIn('[name="name"]', 'update name');
await fillIn('[name="default_port"]', '1234');
await click('form button:not([type="submit"])');
assert.notEqual(this.server.db.targets[0].name, 'update name');
assert.notEqual(this.server.db.targets[0].port, '1234');
2020-08-21 13:22:00 -04:00
});
test('can create new target', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(1);
const targetsCount = this.server.db.targets.length;
await visit(urls.newTarget);
await fillIn('[name="name"]', 'Target name');
await fillIn('[name="description"]', 'description');
await fillIn('[name="default_port"]', '1234');
await click('form [type="submit"]:not(:disabled)');
assert.equal(this.server.db.targets.length, targetsCount + 1);
2020-08-21 13:22:00 -04:00
});
test('can cancel new target creation', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(2);
const targetsCount = this.server.db.targets.length;
await visit(urls.newTarget);
await fillIn('[name="name"]', 'Target Name');
await click('form button:not([type="submit"])');
assert.equal(this.server.db.targets.length, targetsCount);
assert.equal(currentURL(), urls.targets);
});
test('can delete a target', async function(assert) {
assert.expect(1);
const targetsCount = this.server.db.targets.length;
await visit(urls.target);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
assert.equal(this.server.db.targets.length, targetsCount - 1);
2020-08-21 13:22:00 -04:00
});
test('saving a new target with invalid fields displays error messages', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(2);
this.server.post('/targets', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {
request_fields: [
{
name: 'name',
description: 'Name is required.',
},
],
},
}
);
});
await visit(urls.newTarget);
await fillIn('[name="name"]', 'new target');
await click('form [type="submit"]');
await a11yAudit();
2020-09-21 13:28:03 -04:00
assert.ok(find('[role="alert"]'));
assert.ok(find('.rose-form-error-message'));
2020-08-21 13:22:00 -04:00
});
test('errors are displayed when delete on a target fails', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(1);
this.server.del('/targets/:id', () => {
return new Response(
490,
{},
{
status: 490,
code: 'error',
message: 'Oops.',
}
);
});
await visit(urls.target);
await click('.rose-layout-page-actions .rose-dropdown-button-danger');
await a11yAudit();
2020-09-21 13:28:03 -04:00
assert.ok(find('[role="alert"]'));
2020-08-21 13:22:00 -04:00
});
test('saving an existing target with invalid fields displays error messages', async function (assert) {
2020-09-20 16:04:20 -04:00
assert.expect(2);
this.server.patch('/targets/:id', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {
request_fields: [
{
name: 'name',
description: 'Name is required.',
},
],
},
}
);
});
await visit(urls.target);
await click('form [type="button"]', 'Activate edit mode');
2020-09-20 16:04:20 -04:00
await fillIn('[name="name"]', 'existing target');
await click('form [type="submit"]');
await a11yAudit();
2020-09-21 13:28:03 -04:00
assert.ok(find('[role="alert"]'));
assert.ok(find('.rose-form-error-message'));
2020-08-21 13:22:00 -04:00
});
2020-08-27 09:52:44 -04:00
2020-09-20 16:04:20 -04:00
test('visiting target host sets', async function (assert) {
2020-09-21 11:57:33 -04:00
assert.expect(3);
const targetHostSetCount = instances.target.hostSets.length;
2020-09-20 16:04:20 -04:00
await visit(urls.targetHostSets);
await a11yAudit();
assert.equal(currentURL(), urls.targetHostSets);
2020-09-21 11:57:33 -04:00
assert.ok(targetHostSetCount);
assert.equal(findAll('tbody tr').length, targetHostSetCount);
2020-08-27 09:52:44 -04:00
});
test('can remove a host sets', async function (assert) {
2020-09-21 11:57:33 -04:00
assert.expect(2);
const targetHostSetCount = instances.target.hostSets.length;
await visit(urls.targetHostSets);
assert.equal(findAll('tbody tr').length, targetHostSetCount);
await click('tbody tr .rose-dropdown-button-danger');
assert.equal(findAll('tbody tr').length, targetHostSetCount - 1);
});
2020-09-16 14:33:04 -04:00
2020-09-21 13:28:03 -04:00
test('shows error message on host set remove', async function (assert) {
assert.expect(2);
this.server.post('/targets/:idMethod', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {},
}
);
});
2020-09-21 11:57:33 -04:00
const targetHostSetCount = instances.target.hostSets.length;
await visit(urls.targetHostSets);
assert.equal(findAll('tbody tr').length, targetHostSetCount);
await click('tbody tr .rose-dropdown-button-danger');
2020-09-21 13:28:03 -04:00
assert.ok(find('[role="alert"]'));
});
test('select and save host sets to add', async function (assert) {
assert.expect(3);
instances.target.update({ hostSetIds: [] });
await visit(urls.targetHostSets);
assert.equal(findAll('tbody tr').length, 0);
2020-09-21 11:57:33 -04:00
await click('.rose-layout-page-actions a')
assert.equal(currentURL(), urls.targetAddHostSets);
// Click three times to select, unselect, then reselect (for coverage)
await click('tbody label');
await click('tbody label');
2020-09-21 11:57:33 -04:00
await click('tbody label');
await click('form [type="submit"]');
await visit(urls.targetHostSets);
2020-09-21 13:28:03 -04:00
assert.equal(findAll('tbody tr').length, 1);
2020-08-27 16:38:49 -04:00
});
test('select and cancel host sets to add', async function (assert) {
2020-09-21 11:57:33 -04:00
assert.expect(4);
const targetHostSetCount = instances.target.hostSets.length;
await visit(urls.targetHostSets);
assert.equal(findAll('tbody tr').length, targetHostSetCount);
// first, remove a target host set (otherwise none would be available to add)
await click('tbody tr .rose-dropdown-button-danger');
assert.equal(findAll('tbody tr').length, targetHostSetCount - 1);
await click('.rose-layout-page-actions a')
assert.equal(currentURL(), urls.targetAddHostSets);
await click('tbody label');
await click('form [type="button"]');
await visit(urls.targetHostSets);
assert.equal(findAll('tbody tr').length, targetHostSetCount - 1);
2020-08-27 16:38:49 -04:00
});
2020-09-21 13:28:03 -04:00
test('shows error message on host set add', async function (assert) {
assert.expect(4);
this.server.post('/targets/:idMethod', () => {
return new Response(
400,
{},
{
status: 400,
code: 'invalid_argument',
message: 'The request was invalid.',
details: {},
}
);
});
instances.target.update({ hostSetIds: [] });
await visit(urls.targetAddHostSets);
assert.equal(instances.target.hostSets.length, 0);
assert.equal(currentURL(), urls.targetAddHostSets);
await click('tbody label');
await click('form [type="submit"]');
assert.equal(currentURL(), urls.targetAddHostSets);
assert.equal(instances.target.hostSets.length, 0);
});
2020-08-21 13:22:00 -04:00
});