github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/unit/serializers/application-test.js (about)

     1  import { module, test } from 'qunit';
     2  import { setupTest } from 'ember-qunit';
     3  import ApplicationSerializer from 'nomad-ui/serializers/application';
     4  
     5  import Model from '@ember-data/model';
     6  import { attr } from '@ember-data/model';
     7  
     8  class TestSerializer extends ApplicationSerializer {
     9    arrayNullOverrides = ['Things'];
    10  
    11    mapToArray = [
    12      'ArrayableMap',
    13      { beforeName: 'OriginalNameArrayableMap', afterName: 'RenamedArrayableMap' },
    14    ];
    15  
    16    separateNanos = ['Time'];
    17  }
    18  
    19  class TestModel extends Model {
    20    @attr() things;
    21  
    22    @attr() arrayableMap;
    23    @attr() renamedArrayableMap;
    24  
    25    @attr() time;
    26    @attr() timeNanos;
    27  }
    28  
    29  module('Unit | Serializer | Application', function(hooks) {
    30    setupTest(hooks);
    31  
    32    hooks.beforeEach(function() {
    33      this.store = this.owner.lookup('service:store');
    34      this.owner.register('model:test', TestModel);
    35      this.owner.register('serializer:test', TestSerializer);
    36  
    37      this.subject = () => this.store.serializerFor('test');
    38    });
    39  
    40    const normalizationTestCases = [
    41      {
    42        name: 'Null array and maps',
    43        in: {
    44          ID: 'test-test',
    45          Things: null,
    46          ArrayableMap: null,
    47          OriginalNameArrayableMap: null,
    48          Time: 1607839992000100000,
    49        },
    50        out: {
    51          data: {
    52            id: 'test-test',
    53            attributes: {
    54              things: [],
    55              arrayableMap: [],
    56              renamedArrayableMap: [],
    57              time: 1607839992000,
    58              timeNanos: 100096,
    59            },
    60            relationships: {},
    61            type: 'test',
    62          },
    63        },
    64      },
    65      {
    66        name: 'Non-null array and maps',
    67        in: {
    68          ID: 'test-test',
    69          Things: [1, 2, 3],
    70          ArrayableMap: {
    71            b: { Order: 2 },
    72            a: { Order: 1 },
    73            'c.d': { Order: 3 },
    74          },
    75          OriginalNameArrayableMap: {
    76            a: { X: 1 },
    77          },
    78          Time: 1607839992000100000,
    79          SomethingExtra: 'xyz',
    80        },
    81        out: {
    82          data: {
    83            id: 'test-test',
    84            attributes: {
    85              things: [1, 2, 3],
    86              arrayableMap: [
    87                { Name: 'a', Order: 1 },
    88                { Name: 'b', Order: 2 },
    89                { Name: 'c.d', Order: 3 },
    90              ],
    91              renamedArrayableMap: [{ Name: 'a', X: 1 }],
    92              time: 1607839992000,
    93              timeNanos: 100096,
    94            },
    95            relationships: {},
    96            type: 'test',
    97          },
    98        },
    99      },
   100    ];
   101  
   102    normalizationTestCases.forEach(testCase => {
   103      test(`normalization: ${testCase.name}`, async function(assert) {
   104        assert.deepEqual(this.subject().normalize(TestModel, testCase.in), testCase.out);
   105      });
   106    });
   107  });