github.com/blixtra/nomad@v0.7.2-0.20171221000451-da9a1d7bb050/ui/tests/integration/job-diff-test.js (about)

     1  import { findAll, find } from 'ember-native-dom-helpers';
     2  import { test, moduleForComponent } from 'ember-qunit';
     3  import hbs from 'htmlbars-inline-precompile';
     4  
     5  moduleForComponent('job-diff', 'Integration | Component | job diff', {
     6    integration: true,
     7  });
     8  
     9  const commonTemplate = hbs`
    10    <div class="boxed-section">
    11      <div class="boxed-section-body is-dark">
    12        {{job-diff diff=diff}}
    13      </div>
    14    </div>
    15  `;
    16  
    17  test('job field diffs', function(assert) {
    18    this.set('diff', {
    19      ID: 'test-case-1',
    20      Type: 'Edited',
    21      Objects: null,
    22      Fields: [
    23        field('Removed Field', 'deleted', 12),
    24        field('Added Field', 'added', 'Foobar'),
    25        field('Edited Field', 'edited', 512, 256),
    26      ],
    27    });
    28  
    29    this.render(commonTemplate);
    30  
    31    assert.equal(
    32      findAll('.diff-section-label').length,
    33      5,
    34      'A section label for each line, plus one for the group'
    35    );
    36    assert.equal(
    37      cleanWhitespace(find('.diff-section-label .diff-section-label.is-added').textContent),
    38      '+ Added Field: "Foobar"',
    39      'Added field is rendered correctly'
    40    );
    41    assert.equal(
    42      cleanWhitespace(find('.diff-section-label .diff-section-label.is-edited').textContent),
    43      '+/- Edited Field: "256" => "512"',
    44      'Edited field is rendered correctly'
    45    );
    46    assert.equal(
    47      cleanWhitespace(find('.diff-section-label .diff-section-label.is-deleted').textContent),
    48      '- Removed Field: "12"',
    49      'Removed field is rendered correctly'
    50    );
    51  });
    52  
    53  test('job object diffs', function(assert) {
    54    this.set('diff', {
    55      ID: 'test-case-2',
    56      Type: 'Edited',
    57      Objects: [
    58        {
    59          Name: 'ComplexProperty',
    60          Type: 'Edited',
    61          Objects: null,
    62          Fields: [
    63            field('Prop 1', 'added', 'prop-1-value'),
    64            field('Prop 2', 'none', 'prop-2-is-the-same'),
    65            field('Prop 3', 'edited', 'new value', 'some old value'),
    66            field('Prop 4', 'deleted', 'delete me'),
    67          ],
    68        },
    69        {
    70          Name: 'DeepConfiguration',
    71          Type: 'Added',
    72          Objects: [
    73            {
    74              Name: 'VP Props',
    75              Type: 'Added',
    76              Objects: null,
    77              Fields: [
    78                field('Engineering', 'added', 'Regina Phalange'),
    79                field('Customer Support', 'added', 'Jerome Hendricks'),
    80                field('HR', 'added', 'Jack Blue'),
    81                field('Sales', 'added', 'Maria Lopez'),
    82              ],
    83            },
    84          ],
    85          Fields: [field('Executive Prop', 'added', 'in charge')],
    86        },
    87        {
    88          Name: 'DatedStuff',
    89          Type: 'Deleted',
    90          Objects: null,
    91          Fields: [field('Deprecated', 'deleted', 'useless')],
    92        },
    93      ],
    94      Fields: null,
    95    });
    96  
    97    this.render(commonTemplate);
    98  
    99    assert.ok(
   100      cleanWhitespace(
   101        find('.diff-section-label > .diff-section-label.is-added').textContent
   102      ).startsWith('+ DeepConfiguration {'),
   103      'Added object starts with a JSON block'
   104    );
   105    assert.ok(
   106      cleanWhitespace(
   107        find('.diff-section-label > .diff-section-label.is-edited').textContent
   108      ).startsWith('+/- ComplexProperty {'),
   109      'Edited object starts with a JSON block'
   110    );
   111    assert.ok(
   112      cleanWhitespace(
   113        find('.diff-section-label > .diff-section-label.is-deleted').textContent
   114      ).startsWith('- DatedStuff {'),
   115      'Removed object starts with a JSON block'
   116    );
   117  
   118    assert.ok(
   119      cleanWhitespace(
   120        find('.diff-section-label > .diff-section-label.is-added').textContent
   121      ).endsWith('}'),
   122      'Added object ends the JSON block'
   123    );
   124    assert.ok(
   125      cleanWhitespace(
   126        find('.diff-section-label > .diff-section-label.is-edited').textContent
   127      ).endsWith('}'),
   128      'Edited object starts with a JSON block'
   129    );
   130    assert.ok(
   131      cleanWhitespace(
   132        find('.diff-section-label > .diff-section-label.is-deleted').textContent
   133      ).endsWith('}'),
   134      'Removed object ends the JSON block'
   135    );
   136  
   137    assert.equal(
   138      findAll('.diff-section-label > .diff-section-label.is-added > .diff-section-label').length,
   139      this.get('diff').Objects[1].Objects.length + this.get('diff').Objects[1].Fields.length,
   140      'Edited block contains each nested field and object'
   141    );
   142  
   143    assert.equal(
   144      findAll(
   145        '.diff-section-label > .diff-section-label.is-added > .diff-section-label > .diff-section-label .diff-section-table-row'
   146      ).length,
   147      this.get('diff').Objects[1].Objects[0].Fields.length,
   148      'Objects within objects are rendered'
   149    );
   150  });
   151  
   152  function field(name, type, newVal, oldVal) {
   153    switch (type) {
   154      case 'added':
   155        return {
   156          Annotations: null,
   157          New: newVal,
   158          Old: '',
   159          Type: 'Added',
   160          Name: name,
   161        };
   162      case 'deleted':
   163        return {
   164          Annotations: null,
   165          New: '',
   166          Old: newVal,
   167          Type: 'Deleted',
   168          Name: name,
   169        };
   170      case 'edited':
   171        return {
   172          Annotations: null,
   173          New: newVal,
   174          Old: oldVal,
   175          Type: 'Edited',
   176          Name: name,
   177        };
   178    }
   179    return {
   180      Annotations: null,
   181      New: newVal,
   182      Old: oldVal,
   183      Type: 'None',
   184      Name: name,
   185    };
   186  }
   187  
   188  function cleanWhitespace(string) {
   189    return string
   190      .replace(/\n/g, '')
   191      .replace(/ +/g, ' ')
   192      .trim();
   193  }