github.com/hernad/nomad@v1.6.112/ui/tests/unit/serializers/application-test.js (about)

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