open-nomad/ui/tests/integration/components/job-diff-test.js

202 lines
5.8 KiB
JavaScript
Raw Normal View History

import { findAll, find, render } from '@ember/test-helpers';
2019-03-13 00:04:16 +00:00
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
2017-09-19 14:47:10 +00:00
import hbs from 'htmlbars-inline-precompile';
import cleanWhitespace from '../../utils/clean-whitespace';
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
2017-09-19 14:47:10 +00:00
2021-12-28 14:45:20 +00:00
module('Integration | Component | job diff', function (hooks) {
2019-03-13 00:04:16 +00:00
setupRenderingTest(hooks);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
const commonTemplate = hbs`
<div class="boxed-section">
<div class="boxed-section-body is-dark">
<JobDiff @diff={{diff}} />
2019-03-13 00:04:16 +00:00
</div>
2017-09-19 14:47:10 +00:00
</div>
2019-03-13 00:04:16 +00:00
`;
2017-09-19 14:47:10 +00:00
2021-12-28 14:45:20 +00:00
test('job field diffs', async function (assert) {
2019-03-13 00:04:16 +00:00
this.set('diff', {
ID: 'test-case-1',
Type: 'Edited',
Objects: null,
Fields: [
field('Removed Field', 'deleted', 12),
field('Added Field', 'added', 'Foobar'),
field('Edited Field', 'edited', 512, 256),
],
});
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
await render(commonTemplate);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
assert.equal(
findAll('[data-test-diff-section-label]').length,
5,
'A section label for each line, plus one for the group'
);
assert.equal(
cleanWhitespace(
find('[data-test-diff-section-label="field"][data-test-diff-field="added"]').textContent
),
'+ Added Field: "Foobar"',
'Added field is rendered correctly'
);
assert.equal(
cleanWhitespace(
find('[data-test-diff-section-label="field"][data-test-diff-field="edited"]').textContent
),
'+/- Edited Field: "256" => "512"',
'Edited field is rendered correctly'
);
assert.equal(
cleanWhitespace(
find('[data-test-diff-section-label="field"][data-test-diff-field="deleted"]').textContent
),
'- Removed Field: "12"',
'Removed field is rendered correctly'
);
await componentA11yAudit(this.element, assert);
2017-09-19 14:47:10 +00:00
});
2021-12-28 14:45:20 +00:00
test('job object diffs', async function (assert) {
2019-03-13 00:04:16 +00:00
this.set('diff', {
ID: 'test-case-2',
Type: 'Edited',
Objects: [
{
Name: 'ComplexProperty',
Type: 'Edited',
Objects: null,
Fields: [
field('Prop 1', 'added', 'prop-1-value'),
field('Prop 2', 'none', 'prop-2-is-the-same'),
field('Prop 3', 'edited', 'new value', 'some old value'),
field('Prop 4', 'deleted', 'delete me'),
],
},
{
Name: 'DeepConfiguration',
Type: 'Added',
Objects: [
{
Name: 'VP Props',
Type: 'Added',
Objects: null,
Fields: [
field('Engineering', 'added', 'Regina Phalange'),
field('Customer Support', 'added', 'Jerome Hendricks'),
field('HR', 'added', 'Jack Blue'),
field('Sales', 'added', 'Maria Lopez'),
],
},
],
Fields: [field('Executive Prop', 'added', 'in charge')],
},
{
Name: 'DatedStuff',
Type: 'Deleted',
Objects: null,
Fields: [field('Deprecated', 'deleted', 'useless')],
},
],
Fields: null,
});
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
await render(commonTemplate);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="added"]').textContent
).startsWith('+ DeepConfiguration {'),
'Added object starts with a JSON block'
);
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="edited"]').textContent
).startsWith('+/- ComplexProperty {'),
'Edited object starts with a JSON block'
);
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="deleted"]').textContent
).startsWith('- DatedStuff {'),
'Removed object starts with a JSON block'
);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="added"]').textContent
).endsWith('}'),
'Added object ends the JSON block'
);
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="edited"]').textContent
).endsWith('}'),
'Edited object starts with a JSON block'
);
assert.ok(
cleanWhitespace(
find('[data-test-diff-section-label="object"][data-test-diff-field="deleted"]').textContent
).endsWith('}'),
'Removed object ends the JSON block'
);
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
assert.equal(
findAll(
'[data-test-diff-section-label="object"][data-test-diff-field="added"] > [data-test-diff-section-label]'
).length,
this.diff.Objects[1].Objects.length + this.diff.Objects[1].Fields.length,
2019-03-13 00:04:16 +00:00
'Edited block contains each nested field and object'
);
assert.equal(
findAll(
'[data-test-diff-section-label="object"][data-test-diff-field="added"] [data-test-diff-section-label="object"] [data-test-diff-section-label="field"]'
).length,
this.diff.Objects[1].Objects[0].Fields.length,
2019-03-13 00:04:16 +00:00
'Objects within objects are rendered'
);
await componentA11yAudit(this.element, assert);
2019-03-13 00:04:16 +00:00
});
2017-09-19 14:47:10 +00:00
2019-03-13 00:04:16 +00:00
function field(name, type, newVal, oldVal) {
switch (type) {
case 'added':
return {
Annotations: null,
New: newVal,
Old: '',
Type: 'Added',
Name: name,
};
case 'deleted':
return {
Annotations: null,
New: '',
Old: newVal,
Type: 'Deleted',
Name: name,
};
case 'edited':
return {
Annotations: null,
New: newVal,
Old: oldVal,
Type: 'Edited',
Name: name,
};
}
return {
Annotations: null,
New: newVal,
Old: oldVal,
Type: 'None',
Name: name,
};
2017-09-19 14:47:10 +00:00
}
2019-03-13 00:04:16 +00:00
});